Debugging C++ code - c++

While running some code through gdb , I cam across this structure definition :
ptype spawnStmt
type = struct stmt {
stmt *next;
const char *stmtname;
int lineNo;
const char *filename;
stmtType type;
stmt::<anonymous union> s;
} *(var *, stmtlist *)
(gdb) p spawnStmt
$3 = {stmt *(var *, stmtlist *)} 0x80514f8 <spawnStmt>
Can someone please explain to me what this means - {stmt *(var *, stmtlist *)} 0x80514f8 .
My intention is to print the values of the elements in this structure.
What would be the correct syntax for that ?
Thanks

Can someone please explain to me what this means
spawnStmt is a function at address 0x80514f8, taking var* and stmtlist* as parameters, and returning struct stmt.
My intention is to print the values of the elements in this structure.
There is no "this structure" yet. You have to set a break point on spawnStmt(), finish from it, and only then can you print values of the elements (finish will actually print them for you).

Related

Pointer help: dereferencing a pointer to a struct accessing data within it

I'm having some trouble wrapping my head around how a certain line of code works. For some reason it's just not clicking. This line of code is used generally with abstracting file handles in unix domain sockets.
Context:
typedef struct myStruct {
char charArray[10];
} myStruct;
myStruct myStructure;
myStruct *ptrToStruct = &myStructure;
/* This should change myStructure.charAarray[0] to equal a */
*(ptrToStruct.charArray) = 'a';
I understand that an array is essentially a pointer that is pointing to the first index in the array but the pointer has no data (charArray).
The reason this is so hard for me to understand is because the ptrToStruct is trying to access the pointer's data member charArray but the pointer has no data member charArray and then it's dereferencing it.
Is this sort of like (*ptrToStruct).(*charArray) = 'a'? But the dereferencing operator is being factored out? I apologize for being at all unclear.
UPDATE: The question has been answered. I was misreading code, the code was actually *(myStructure.charArray) and that's how it was altering the first index of the array. I should have also figured this out because as Sid explained pointers do not have the . operator.
ptrToStruct isn't a struct, so
ptrToStruct.charArray
should be
(*ptrToStruct).charArray
or
ptrToStruct->charArray
Then, yes, you can set the character using
*( ptrToStruct->charArray ) = 'a';
or
( ptrToStruct->charArray )[0] = 'a';
This is no different than
char charArray[10];
*charArray = 'a';
and
char charArray[10];
charArray[0] = 'a';

What does this mean *((int*)(&val) +1)

I'm trying to understand this line of code. Can someone help me? Is it saving the result in the variable val or in the address of the variable val?
*((int*)(&val) +1)= A*(y) + (B - C)
Thank you
&val take the address of val
(int*)(&val) consider this address as a pointer to int
(int*)(&val) +1 increment this address by 1 (times sizeof(int))
*((int*)(&val) +1) = ... assign the right hand side value at this incremented address
It is interpreting val as if it was an array of integers, and storing the result of the right hand expression in its second element. To understand exactly the point of it all you should provide some more context (my guess: is it manipulating the raw content of double values?)
Notice that, depending on the type of val, this may be undefined behavior due to strict aliasing rules.
Divide expression *((int*)(&val) +1) into smaller ones to understand it:
take address of val (&val) and treat it as a pointer to an int (int *)
add 1 +1 to this pointer which means 'move pointer to next int' as it was an array of ints.
finaly by combining * and = apply right hand side expression to int pointed by pointer.
I hope others have answered your question. Adding to what others have said, the same code can be written as follows:
(int*)(&val)[1]= A*(y) + (B - C)
where (int*) will type cast &val as an implicit pointer to an integer which points to the address of val and [1] indicates the first integer location ahead of the location where val is stored.
This is how arrays are interpreted. Say you have an array
int a[10];
For this array, 'a' is a pointer which points to the base address ( address of the element a[0] ), and a[i] is nothing but *(a+i), i.e. the element which is i locations ahead of the first element of the array.
This is not correct code and you should never use it
Imagine this class:
class A {
int number = 10;
public:
void print(){ std::cout << number; }
};
The int number is private for the access not the use!
So how can we access this private int.
Simply:
A obj;
*( (int*) ( &obj ) ) = 100;
obj.print();
output
100
demo
Now if you would have more than one data then how to access?
by this syntax:
*((int*)(&val) +1)
It says:
find the address of the first data,
one index go ahead,
cast it to the int*,
then dereference it,
then initialize it

Can gdb print 64-bit address?

I entered the following command in gdb:
(gdb) p PyObject_GetAttrString($2, "_other_obj_m")
and got the following output which should be a variable address type (PyObject*).
$4 = -246881136
Then I used printf to convert it to hex format and got a wrong address "0xf148e490", the real address is "0x4f78f148e490".
Any one know how to solve this problem?
If your target program doesn't have any debugging info for a function, gdb will assume that the function has return type int. On x86_64 Linux, that's likely to be 4 bytes.
$ gdb -q python2.7
(gdb) ptype PyObject_GetAttrString
type = int ()
(gdb) p sizeof(int)
$1 = 4
If your target has debugging info, you'll get the correct result.
$ gdb -q python2.7-dbg
Reading symbols from python2.7-dbg...done.
(gdb) ptype PyObject_GetAttrString
type = struct _object {
struct _object *_ob_next;
struct _object *_ob_prev;
Py_ssize_t ob_refcnt;
struct _typeobject *ob_type;
} *(PyObject *, const char *)
If you don't have any debugging info, you can cast the function symbol to the correct type signature, or to one that's close enough, such as pointer to function returning void *. Try this:
(gdb) p ((void * (*)(void *, char *))PyObject_GetAttrString)($2, "_other_obj_m")

Can strcmp() Compare a Dynamic Character Array with a Static Character array?

Perhaps this is a stupid question, but the reason I ask is because I'm using strcmp() to compare a dynamic array with a static array and it's seemingly giving the wrong result. The arrays should be equal, but strcmp() is returning false. When I pause my program, the only two things that differ between those arrays are their memory locations.
I can't show you the whole code, but here's part of my function which has these two variables in it:
stock * Table::retrieveStock(const char tick[])
{
uint8_t index = hashing(tick);
if (table[index])
{
bool test = strcmp(table[index]->data->getTick(), tick);
//irrelevant code
and here's how some of the pointers are declared in the Table class:
struct node
{
stock * data;
node * next;
node()
{
stock();
next = nullptr;
}
node(stock *& item)
{
data = item;
next = nullptr;
}
};
node ** table;
I have an array of class objects in it, anyways, the point is that I'm comparing two tick values, which should be equal. One I'm pulling out of the object with the getTick() function:
const char * stock::getTick() const
{
return tick;
}
and one is passed into the function: const char tick[]. I'm quite clueless as to what is wrong with my code here. I would love to give you guys more code to work with, but I can't see how it would do anything but make solving this issue more difficult for you.
Edit: Maybe this will help; I just thought of it. This is exactly what my compiler says the values are:
curr->data 0x005d93e8 {tick=0x005d9f38 "TSLA" name=0x005da100 "2" value=2.00000000 ...} stock *
and
tick 0x004afb20 "TSLA" const char *
strcmp() returns 0 for a match (and non-zero for no match). It doesn't return true or false as you are expecting.

How to store a pointer to a position inside char * buffer, sending to a function, retrieveing a value?

I have a char * buffer in which I store any kind of values doing casting. Every time I store a float I add 'len of var' to 'my_position' var.
I need a way to access to the data from another point on my app.
Ok, buffer+position gives me the pointer to memory. How can I return it ? As an int ?
I think I have to return a char * pointer. (pointer_char)
Ok, I have now this function :
create_float(char * pointer_char) {
float * xx = reinterpret_cast<float*> (pointer_char);
}
I have a crash ....
Any help and suggestion would be appreciated ... Thanks
I don't think that I understood your question completely, but if you simply need a function that reads and returns a float value from a char* buffer, you can go with this:
float create_float(char* ptr)
{
return *(reinterpret_cast<float*>(ptr));
}
Also, it's difficult to tell the reason of the crash without some context. Can you post the code here?