debugging

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sheldon

    #1

    debugging

    Hi,

    I would like to view the contents of structure as part of a debugging
    routine.
    The structure is represented by a pointer ptr->data.
    I would like to view the first element in the array, data. This is part
    of debugging this program.
    A problem occured when a 2D binary array: ptr->data was copied to the
    another array in another structure via memcpy(). Since the codes is
    big, I will only post the relevant parts I feel is needed - I might be
    wrong. Here is a summary:

    *************** *************** ************
    struct hdfstruct {
    float** latitude;
    } hdfdata;

    int main(void) {

    printf("Allocat ing memory!\n");
    hdfdata.latitud e = calloc(ROW,size of(float *));
    if(hdfdata.lati tude == NULL) {
    fprintf(stderr, "Failed to allocate memory\n");
    return EXIT_FAILURE;
    }
    for (i = 0; i < 15; i++) {
    hdfdata.latitud e[i] = calloc(15,sizeo f(float));
    if(hdfdata.lati tude == NULL) {
    fprintf(stderr, "Failed to allocate memory\n");
    return EXIT_FAILURE;
    }
    }
    /* Reading the hdf data into the arrays */
    stat = readHDF(hdfdata .latitude,latfi le,"/LAT");
    }
    int readHDF(float **arp, char *hdfname, char *fieldname) {
    /* omitted the parts concerning extracting the data */
    /* I didn't write it so I kow that it works */
    memcpy(arp, cloudproduct->data, 15*15*4);
    return (1) /* zero is returned on error */
    }
    *************** *************** *******
    Now I don't want to sent the structure into the readhdf function as I
    will be calling the function many times so it is necessary to send a
    pointer instead. Still the results is that the entire 2D array has the
    same value and this is wrong. The codes runs without error but an error
    ocurred somewhere.
    To summerize: I am wondering if there is a way to view a structure like
    in python: print list and to comfirm my suspicion about the memcpy to
    the pointer.

    I certainly hope that I gave enough info to get some help.

    Thanks in advance,
    Sheldon

  • Bill Pursell

    #2
    Re: debugging

    Sheldon wrote:
    I would like to view the contents of structure as part of a debugging
    routine.
    <snip>
    To summerize: I am wondering if there is a way to view a structure like
    in python: print list and to comfirm my suspicion about the memcpy to
    the pointer.
    Write a function to print the structure. eg:
    void pretty_print (struct foo *f)
    {
    printf("a=%d\t b=%d\nc=%d\tc=% d\n", f->a, f->b, f->c, f->c);
    }

    Now, in your debugger, make a call to the function.

    --
    Bill Pursell

    Comment

    • Sheldon

      #3
      Re: debugging


      Bill Pursell skrev:
      Sheldon wrote:
      >
      I would like to view the contents of structure as part of a debugging
      routine.
      <snip>
      To summerize: I am wondering if there is a way to view a structure like
      in python: print list and to comfirm my suspicion about the memcpy to
      the pointer.
      >
      Write a function to print the structure. eg:
      void pretty_print (struct foo *f)
      {
      printf("a=%d\t b=%d\nc=%d\tc=% d\n", f->a, f->b, f->c, f->c);
      }
      >
      Now, in your debugger, make a call to the function.
      >
      --
      Bill Pursell
      This would work if I had simple scalars in the structure but there are
      arrays and strings as well as integers/floats. I just would like to
      know what are the names of the variables in the structure and perhaps
      the variable types.

      /Sheldon

      Comment

      • Bill Pursell

        #4
        Re: debugging


        Sheldon wrote:
        Bill Pursell skrev:
        >
        Sheldon wrote:
        I would like to view the contents of structure as part of a debugging
        routine.
        <snip>
        To summerize: I am wondering if there is a way to view a structure like
        in python: print list and to comfirm my suspicion about the memcpy to
        the pointer.
        Write a function to print the structure. eg:
        void pretty_print (struct foo *f)
        {
        printf("a=%d\t b=%d\nc=%d\tc=% d\n", f->a, f->b, f->c, f->c);
        }

        Now, in your debugger, make a call to the function.
        >
        This would work if I had simple scalars in the structure but there are
        arrays and strings as well as integers/floats. I just would like to
        know what are the names of the variables in the structure and perhaps
        the variable types.
        >
        This will work for anything, but the function you need to
        write simply has to account for the content of the structure.
        For example, you can do something like:

        #include <stdio.h>
        #include <stdlib.h>

        struct array_list {
        char *name;
        int *a;
        size_t size_a;
        struct array_list *next;
        };

        void
        print_array(int *a, size_t s)
        {
        size_t i;
        for (i=0; i < s; i++)
        printf("%d ", a[i]);
        }

        void
        print_array_lis t (struct array_list *d)
        {
        struct array_list *this;
        printf("%s = ", d->name);
        print_array( d->a, d->size_a);
        putchar('\n');
        for( this = d->next; this != NULL; this = this->next)
        print_array_lis t(this);
        }

        void * Malloc(size_t size)
        {
        void *ret;
        ret = malloc(size);
        if (ret == NULL)
        exit (EXIT_FAILURE);
        return ret;
        }
        int
        main(void)
        {
        struct array_list a,b;
        int i;

        a.name = "struct a";
        b.name = "struct b";
        a.size_a = 5;
        b.size_a = 3;
        a.a = Malloc(a.size_a * sizeof *a.a);
        b.a = Malloc(b.size_a * sizeof *b.a);
        for (i=0; i < a.size_a; i++)
        a.a[i] = 5 * i;
        for (i=0; i < b.size_a; i++)
        b.a[i] = -2 * i;
        a.next = &b;
        b.next = NULL;

        print_array_lis t (&a);
        return EXIT_SUCCESS;
        }

        This produces the output:
        $ ./a.out
        struct a = 0 5 10 15 20
        struct b = 0 -2 -4

        --
        Bill Pursell

        Comment

        • Sheldon

          #5
          Re: debugging


          Bill Pursell skrev:
          Sheldon wrote:
          Bill Pursell skrev:
          Sheldon wrote:
          >
          I would like to view the contents of structure as part of a debugging
          routine.
          <snip>
          To summerize: I am wondering if there is a way to view a structure like
          in python: print list and to comfirm my suspicion about the memcpy to
          the pointer.
          >
          Write a function to print the structure. eg:
          void pretty_print (struct foo *f)
          {
          printf("a=%d\t b=%d\nc=%d\tc=% d\n", f->a, f->b, f->c, f->c);
          }
          >
          Now, in your debugger, make a call to the function.
          >

          This would work if I had simple scalars in the structure but there are
          arrays and strings as well as integers/floats. I just would like to
          know what are the names of the variables in the structure and perhaps
          the variable types.
          >
          This will work for anything, but the function you need to
          write simply has to account for the content of the structure.
          For example, you can do something like:
          >
          #include <stdio.h>
          #include <stdlib.h>
          >
          struct array_list {
          char *name;
          int *a;
          size_t size_a;
          struct array_list *next;
          };
          >
          void
          print_array(int *a, size_t s)
          {
          size_t i;
          for (i=0; i < s; i++)
          printf("%d ", a[i]);
          }
          >
          void
          print_array_lis t (struct array_list *d)
          {
          struct array_list *this;
          printf("%s = ", d->name);
          print_array( d->a, d->size_a);
          putchar('\n');
          for( this = d->next; this != NULL; this = this->next)
          print_array_lis t(this);
          }
          >
          void * Malloc(size_t size)
          {
          void *ret;
          ret = malloc(size);
          if (ret == NULL)
          exit (EXIT_FAILURE);
          return ret;
          }
          int
          main(void)
          {
          struct array_list a,b;
          int i;
          >
          a.name = "struct a";
          b.name = "struct b";
          a.size_a = 5;
          b.size_a = 3;
          a.a = Malloc(a.size_a * sizeof *a.a);
          b.a = Malloc(b.size_a * sizeof *b.a);
          for (i=0; i < a.size_a; i++)
          a.a[i] = 5 * i;
          for (i=0; i < b.size_a; i++)
          b.a[i] = -2 * i;
          a.next = &b;
          b.next = NULL;
          >
          print_array_lis t (&a);
          return EXIT_SUCCESS;
          }
          >
          This produces the output:
          $ ./a.out
          struct a = 0 5 10 15 20
          struct b = 0 -2 -4
          >
          --
          Bill Pursell
          You are too good, simply too good.

          Thanks,
          Will try this out.

          Comment

          Working...