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);
Saxtheowl
  • 4,136
  • 5
  • 23
  • 32
Joe Slater
  • 2,483
  • 6
  • 32
  • 49
  • 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. – Some programmer dude Oct 24 '19 at 10:26
  • 1
    I see nothing wrong with your approach (see Some programmer dude's comment). – Paul Ogilvie Oct 24 '19 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. – Some programmer dude Oct 24 '19 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? – Gerhardh Oct 24 '19 at 10:45
  • @Someprogrammerdude I dont need memory on heap in this case because it has no use outside the function. The size of buffer is only a few hundred bytes. Also, I heard its good practice to minimize the use of malloc – Joe Slater Oct 24 '19 at 10:55
  • @Gerhardh I am passing `buf` to a function which increments the pointer. I believe this does not work well with passing an array. – Joe Slater Oct 24 '19 at 11:00
  • Then `_buf` fulfills those needs. If defined inside a function it's an *automatic* variable whose life-time is inside the scope it's defined If the size if known at compile-time then the compiler will make sure there's space for it, otherwise space for it will be created at run-time. – Some programmer dude Oct 24 '19 at 11:01
  • As for the incrementing pointer issue, is the incremented pointer passed back from the function you call? How? If not then you don't have to worry about anything, as you won't be passing the array itself to any function, just a pointer (to its first element). Now would be a good time to edit your question to include a [mcve] of what you're doing, and how you use the array and/or pointer. And of course all the other details you mentioned in comments. – Some programmer dude Oct 24 '19 at 11:02
  • 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](https://stackoverflow.com/a/58539439/440558). – Some programmer dude Oct 24 '19 at 11:20
  • @Someprogrammerdude ok thanks for your help. Much appreciated. – Joe Slater Oct 24 '19 at 11:22

2 Answers2

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.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
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.

kiran Biradar
  • 12,700
  • 3
  • 19
  • 44
  • 2
    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](https://stackoverflow.com/questions/5407546/memory-allocated-with-alloca-gets-freed-at-end-of-function-or-at-end-of-scope). – Andrew Henle Oct 24 '19 at 10:28
  • @AndrewHenle I have included the side note. – kiran Biradar Oct 24 '19 at 10:29
  • @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). – Some programmer dude Oct 24 '19 at 11:11