开发者

Need dynamically allocated (initialized) static variable

开发者 https://www.devze.com 2023-02-13 05:47 出处:网络
I need a static string(equivalent) variable in C and I\'m having troubles implementing s开发者_StackOverflow中文版omething like that.

I need a static string(equivalent) variable in C and I'm having troubles implementing s开发者_StackOverflow中文版omething like that.

I need to add content to that string every time the function is called. I tried with

static char *result = (char*)calloc(0, sizeof(char));

But that way I got:

error: initializer element is not constant

Which makes sense, but I really don't know how to do this differently (tried with global variable also, but no success).

Anyone can help?


static initializers must be constant, as your error message indicates.

Assign it NULL, then in some other function, test if it's NULL, allocate the resources it needs (some sensible default) and go from there. Ensure you do clean up after you're done, and if this is a threaded environment, I suggest you go a different route put this in other storage that you pass along wherever you need it.


If you need to increase array length, use realloc instead.


char *buf = NULL;

while(/* some loop here */){
    buf = realloc(buf, sizeof(/* new content */));
    ... // copy your new content to buf here
}

If you only add content, maybe better to use lists instead?


The most common way I've seen of doing this is to initialize a pointer to NULL, and store a length- optionally an end pointer is stored as well. When the function is called, check to see if you have enough space in your buffer available, and if not then realloc the memory chunk. Usually you don't want to realloc every time, and instead grow by either some fixed amount (usually selected based on the memory alignment of the system), or else to double the previous size (be sure to make sure you actually have enough free space!).

#define MYDATA_GROW_AMOUNT (12345) //exploit memory alignment on your system
...
/* if the compiler supports it, consider using __thread__ here */
static /* __thread__ */ char  *mydata     = NULL;
static /* __thread__ */ char  *mydata_end = NULL;
static /* __thread__ */ size_t mydata_len = 0;
...
/* gcc? you might want to use
       if(__builtin_expect((mydata_len < required_data_len),0))
   here instead */
if(mydata_len < required_data_len)
{
    mydata_end = mydata + mydata_len;
    mydata_len += MYDATA_GROW_AMOUNT;
    mydata = realloc(mydata,mydata_len);
}
...


I was able to get this to work for a "fixed" size array, i.e. the length can be defined during run-time, NOT compile time. I created a function which has the empty pointer itself A as an input, then alters it within and also returns it. The length len is custom run-time length:

char *malloc_char_array(char* &A, int len);

Then call that function in the array initialization:

static char *result = malloc_char_array(result, 50); //test length of 50

The static initialization only occurs the very first time the function is called, so it works great for arrays that need to be dynamic in size (i.e. not defined during compile time), yet still remain fixed throughout the entire run of the program.

And the allocating function would look something like:

char *malloc_char_array(char* &A, int len)
{
    A = (char*) malloc(len * sizeof(char));
    return A;
}

With this method there's no need to free the "static" allocated memory, just let it stay in the stack until the program quits.

0

精彩评论

暂无评论...
验证码 换一张
取 消