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!