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;
}
8
  • 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. Commented Jul 20, 2016 at 10:46
  • 1
    @Serhio That's not what question is about. OP wants to return new array from each invocation of a function Commented Jul 20, 2016 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. Commented Jul 20, 2016 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 Commented Jul 20, 2016 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. Commented Jul 20, 2016 at 11:09

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.