-3

I have problem with pointers. In program I put all words in array foldersName[]. All words in array are OK, when I print them, but I want to return array of pointers, for each word in array one pointer. My method is:

char** getTokens(char * path){
.
.//Getting tokens in array foldersName[];
.char foldersName[count][255];
.
char * tokens[actualCountOfFolders]; //How much folders in foldersName
int i;
for(i=0;i<count;i++){
    tokens[i] = foldersName[i];
    printf("Folders pointer %s \n",tokens[i]);
  }
return tokens;

For example I have foldersName = {"C","Game","Halo 4","Map"}. But if I printf tokens[i], like I did in for loop, it prints this {"C","Game","Halo 4","Map?"}. How to fix it?

And can I do this, after calling function, in next code?

char ** tokens =(char **) malloc(sizeof(char)*actualCountOfFolders);
    tokens  = getTokens(path);
    int i = 0;
     for(i =0;i<actualCountOfFolders;i++){
     printf("Folders %s \n",tokens[i]);
  }
ad absurdum
  • 19,498
  • 5
  • 37
  • 60
Michal Fiala
  • 75
  • 2
  • 9
  • You should not return a pointer to a automatic variable that will be cleaned up once the function ends. – Unimportant Jan 06 '17 at 13:20
  • 1
    Please see this discussion http://stackoverflow.com/questions/11656532/returning-an-array-using-c – Selçuk Cihan Jan 06 '17 at 13:20
  • You `malloc` some space for `tokens` but then by doing `tokens = getTokens(path);` you loose the access to the memory allocated. – Missu Jan 06 '17 at 13:21
  • Unrelated to your question, but you might want to read [this discussion about casting the result of `malloc`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Some programmer dude Jan 06 '17 at 13:21
  • `tokens` is local to `getTokens()` so ceases to exist when the function returns. Returning it therefore causes the caller, if it uses the return value, to have undefined behaviour. Independently of that, what you're describing the caller as doing is also invalid. Pointer assignments do not copy whole arrays. – Peter Jan 06 '17 at 13:23

2 Answers2

1

The problem is that you return a pointer to a local variable. When the function getTokens returns, all its variables goes out of scope and will no longer exist. That goes for the array tokens as well.

That you allocate memory before the call doesn't matter, because you overwrite (reassign) the pointer returned by malloc with the pointer returned by getTokens, leading to a memory leak. Oh and that malloc doesn't allocate the correct amount anyway, since it only allocate actualCountOfFolders bytes (characters), not actualCountOfFolders number of pointers to characters.

The two typical solutions are to either allocate dynamically inside the getTokens function, or to pass the array as an argument to the function.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

Never return the address of local variable created inside the function as output of that function.

Some people will say because a clean up will be done , well it not exactly what going to happen. In fact once you create your buffer inside the the function the start address of this buffer will declared inside the stack below to that function so your entire buffer will be stored inside the the function stack, once you are done with function and a return from it the data still exist in the stack address but the range of address is no more below to the function stack because we are done from the function call so if another function is called the program will reserve this range of address for the new call or for the new declared variable as result the buffers values will be overwritten and nothing will prevent this write , this is exactly what happen for local buffer or variable declared inside the function.

The second code is wrong, in fact by using malloc you allocate memory in the heap and once you call the line tokens = getTokens(path); you overwrite the heap address with the address returned by the function and this will result in memory leakage because we loose the pointer to the allocated memory in the heap without mentioning of course the problem of loosing data stored in the function stack when we are out of scope of the function.

dhokar.w
  • 386
  • 3
  • 6
  • yes i mean the address of the local variable ( which is the pointer to this local variable) not the value of the local variable . thank you – dhokar.w Jan 06 '17 at 14:46