0

I'm trying to create a variable, char x[], inside a method and then return it.

I'm getting: address of stack memory associated with local variable 'x' returned

This is because the memory allocated to x will be freed when the method finishes execution. To prevent this, I could declare the variable as static, static char x[] but then

I'm getting: variable length array declaration cannot have 'static' storage duration

Because I'm using another variable's length when declaring my x array.

I'm new to C, can someone tell me what is the best practice here? What should I do?

  • You should use [`malloc()`](https://man7.org/linux/man-pages/man3/malloc.3.html) instead of `new` to allocate in C. – MikeCAT Feb 25 '21 at 13:19
  • @PaulR: This question is tagged C, but the first question you marked as a duplicate has two C++ answers first, neither of which will work in C, followed by two C answers that are inapplicable because they return pointers to static objects. The second question you marked as a duplicate is tagged C++ and not C. Further, it is a different question, and the answers there generally do not answer the question asked here. – Eric Postpischil Feb 25 '21 at 13:21
  • @EricPostpischil: yes, I couldn't see a question that covered all the bases, and a lot of the duplicate linking seems to be between C and C++ questions - I think there's enough in the above linked dupes to answer the OP's question though ? – Paul R Feb 25 '21 at 13:23
  • @PaulR: Where does any of the answers in the purported duplicates answer OP’s question about “What should I do?” by telling them to use `malloc`? – Eric Postpischil Feb 25 '21 at 13:24
  • I expect this is a duplicate, but neither of those was, so I have reopened. The question can be reclosed as a duplicate when appropriate originals are found. – Eric Postpischil Feb 25 '21 at 13:25

2 Answers2

0

Once you have the length figured out, say in some variable named length, allocate memory for the data you are going to return:

char *Data = malloc(length * sizeof *Data);
if (!Data)
{
    fputs("Error, failed to allocate memory.\n", stderr);
    exit(EXIT_FAILURE);
}

Then you can use Data as if it were an array of char. (sizeof *Data will be 1 in this case, but it is good practice to write memory allocations as NumberOfItems * sizeof *Pointer as this automatically matches the size request to the size of the objects being allocated, in case the types change in future program revisions.)

When you return memory in this way, the caller is responsible for releasing it, and this should be stated in documentation for the function that allocates memory. (Often, when a function allocates memory, a paired function is provided that deallocates the memory. This is trivial and may be omitted when only a simple free(pointer) is required. It is more apropos when complicated data structures are allocated in multiple parts, and the routine to free them has more work to do.)

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
0

One way it to use malloc:

char* get_str(void) {
    char* str = malloc(12 + 1); // +1 for the '\0' terminator
    strcpy(str, "Hello world!");
    return str;
}
void func(void) {
    char* str = get_str();
    // Be sure to free it when you're done:
    free(str);
}

Or you could create the array beforehand and pass it to the function:

void initalize_str(size_t len, char* str) {
    strncpy(str, "Hello world!", len);
}

void func(void) {
    char s[128];
    initalize_str(128, s);
}
cajomar
  • 428
  • 5
  • 9