1

Seems like I still didn't get the pointers in C right.

I want the length of the global array (pointer) j being dynamic.

I have this (Arduino) code

unsigned int* j;

void setup() {

  Serial.begin(9600);

  initj();

  Serial.println(j[0]); //111 -> right
  Serial.println(j[0]); //768 -> wrong!
  Serial.println(j[1]); //32771 -> wrong!
}

void initj() {
  unsigned int i[2];
  i[0] = 111;
  i[1] = 222;

  j = i;

  Serial.println(j[0]); // 111 -> right
  Serial.println(j[1]); // 222 -> right
}

void loop() {
}

How can I do this right?

Thank you in advance!

4
  • 2
    "I want the length of the global array (pointer) j being dynamic." (emphasis added) Arrays and pointers are not synonymous, and if you are thinking that they are you may be in trouble from the get-go. Yes, they share a lot of syntax and yes, arrays decay to pointer at the drop of a hat but they are different things. Commented Dec 11, 2011 at 18:10
  • 1
    You need to read about the difference between the heap and the stack. Especially what happens to stack-storage when you exit a function. Commented Dec 11, 2011 at 18:10
  • I suggest first learning to code a bit of C on your desktop, and once you learned enough, go to your Arduino (which implements a small variant of C or C++) Commented Dec 11, 2011 at 18:15
  • @BasileStarynkevitch coding on Arduino isn't that difficult (although I have to admit I have problems with pointers). Thank you anyway. Commented Dec 11, 2011 at 18:39

3 Answers 3

3

Your initj() function sets j to point at a local array. The lifetime of this array is limited to the function call itself, as soon as the function returns, the pointer is no longer valid. Attempting to dereference j is therefore undefined behaviour.

I don't know exactly what you want to do, but three possibilities are:

  1. Declare i at global scope instead.
  2. Declare i as static.
  3. Dynamically allocate the array using malloc (maybe unsuitable for an embedded platform).
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your help! I think there is no way to declare i global without defining it's length globally, right? Therefore I have to go for (2)!?
@Marcel: What difference does it make whether you define int i[2]; at global scope, or static int i[2]; inside the function?
this code is just an example. In my real problem the length of the array is derived from a function (it involves counting the digits of a (globally defined) string, etc.) How can I use the result from a function for the length of a globally defined variable?
3

This because the array i[2] is local to the initj() function. Therefore, once the function returns, it is no longer valid. So the pointer j becomes a dangling pointer.

So you have invoked undefined behavior.

As for why these two lines behave the way they do:

Serial.println(j[0]); //111 -> right
Serial.println(j[0]); //768 -> wrong!

Even though the values are lost, they still happen to be on the stack. So when you access it before you call Serial.println, you get the "right" value. But that function call ends up overwriting the stack. So on the second call, it gives the wrong value.

But in any case, it's still undefined behavior. Anything is allowed to happen.


To fix this, you need to put the values in a scope that is visible to the setup() function. You can either declare i[2] globally, or in setup() and pass it into the initj() function.

You can also dynamically allocate the array in heap memory with malloc(). (and be sure to free it later with free())

Comments

1

Once initj is done, its stack space is destroyed by the OS (or actually, the stackpointer is moved and initj's addresses are not reliable anymore). Because i exists in that stack space, i is gone. And ou just copied the value of i to j. So, in setup(), j is a dangling pointer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.