c - Return pointer of file/folder struct in function -


i'm trying create function returns array of pointers structs weird behavior when returning array. struct:

struct dlls{     char* foldername = (char*)(malloc(sizeof(char*)*longextfilename));     char* cppfilename = (char*)(malloc(sizeof(char*)*longextfilename)); }; 

and function. function opens given path folder , returns it's subfolders contained in it. example: "c:\" return array of x size, array[0]->foldername = "amd", array[1] = "a_files"... etc. according computer , folder contained in c:*.

dlls** getfiles(char* dirpath, char* extension){      dlls* arr[1]; //null struct in case there wrong folders in given path     arr[0] = (dlls*)malloc(sizeof(dlls*)*16);     arr[0]->foldername = "null";     arr[0]->cppfilename = "null";      char* path = (char*)(malloc(sizeof(char*)*longextfilename));     path[0] = 0;      strcat(path, dirpath);     strcat(path, extension); //this modifies given path search winapi function                              //path changed search files by: path\extension, extension given extensión \.cpp -> c:\*.cpp                              //if extensions given user "\\*" search folders      int numfiles; //number of folders found     void* rootpath;     win32_find_data fileinformation;       if ((rootpath = findfirstfile((path), &fileinformation)) == invalid_handle_value)                return arr; //wrong folder path given      else numfiles = 0;      {         if(fileinformation.dwfileattributes & file_attribute_directory){             numfiles++;         }      } while (findnextfile(rootpath, &fileinformation));      findclose(rootpath);      if(!(numfiles-2)) return arr; //if not folders found, skipping "." , ".." folders, return null struct.      dlls* array[numfiles - 2]; //create array of pointers of structs of number of found folders     int = -1;      rootpath = findfirstfile(path, &fileinformation);     findnextfile(rootpath, &fileinformation);     findnextfile(rootpath, &fileinformation); //skip "." , ".." folders      {         if(fileinformation.dwfileattributes & file_attribute_directory){             array[i] = (dlls*)malloc(sizeof(dlls*)*256); //allocate struct memory             array[i]->foldername = fileinformation.cfilename;             printf("%s\n", array[i]->foldername);                     //when printing names in process, shows folders names correctly in console.      }      } while (++i, findnextfile(rootpath, &fileinformation));      findclose(rootpath);      //weird behavior starts here:     printf("\n\nfolders saved:\n\n");     for(i = 0; i<numfiles-2; i++)     printf("%s\n", array[i]->foldername);        return array;    }; 

when print struct information saved @ time folder found, prints fine, when folder finding cycle ends , make show struct array information 1 one, array weird mess, crashes or prints first folder name correctly , rest weird symbols.

it's not permitted have = in struct definition. don't know trying anyway. struct definition looks like:

struct dlls {     char *foldername;     char *cppfilename; }; 

later on can declare variables of type , make variable's pointers point somewhere.


this hopeless:

dlls* array[numfiles - 2]; // ... return array; 

because returning pointer local variable. array destroyed when function returns. make array persist longer function declared in, must either make static, or use dynamic allocation.

even if fix this, bug is:

array[i]->foldername = fileinformation.cfilename; 

the fileinformation structure local function too, once function exits, pointer pointing invalid memory.

to fix use dynamic allocation again, although must consistent , dynamically allocate foldernames can free them later.

it important think things in memory, how long allocated for, , pointers pointing. may draw memory map on piece of paoer can see variables , what's pointing @ what.


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -