0

Suppose I have two arrays:

uint8_t buf1[10];
uint8_t buf2[20];

I now want to create an array of these arrays, e.g.,

uint8_t *buffers[] = {buf1, buf2};

When I look at the buffers variable in the debugger, however, I see that its two elements do not point to any elements of buf1 and buf2, but look like random values (maybe the contents of buf1/buf2?). Also, when dereferencing, e.g.,

uint8_t x = *(buffers[0] + 1);

the programs throws a bus error.

If I change the definition to

uint8_t *buffers[] = {&buf1[0], &buf2[0]};

everything is alright.

Why is there a difference? I always thought that array and &array[0] are equivalent?! What is the correct type of buffers to make definition 1 work?

EDIT: OK, I goofed up. There is in fact no difference between definition 1 and 2. I'm not really sure what change I did to see the correct values, but I do have a screenshot.

Anyway, I now know the answer to my question why the pointers are all bad, but you couldn't know because I stripped an important detail from the question.

I added __attribute__((section(".foo"))) to the definition of buffers, and that segment foo is uninitialized. Of course the values are random!

Sorry for wasting your time. :-)

5
  • I'm not sure about the issue. I tried the code on my side, and accessing buffers[x][y] was possible, which means it works (I didn't debug and look at the addresses though). However, I can tell you why your dereference test is wrong because buffers[0]+1 is just some random number, not an address. Commented Nov 17, 2019 at 14:42
  • @larsb: I guess they are equivalent and your first expression is fine. I run both under Visual Studio 2017 and 2008 to make sure. What compiler are you using? Commented Nov 17, 2019 at 14:47
  • Please show a full, shortest compilable code including all the #includes and int main() etc. that allows to reproduce the "bus error". Please specify the compiler and compiler options used and, if relevant, the target machine you are running this on. Please read how do you ask a good question on stackoverflow and helpful to me stackoverflow question checklist. I want to experience the same bus error on my computer too, what should I do? Commented Nov 17, 2019 at 14:52
  • uint8_t *buffers[] = {buf1, buf2}; is equivalent to uint8_t *buffers[] = {&buf1[0], &buf2[0]};. Commented Nov 17, 2019 at 14:58
  • @EmmanuelO : On the contrary (buffers[0] + 1) is equivalent to &(buf1[1]), so *(buffers1[0] + 1) is buf1[1]. Of course that may or may not be what larsb intended. Commented Nov 17, 2019 at 15:15

1 Answer 1

3

At onlinegdb the following code:

#include <stdint.h>

uint8_t buf1[10];
uint8_t buf2[20];
uint8_t* buffers1[] = {buf1, buf2};
uint8_t* buffers2[] = {&buf1[0], &buf2[0]};

int main()
{
    uint8_t x1 = *(buffers1[0] + 1);
    uint8_t x2 = *(buffers2[0] + 1);

    return 0;
}

at the return statements has the following state:

enter image description here

Which seems to do what you expect, but not what you describe. How does this test differ from yours?

Sign up to request clarification or add additional context in comments.

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.