2

I have a function which creates an array. This array I want to return. Originally I allocated space for the array using malloc, but now I'm starting to realize, it seems unnecessary to be using dynamic memory allocation, since I could just return a copy of the array. When should I use dynamic allocation (malloc) and when should I just stick to static?

* int createArray(some parameter, some parameter) {
int n = 0;
while(.....) {
n++;

}

* int newArray = (*int) malloc (sizeof(int) * n);

return newArray;

}

someArray = createArray(parameter, parameter);

EDIT: For example would this modified code be a valid alternative to the code above? Would it work as intended and return an array I can assign and use later in the program?

* int createArray(some parameter, some parameter) {
    int n = 0;
    while(.....) {
    n++;

    }

    int newArray[n];

    return newArray;

    }

    someArray = createArray(parameter, parameter);

I'm a freshman computer science student, thanks in advance!

  • You as the designer of the function need to decide which method is the best. There is no rule: use this or use that. For example I sometimes code on a system with as little as 4Kb of ram - I have no `malloc()` there at all (well, I have, but that would be a waste). – KamilCuk Feb 18 '19 at 20:47
  • Dynamic memory allocation will return a brand new array at every call. Static will always return the same array. It depends on your needs. – Alain Merigot Feb 18 '19 at 20:49
  • `malloc` is actually a pretty expensive operation (I believe it generates about 100 lines or so of assembly.) If you can get away with just using the stack (you can return a custom struct with an array and a `size_t` member that stores the length of the array) then that's the way to go IMO – Dustin Nieffenegger Feb 18 '19 at 20:49

2 Answers2

0

I have a hunch that OP meant to ask about automatic allocation instead of static allocation as the static keyword is not used in the example code. This answer is written from that perspective.

If your function is returning an array, you'd want to use dynamic memory allocation. If you use automatic memory allocation, your array will cease to exist after the function it was declared in exits. Unofficially you might still be able to access it, but as far as the C language is concerned it's fair game to be overwritten and isn't reliable or safe.

Generally, you'll want to use automatic memory allocation wherever possible because it's self managing (you can't forget to free it later and cause a memory leak). Some exceptions to this rule:

  • Memory needs to outlast the creating function
  • It isn't known how much memory will be needed at compile time
  • When you need a ton of memory (more than the stack can provide)
0

You should never use static for the reasons you're giving. Consider this code:

someArray = createArray(parameter, parameter);
someArray2 = createArray(parameter, parameter);

If you use static then both of these will point to the same memory, which is probably not what you want. Doing so is valid C code, but most coders would advice strongly against it. Furthermore, the name of the function starts with create, which is a very strong indication that you should use dynamic allocation.

In general, static is suitable when you need to preserve data in a function. Never return a pointer to static variables. That can cause hard tracked bugs.

A very simple example of where static variables can be useful is a simple counter.

int count() {
    static int c = 0;
    c++;
    return c;
}

A more complex example is a random number generator, where you need to preserve the previous number. You can find an example here.

klutt
  • 30,332
  • 17
  • 55
  • 95