Reputation:
I have written a function that creates a data table, initializes it's elements to 0 and returns a pointer to the data table. However it doesn't work. Could someone explain it to me so I understand where my problem is. This is my code:
#include <stdio.h>
// Here I create the data table and initialize it's elements
int* tabinit(int size) {
int tab[size];
for (int i = 0; i < size; i++)
tab[i] = 0;
return tab;
}
int main() {
int* t = tabinit(10);
for (int i = 0; i < 10; i++)
printf("%d ", t[i]);
printf("\n");
return 0;
}
Upvotes: 1
Views: 77
Reputation: 73424
When a function terminates, then all its automatic variables go out of scope.
tab
is an automatic variable in this case, and will go out of scope when the function terminates. Now you are returning a pointer to a local array, so when the array goes out of scope, you will find yourself with a dangling pointer.
Didn't your compiler warned you about this? I got, in GCC, this warning:
warning: function returns address of local variable [-Wreturn-local-addr]
return tab;
^~~
First make sure that you really want for practicing to create the array in the function. If not, then you don't need to, you could just create in main, and then pass it to the function.
But, if you want to practice, then you need to dynamically allocate the array, if you really want it to stay alive after the function terminates. In that case, the array will free its memory, only when you tell it to (so never forget to free).
Example:
#include <stdio.h>
#include <stdlib.h>
int* tabinit(int size) {
int *tab = malloc(size * sizeof(int));
for (int i = 0; i < size; i++)
tab[i] = 0;
return tab;
}
int main() {
int* t = tabinit(10);
for (int i = 0; i < 10; i++)
printf("%d ", t[i]);
printf("\n");
// do NOT forget to free
free(t);
return 0;
}
Output:
0 0 0 0 0 0 0 0 0 0
PS: When you get more familiar with malloc()
, I suggest you read this: Should I check if malloc() was successful?
Upvotes: 2
Reputation: 4099
You should never return a pointer to a local object. After the function finishes, the local object gets deallocated and you're left with a dangling pointer (pointer that doesn't point to valid memory).
One way to do this would be to create your object in main and pass it into your function for init, like so:
void tabinit(int* tab, int size) {
for (int i = 0; i < size; i++)
tab[i] = 0;
}
int main() {
int t[10];
tabinit(t, 10);
for (int i = 0; i < 10; i++)
printf("%d ", t[i]);
printf("\n");
return 0;
}
Upvotes: 2
Reputation: 6414
Here:
// Here I create the data table and initialize it's elements
int* tabinit(int size) {
int tab[size]; // <------- here
for (int i = 0; i < size; i++)
tab[i] = 0;
return tab; // <------- also here
}
As gsamaras said, the content pointed by tab become out of scope after the function call. You can change this line:
int tab[size];
By:
int *tab;
tab = malloc(size * sizeof(int));
The content will be dynamically allocated, and then will remains after exiting from the function. However, you'll have to free it manually (free(t)
) afterwards or it will be reserved until the end of the program (which could occur a memory leak).
Upvotes: 1