Re: debugging
- From: "Sheldon" <shejo284@xxxxxxxxx>
- Date: 30 Oct 2006 12:46:11 -0800
Bill Pursell skrev:
Sheldon wrote:
Bill Pursell skrev:
Sheldon wrote:
I would like to view the contents of structure as part of a debugging<snip>
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.
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_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[i] = 5 * i;
for (i=0; i < b.size_a; i++)
b.a[i] = -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
--
Bill Pursell
You are too good, simply too good.
Thanks,
Will try this out.
.
- References:
- debugging
- From: Sheldon
- Re: debugging
- From: Bill Pursell
- Re: debugging
- From: Sheldon
- Re: debugging
- From: Bill Pursell
- debugging
- Prev by Date: Re: debugging
- Next by Date: Re: assembly in future C standard
- Previous by thread: Re: debugging
- Next by thread: Triangular Matrix
- Index(es):