-1

One of the issues I am having is attempting to cast variables into memory to be accessed at later points.

I have an example code here that works perfectly:

unsigned int *label = (unsigned int *)malloc(sizeof(unsigned int));
label = (unsigned int * ) 0xFFAAFFAA;

Anywhere else in my code I can access this value label, and it will be pointing to 0xFFAAFFAA as its value when I try to print it.

However, if I try to assign from a variable like such:

 //all of this is inside a method.. so any variables declared would be local
    unsigned int localVariable = 0xFFFFFFFF;
      unsigned int *label = (unsigned int *)malloc(sizeof(unsigned int));
      label = &localVariable;

The result will be something crazy like: 0x7f252d6b5f00 .. which I am just assuming is some random address in memory. I know this issue is because of a local variable as that operates within a function and is not global. But I can't figure out what the syntax of this would be...

The reason I want to define the local variable is because there is other logic in the function to add and subtract from that variable.. I left it out to keep it minimal

EDIT: so I could do something like this?

 unsigned int localVariable = 0xFFFFFFFF;
  unsigned int *label = (unsigned int *)malloc(sizeof(unsigned int));
  *label = localVariable;
Joe Caraccio
  • 1,899
  • 3
  • 24
  • 41
  • 2
    What are you trying to do here? `label = (unsigned int * ) 0xFFAAFFAA;` simply overwrites the pointer itself. – Oliver Charlesworth Apr 01 '17 at 20:11
  • @OliverCharlesworth Hi Oliver, darn. Basically All I am trying to do is create a new unsigned.. perform some logic on it such as subtracting and adding and then assign its value to a pointer – Joe Caraccio Apr 01 '17 at 20:13
  • because I can't seem to shift bits or do any sort of those operations on an unsigned int pointer.. so I need to do that on a normal value and then copy it – Joe Caraccio Apr 01 '17 at 20:13
  • I suppose you want `*label = (unsigned int*)0xFFAAFFAA;`. Notice `*` before `label`. – user7771338 Apr 01 '17 at 20:14
  • @FREE_AND_OPEN_SOURCE would there be any way to do that code, but instead of 0xFFAAFFAA I could pass in an already defined unsigned int variable.. my whole issue has been with the address of it being still local – Joe Caraccio Apr 01 '17 at 20:16
  • You might want to read [this question and its answers](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) about casting the returned value of `malloc` (and other functions returning a `void *`). – Some programmer dude Apr 01 '17 at 20:17
  • @JoeCaraccio. I don't understand what are you trying to do? You want to keep local variables' values in next function call? – user7771338 Apr 01 '17 at 20:17
  • 1
    With both examples you ***reassign*** `label`, thereby losing the original pointer returned by `malloc`. And with the second example you make the `label` variable point to a local variable, a variable that will go out of scope once the function returns and leaving you with an invalid pointer. – Some programmer dude Apr 01 '17 at 20:18
  • @Someprogrammerdude yeah.. thats my issue.. I am searching for a solution that will allow me to assign the pointer to the VALUE of the local variable.. so it won't go out of scope once the function ends.. – Joe Caraccio Apr 01 '17 at 20:20
  • 1
    So you want to copy the value of `localVariable`? Then just `*label = localVariable` should do that. Note the dereference. There's no way to make `localVariable` stay in scope, all you can do is copy its value. – Some programmer dude Apr 01 '17 at 20:22
  • 2
    This sounds like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What are you *actually* trying to do, and can you show some larger context? – dbush Apr 01 '17 at 20:23
  • @Someprogrammerdude Thats all I really care about is copying its value... Do you mean doing something like this, because I still have issues : unsigned int localVariable = 0xFFFFFFFF; unsigned int *label = (unsigned int *)malloc(sizeof(unsigned int)); *label = localVariable; – Joe Caraccio Apr 01 '17 at 20:26
  • edit: put it in formated code above.. – Joe Caraccio Apr 01 '17 at 20:26

1 Answers1

0

It sounds like what you want here is to create a pointer to a variable in a function then have it accessible outside of the function.

The issue with your first example is that you allocate memory and assign its address to label, but then you overwrite the value of label causing you to loose the reference to the memory you just allocated. You need to dereference the pointer using the * operator to write to the memory address you just allocated.

The second example won't work because you take the address of a local variable, then that variable goes out of scope when the function returns. So the address becomes invalid.

The below example allocates space, dereferences the pointer just allocated to write to the given address, then the address is returned to the caller.

unsigned int *func()
{
    // allocate space (don't cast return value of malloc)
    unsigned int *label = malloc(sizeof(unsigned int));
    // dereference pointer and write value to allocated memory
    *label = 0xFFAAFFAA;
    // pass pointer to allocated memory to the calling function
    return label;
}

Keep in mind that the calling function will be responsible for calling free on the memory that is returned when it is done using it to avoid a memory leak.

dbush
  • 205,898
  • 23
  • 218
  • 273