allocate local variable in function

Is it possible to allocate a local variable in a function? Or what else could I do to make the following dumm code possible?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void func1(){
 int *a = (int *) malloc(1024 * sizeof(int)), i;
 func2(a);
 for(i = 0; a[i] != -1; i++){
  printf("%i\n", a[i]);
 }
}

void func2(int a[]){
 int i, x;
 x = computex();
 if(x > 1023){
  a = (int *) malloc((x + 1) * sizeof(int)); // naturally this doesnt' work since the address changes
 }
 for(i = 0; i < x; i++){
  a[i] = fillWithPositiveValue();
 }
 a[i] = -1;
}
you should pass a by address, not as an array
Thanks, I should have mentioned, that I already tried that, but since I got an error I was looking for something else.
I was too stupid too cast the address explicitly. Now it works.
1
2
3
if(x > 1023){
  a = (int *) malloc((x + 1) * sizeof(int)); // naturally this doesnt' work since the address changes
 }


I think that it doesn't work because the compiler sees a as an array rather than just a plain pointer. You can never change the address of an array after the array is declared.

1
2
int a[5] = {0};
a = new int[5]; // illegal! a isn't modifiable  


if you pass a to the function as a plain pointer you would be doing something like this.
1
2
3
4
5
6
7
8
9
int* a = new int[5];
func2(a);

// if func2 was declared/defined like so
void func2(int* a);
func2(a)
{
   a = new int[10]; // memory leak!  What happened to the array of 5 ints?
}  


Arrays are always passed as pointers even though the syntax may look like it is being passed by value. Moreover, the name of the array is never modifiable unless it was declared as a plain pointer and assigned an address.
Of course, you can always just int *func2();
@Helios
Your solution is in my case not possible. The above is just a dummy code for what I need to do.

And it is possible by using a double pointer. Something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void func1(){
 int *a = malloc(1024 * sizeof(int)), i;
 func2(&a);
 for(i = 0; a[i] != -1; i++){
  printf("%i\n", a[i]);
 }
}

void func2(int *a[]){
 int i, x;
 x = computex();
 if(x > 1023){
  *a = (int *) malloc((x + 1) * sizeof(int)); // naturally this doesnt' work since the address changes
 }
 for(i = 0; i < x; i++){
  (*a)[i] = fillWithPositiveValue();
 }
 (*a)[i] = -1;
}

You can also use an integer instead of a pointer to an integer, pass the address and explicitly cast it as a pointer to an integer and it works fine as well. I really was just much too tired for that...
It doesn't change that you still have a memory leak. Why are you allocating and assigning to a in both functions? The real program doesn't do that does it?
+1 to what kempofighter is saying.

You're never freeing your allocated buffers. And you really shouldn't split the task of allocation between multiple functions. Designate one area of code to do the allocation and cleanup. When you start splitting the task and putting it all over the place, you end up with all sorts of memory maintanance nightmares and code gets extremely difficult to manage.

KISS
The real programm does that and all memory is freed every time it is needed to be freed even though not in this dummy code. The print_r etc. is not how I use the allocated memory. It is part of an AI responsible for memory allocation for dynamically created functions.
Roughly speaking a is a container of pointers the functions which are automatically and dynamically created by func2. But it can't be summarized this easily since it's quite a bit of code already.
As I said I was just kind of tired and a little confused I guess I should go to bed now ;)
Thanks to everybody for your help.
KISS can't always be applicated eg in AIs, considering performance and automatization. At least I can't.
Last edited on
Topic archived. No new replies allowed.