debugging

S

Sheldon

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("Allocating memory!\n");
hdfdata.latitude = calloc(ROW,sizeof(float *));
if(hdfdata.latitude == NULL) {
fprintf(stderr, "Failed to allocate memory\n");
return EXIT_FAILURE;
}
for (i = 0; i < 15; i++) {
hdfdata.latitude = calloc(15,sizeof(float));
if(hdfdata.latitude == NULL) {
fprintf(stderr, "Failed to allocate memory\n");
return EXIT_FAILURE;
}
}
/* Reading the hdf data into the arrays */
stat = readHDF(hdfdata.latitude,latfile,"/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
 
B

Bill Pursell

Sheldon said:
I would like to view the contents of structure as part of a debugging
routine.
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.
 
S

Sheldon

Bill Pursell skrev:
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.

/Sheldon
 
B

Bill Pursell

Sheldon said:
Bill Pursell skrev:
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);
}

void
print_array_list (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_list(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 = 5 * i;
for (i=0; i < b.size_a; i++)
b.a = -2 * i;
a.next = &b;
b.next = NULL;

print_array_list (&a);
return EXIT_SUCCESS;
}

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

Sheldon

Bill Pursell skrev:
Sheldon said:
Bill Pursell skrev:
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);
}

void
print_array_list (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_list(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 = 5 * i;
for (i=0; i < b.size_a; i++)
b.a = -2 * i;
a.next = &b;
b.next = NULL;

print_array_list (&a);
return EXIT_SUCCESS;
}

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


You are too good, simply too good.

Thanks,
Will try this out.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top