2

What is the standard procedure to allocate memory on the stack. I am currently creating a temporary array

const uint8_t _buf[buf_size];
const uint8_t* buf = _buf;

I never use _buf again. I feel dirty. Is there a better way?

Edit:

The reason I need the pointer is because I am passing it to a function which increments the pointer. I dont believe this will work well with passing an array. Here are more details:

const uint8_t _buf[buf_size];
const uint8_t* buf = _buf;
buf_size = fread((void *)buf, 1, buf_size, file);
// f increments the pointer in buf
f(&buf, &buf_size);
10
  • 2
    Arrays naturally decays to pointers to their first elements. That's actually what happens when you use _buf as the source for the initialization of buf, it's equivalent to const uint8_t* buf = &_buf[0];. So you don't need buf only the array, if will behave as you expect when you need a pointer. Commented Oct 24, 2019 at 10:26
  • 1
    I see nothing wrong with your approach (see Some programmer dude's comment). Commented Oct 24, 2019 at 10:27
  • By the way, why the requirement of "allocate memory on the stack"? What problem is that supposed to solve? Because while local variables (including arrays) are commonly stored on the stack, nothing in the C specification mandates it. It only specifies the behavior of automatic (local non-static) variables. Commented Oct 24, 2019 at 10:37
  • "I never use _buf again" Why? What's wrong with _buf? The array allocates memory on the stack as you want. What's the reason to use another pointer? Commented Oct 24, 2019 at 10:45
  • 2
    Okay now it's clearer. I would personally keep the array and separate pointer variable (like you have it now), as it makes the code more explicit and therefore easier to read and understand what it's doing (IMO). Otherwise you could use compound literals as in Kamils answer. Commented Oct 24, 2019 at 11:20

2 Answers 2

4

You can use a compound literal:

uint8_t* buf = (uint8_t[buf_size]){0};

In many of my programs I use two variables anyway, because it's easier to track the memory with the debugger if it has a readable name.

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

Comments

3

You can use alloca read alloca

   const uint8_t* buf  = alloca(buf_size);

Basically it allocates size bytes of space in the stack frame of the caller.

Also I'm not saying using alloca is more standardized way but at least you get a chance to check the return value of alloca.

3 Comments

You can use alloca(), but a VLA such as const uint8_t _buf[buf_size]; is much better. alloc() in a loop can cause serious problems.
@AndrewHenle I have included the side note.
@JoeSlater alloca is almost never a good option, especially since C99 added variable-length arrays (which basically does the same thing, but in a safer and better way).

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.