3

Is there a way to return a new array allocated with the static keyword after each invocation of a function? I can create a new array if i make a clone to the function, but not from the same function.

Consider the following program:

#include <stdio.h>

char *CrArray1(void);
char *CrArray2(void);

int main(void)
{
    char *p1 = CrArray1();
    strcpy(p1, "Hello, ");

    char *p2 = CrArray1();
    strcat(p2, "World");

    char *q1 = CrArray2();
    strcpy(q1, "Different String");

    printf("p1 is : %s\n", p1);
    printf("q1 is : %s\n", q1);

    return 0;
}

char *CrArray1(void)
{
    static char Array[128];
    return Array;
}

char *CrArray2(void)
{
    static char Array[128];
    return Array;
}
machine_1
  • 4,266
  • 2
  • 21
  • 42
  • 3
    Yes, returning of local static object address is OK. But it is the same object for each call to the same function. And different if functions are different. – Sergio Jul 20 '16 at 10:46
  • 1
    @Serhio That's not what question is about. OP wants to return **new** array from **each invocation of a function** – mvidelgauz Jul 20 '16 at 10:48
  • 2
    No, there isn't. Objects with static storage are placed into a special memory section by the compiler. So they must all be known at build time. – StoryTeller - Unslander Monica Jul 20 '16 at 10:50
  • @machine_1 Can you please describe your **actual** problem? Why do you want those arrays to be static? Short answer is no, static memory is not allocated at run time – mvidelgauz Jul 20 '16 at 10:50
  • 1
    You don't get a **new** array for each invocation of the function. That is absolutely not what `static` does. In contrary `static` ensures it is the same array and that it exists for the whole program execution. So the answer is no. – Support Ukraine Jul 20 '16 at 11:09
  • @StoryTeller Is memory reserved for them even if i don't call the function ? – machine_1 Jul 20 '16 at 11:35
  • @machine_1, of course. Although it won't be initialized until you do enter. – StoryTeller - Unslander Monica Jul 20 '16 at 11:37
  • And if you are going the good path of dynamic allocations, please have a designated `CrArray1Release` function, even if it only calls `free`. It's good design. – StoryTeller - Unslander Monica Jul 20 '16 at 11:39

3 Answers3

1

No, static objects by definition have only one instance.

You'll need to use malloc() and callers of your function will need to free() the memory.

Kornel
  • 97,764
  • 37
  • 219
  • 309
1

If at compile time you know how many times you are going to call the function then following can be used:

#define NUMBER_OF_TIMES_FUNCTION_WILL_BE_CALLED 10
char *CrArray1(void)
{
    static int i = -1;
    static char Array[NUMBER_OF_TIMES_FUNCTION_WILL_BE_CALLED][128];
    ++i;
    return Array[i];
}

Note: NUMBER_OF_TIMES_FUNCTION_WILL_BE_CALLED has to be a reasonable number.

sameerkn
  • 2,209
  • 1
  • 12
  • 13
0

No, we can't.

wikipedia:

static is used to store the variable in the statically allocated memory instead of the automatically allocated memory.

so if you add after q1:

char *q2 = CrArray2();
strcpy(q2, "Another String");

and print q2, you will get:

p1 is : Hello, World q1 is : Another String q2 is : Another String

That means the local static variable still points to the same memory. So the result is not we want.

But if you use the malloc to require a new memory in function. Each time the pointer that function returned will point to different memory. so there is no influence of those variables.

More about static you can refer what does static means in a C program

Community
  • 1
  • 1
Jesse Chen
  • 469
  • 5
  • 7