0

We have an array of structs like this one:

struct allocation
{
  size_t alloc_size_;

  char* alloc_memory_;
};

static struct allocation allocations[] =
  {{1024, NULL},{2048, NULL},};

later on in main() it's members alloc_memory_ are initialized using numa_alloc_onnode().

So the question: is alloc_memory_ also static and where they are located (heap, stack) ? If they are not static then how to make them static?

mah
  • 39,056
  • 9
  • 76
  • 93
Dimon
  • 436
  • 5
  • 15

1 Answers1

2

The alloc_memory_ member of array allocations are static, but the memory the pointed to are not necessarily static.

In your case, since you allocated them with numa_alloc_onnode in main, this means they pointed to dynamic storage.

If you really want static storage too, you can define the memory before the structure:

static char buffer1[1024];
static char buffer2[2048];

static struct allocation allocations[] = 
{ {1024, buffer1}, {2048, buffer2} };
fluter
  • 13,238
  • 8
  • 62
  • 100
  • Ok thanks. In this case I would need a macro that would generate code type `static char bufferN[SIZE];`. Consider that `static struct allocation allocations[]` above is initialized as `static struct allocation allocations[] = ALLOCATIONS;`, where `ALLOCATIONS` is defined at compile time by user like `-D'ALLOCATIONS={ {1024, buffer1}, {2048, buffer2} }'` So the macro would have to parse `{ {1024, buffer1}, {2048, buffer2} }` and generate code: `static char buffer1[1024]; static char buffer2[2048];`. Could you please point me to such a macro? Thanks – Dimon Apr 20 '16 at 16:26
  • I suggest not passing code through macros, you could pass the size of the buffer though, static `char buffer[N};` and `-DN=1024` – fluter Apr 21 '16 at 00:37