cancel
Showing results for 
Search instead for 
Did you mean: 

Casting Void Pointer to structure changes data

stimpacker
Associate

{

static void* data;

func ->

structure* s = new structure();

// do stuff

data = s;

return;

 

 

Callback ->

structure* s = (structure*) data;

// Error

}

 

Functions are more or less setup as above. This setup is used in many locations on the code, works just fine. But for some reason in one spot, the casting overwrites data in the structure that had memory allocated to it.

The assembly for each cast is generated as

stimpacker_0-1728344544801.png

Which, I dont understand why a cast needs to str data. By chance perhaps, it doesnt corrupt data anywhere else. I still have plenty of memory left as well, so its not like ive ran out.

 

 

4 REPLIES 4
TDK
Guru
{
static void* data;

func ->
structure* s = new structure();
// do stuff
return;

Callback ->
structure* s = (structure*) data;
// Error
}

The local "s" variable in the function has no relation to the local "s" variable in the callback. The global "data" variable isn't accessed anywhere in the function.

It's unclear what result you're looking for here, but the code doesn't do anything useful.

 

The GCC compiler is robust, it's unlikely the be the culprit. More likely a bug in the code.

If you feel a post has answered your question, please click "Accept as Solution".
stimpacker
Associate

Sorry you feel like it doesnt do anything. But like I said, it works fine for the rest of my code and is called dozens of times before it hits this error. The pointer and data is all stored correctly and is able to be accessed. The error only specifically occurs with casting to a pointer of the correct structure type. 

> Sorry you feel like it doesnt do anything. But like I said, it works fine for the rest of my code

You have silently edited your post, so yes it does something different than when you originally posted it and when I made my comment.

TDK_0-1728349880300.png

TDK_1-1728349914777.png

 

data should be declared volatile, since it is used in the main thread and a callback, but probably not the issue.

 

The takeaway is the same: it's a code bug, not a compiler bug.

If something is being modified and you don't know where or why, create a hardware watchpoint which will break when the memory is modified. This will let you see exactly where it gets modified. Could be a stack overflow, could be issues with memory allocation (two variables with the same location), could be out of bounds write.

If you feel a post has answered your question, please click "Accept as Solution".
stimpacker
Associate

Ive already done that. The casting line is what changed the value of my structure. Hence why I included the assembly portion that is changing my structure data. 

 

>You have silently edited your post, so yes it does something different than when you originally posted it and when I made my comment.

What I edited had no relevance to my question. The code I wrote was only a general example of what's going on. I know specifically where the issue is, and its the 2nd line of assembly generated for each cast.