Re: finding sizeof structure using gdb



On Jan 28, 2:54 pm, fnegroni <f.e.negr...@xxxxxxxxxxxxxx> wrote:
I think the command you are looking for is:
print _expr_
where _expr_ is the expression you want to evaluate at runtime.
You can place a breakpoint anywhere in your program where you want to
evaluate an expression, and type 'print' followed by the expression
such as 'sizeof (struct_type)'
To find out the offset of a member it necessitates some casts:

Say you have a struct defined as:

struct kelp_file {
FILE *f;
char *name;

};

and at some point an object of type struct kelp_file called k comes
into visibility.

Use gdb to print offset of name compared to start of structure:

print (size_t)&(k.name) - (size_t)(&k)

Thanks Guys, that was really helpful.
Can you please also tell me how to typecast a memory address to a
particular structure type and print the values of various membes of
that structure.
For eg. Say I have some "structure x" at address 0xffff1000
Now, I want to typecast this memory address 0xffff1000 with the
"structure x" and print the contents of all the members in "structure
x". Can it be done in GDB Or I have to dump the contents of memory and
manually interpret the value of each member ?
.



Relevant Pages

  • Re: traumatized by pointer casting
    ... >> When you cast the array by name to a pointer to struct, ... structure, and after the last member, just not before the first ... the entire structure a multiple of the alignment size. ... That is why the memory allocation functions malloc(), calloc, ...
    (comp.lang.c)
  • Re: Static C++ member accross POSIX shared memory?
    ... A class is placement new'd into a block of shared memory. ... connection id into the shared member. ... accessed from the class's member functions. ... Thats the bit I dont understand. ...
    (comp.unix.programmer)
  • Re: Static C++ member accross POSIX shared memory?
    ... A class is placement new'd into a block of shared memory. ... connection id into the shared member. ... static int NextUserId, ... But static member variables can't be put in the per-instance memory, ...
    (comp.unix.programmer)
  • Re: Clustered Indexes
    ... The only thing is they process a member ... > memory in SQL grows until there is little left for the Web Server. ... cluster on the PK. ... SQL Server uses ...
    (microsoft.public.sqlserver.server)
  • Re: Need help from Perl expert
    ... > I am new to Perl and currently trying to debug a Perl script. ... EXPR is true, setting each element of LIST to $_ in EXPR ... will be aliased to each element of @ports ... Loop through each member of @ports. ...
    (comp.lang.perl.misc)