Kleines Problem mit Speicher
-
Hallo,
ich bin etwas durcheinander, aber ich glaube es ist einfach nur ein kleiner Denkfehler.
Folgender Codestruct File { TCHAR Path[MAX_PATH]; TCHAR Filename[MAX_PATH]; struct Messdatum *md; int mdCount; }; int FileCount; void Init(void); bool GetFiles(TCHAR *path, struct File *files); int main(void) { TCHAR path[MAX_PATH] = _T(""); int korMax = 0; struct File *files = NULL; Init(); if( !GetFiles(path, files) ) // <-- Nach der Funktion ist files wieder NULL, warum??? return 1; _getch(); return 0; } void Init(void) { _tcsncpy_s( path, MAX_PATH, _T("D:\\C\\AAAProjekt\\Messung_260307"), MAX_PATH); FileCount = 0; } bool GetFiles(TCHAR *path, struct File *files) { HANDLE fHandleP, fHandleFile; WIN32_FIND_DATA wfdP, wfdFile; TCHAR aktPath[MAX_PATH+25] = _T(""); struct File *tempFiles = NULL; _stprintf_s(aktPath, MAX_PATH+25, _T("%s\\*"), path); tempFiles = malloc( sizeof( struct File) ); files = malloc( sizeof( struct File) ); ZeroMemory( &wfdP, sizeof(wfdP)); ZeroMemory( &wfdFile, sizeof(wfdFile)); fHandleP = FindFirstFile(aktPath, &wfdP); if( fHandleP == INVALID_HANDLE_VALUE ) { ErrorExit( _T("test") ); return false; } do { // Eintrag nur behandeln, wenn es nicht . oder .. ist (werden nur bei Unterverzeichnissen mit zurückgeliefert) // hier könnte man z.B. auch mit lstrcmp auf . und .. vergleichen, was allerdings nicht ganz so effizient ist if( !( (wfdP.cFileName[0]=='.') && ( (wfdP.cFileName[1]=='.' && wfdP.cFileName[2]==0) || wfdP.cFileName[1]==0 ) )) { if (wfdP.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { _stprintf_s(aktPath, MAX_PATH+25, _T("%s\\%s\\*.vel"), path, wfdP.cFileName); fHandleFile = FindFirstFile(aktPath, &wfdFile); do { if( !( (wfdFile.cFileName[0]=='.') && ( (wfdFile.cFileName[1]=='.' && wfdFile.cFileName[2]==0) || wfdFile.cFileName[1]==0 ) )) { if ( !(wfdFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ) { tempFiles = realloc(tempFiles, sizeof(struct File) * (FileCount) ); if( tempFiles == NULL ) { FindClose(fHandleFile); FindClose(fHandleP); return false; } memcpy(tempFiles, files, sizeof(struct File) * FileCount ); files = realloc(files, sizeof(struct File) * (FileCount + 1) ); if( files == NULL ) { FindClose(fHandleFile); FindClose(fHandleP); return false; } memcpy(files, tempFiles, sizeof(struct File) * FileCount ); _stprintf_s( files[FileCount].Path, MAX_PATH+25, _T("%s\\%s"), path, wfdP.cFileName); _tcsncpy_s( files[FileCount].Filename, MAX_PATH, wfdFile.cFileName, MAX_PATH); FileCount++; } } } while( FindNextFile(fHandleFile, &wfdFile)); FindClose(fHandleFile); } } } while (FindNextFile(fHandleP, &wfdP)); FindClose(fHandleP); free( tempFiles ); return true; }
Also ich rufe eine Funktion auf GetFiles und übergebe Ihr einen Zeiger auf meine Struktur. In der Funktion wird dafür Speicher angefordert und das läuft auch alles. Beim zurückkehren in main zeigt der Pointer (files) aber wieder auf NULL. Wieso verlier ich meinen Speicher? Ich hab doch ein Call by Ref. Wo ist mein Denkfehler
Danke
heppi
-
ansi?
-
du speicherst in der funktion GetFiles die von realloc gelieferten adressen lokal im parameter *struct File files.
in der funktion main bekommt *struct File files nicht viel davon mit.du kannst folgendes machen:
struct File *files GetFiles(TCHAR *path, struct File *files);
bool GetFiles(TCHAR *path, struct File **files);
*files = realloc(...struct File* files; // global
static struct File* files; // global und statisch
bool GetFiles(TCHAR *path);