Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Given a pointer,how does gdb get its type?

Tags:

c

gdb

How does gdb know the pointer points to a int or struct or any other data types?

like image 564
DriverBoy Avatar asked Dec 08 '25 12:12

DriverBoy


2 Answers

from: Examining the Symbol Table

whatis expr

Print the data type of expression expr. expr is not actually evaluated, and any side-effecting operations (such as assignments or function calls) inside it do not take place. See section Expressions.


ptype expr

ptype

Print a description of the type of expression expr. ptype differs from whatis by printing a detailed description, instead of just the name of the type. For example, for this variable declaration:

struct complex {double real; double imag;} v;

the two commands give this output:

(gdb) whatis v
 type = struct complex
(gdb) ptype v
 type = struct complex {
    double real;
    double imag;
 }
like image 122
herodot Avatar answered Dec 10 '25 01:12

herodot


gdb can't know, unless the pointer came from a variable or expression for which the type can be determined.

If gdb is given 0x4567789, it has no idea what that might point to. But if an int *p has that value, gdb can deference that and give you what that address contains.

like image 23
Richard Pennington Avatar answered Dec 10 '25 00:12

Richard Pennington