Skip to main content
Visitor II
October 7, 2024
Question

Casting Void Pointer to structure changes data

  • October 7, 2024
  • 3 replies
  • 1968 views

{

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.

 

 

    This topic has been closed for replies.

    3 replies

    Super User
    October 8, 2024
    {
    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.

    Visitor II
    October 8, 2024

    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. 

    Super User
    October 8, 2024

    > 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.

    Visitor II
    October 8, 2024

    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.