Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Free fails with access violation

Free fails with access violation

Scheduled Pinned Locked Moved C / C++ / MFC
performance
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tejaswini_g
    wrote on last edited by
    #1

    Reposting as thread has grown #include <> // remove extra angle bracket for compilation #include <> #include <> #include <> void FreeMemory(char** myArray, int row); void FileCountInDir(const TCHAR* dirpath, int * filescount, int* filesize); void FindFilesInDirectory(const TCHAR* dirpath, char** filelist); int _tmain(int argc, TCHAR *argv[]) { int i =0; TCHAR szDirpath[MAX_PATH]; //TCHAR Filelist[100][MAX_PATH]; char ** Filelist; int iFileCountInFolder=0; int iFileSize = 0; _tcscpy(szDirpath,_T("C:\\BMPread_W\\TransitionEffects\\Images")); FileCountInDir(szDirpath,&iFileCountInFolder,&iFileSize); Filelist = (char**)malloc(iFileCountInFolder * sizeof(char *)); if(Filelist == NULL) { fprintf(stderr, "out of memory\n"); return 0; } for(i = 0; i < iFileCountInFolder; i++) { Filelist[i] = (char*)malloc((iFileSize * sizeof(char))+1); if(Filelist[i] == NULL) { fprintf(stderr, "out of memory\n"); return 0; } } FindFilesInDirectory(szDirpath, Filelist); for(i =0;i 0)|| ((*filesize) < _tcslen(ffd.cFileName))) { (*filesize) = _tcslen(ffd.cFileName); } } } while (FindNextFile(hFind, &ffd) != 0); FindClose(hFind); } void FreeMemory(char** myArray, int row) { int i; if(myArray != NULL) { for (i = 0; i < row; i++){

    free (myArray[i]);

    } }

    L Richard Andrew x64R L 4 Replies Last reply
    0
    • T tejaswini_g

      Reposting as thread has grown #include <> // remove extra angle bracket for compilation #include <> #include <> #include <> void FreeMemory(char** myArray, int row); void FileCountInDir(const TCHAR* dirpath, int * filescount, int* filesize); void FindFilesInDirectory(const TCHAR* dirpath, char** filelist); int _tmain(int argc, TCHAR *argv[]) { int i =0; TCHAR szDirpath[MAX_PATH]; //TCHAR Filelist[100][MAX_PATH]; char ** Filelist; int iFileCountInFolder=0; int iFileSize = 0; _tcscpy(szDirpath,_T("C:\\BMPread_W\\TransitionEffects\\Images")); FileCountInDir(szDirpath,&iFileCountInFolder,&iFileSize); Filelist = (char**)malloc(iFileCountInFolder * sizeof(char *)); if(Filelist == NULL) { fprintf(stderr, "out of memory\n"); return 0; } for(i = 0; i < iFileCountInFolder; i++) { Filelist[i] = (char*)malloc((iFileSize * sizeof(char))+1); if(Filelist[i] == NULL) { fprintf(stderr, "out of memory\n"); return 0; } } FindFilesInDirectory(szDirpath, Filelist); for(i =0;i 0)|| ((*filesize) < _tcslen(ffd.cFileName))) { (*filesize) = _tcslen(ffd.cFileName); } } } while (FindNextFile(hFind, &ffd) != 0); FindClose(hFind); } void FreeMemory(char** myArray, int row) { int i; if(myArray != NULL) { for (i = 0; i < row; i++){

      free (myArray[i]);

      } }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Please edit your question and put <pre> tags around it all, not just the last line; i.e the opening tag should be before the first line, and the closing tag after the last. How does this even compile, since szDirpath is not declared in your code?

      _tcscpy(szDirpath,_T("C:\\BMPread_W\\TransitionEffects\\Images"));

      FileCountInDir(szDirpath,&iFileCountInFolder,&iFileSize);

      You could also simplify it to:

      FileCountInDir(_T("C:\\BMPread_W\\TransitionEffects\\Images"), &iFileCountInFolder, &iFileSize);

      As to the rest, it seems to be missing major portions of code so it's difficult to tell what is happening.

      1 Reply Last reply
      0
      • T tejaswini_g

        Reposting as thread has grown #include <> // remove extra angle bracket for compilation #include <> #include <> #include <> void FreeMemory(char** myArray, int row); void FileCountInDir(const TCHAR* dirpath, int * filescount, int* filesize); void FindFilesInDirectory(const TCHAR* dirpath, char** filelist); int _tmain(int argc, TCHAR *argv[]) { int i =0; TCHAR szDirpath[MAX_PATH]; //TCHAR Filelist[100][MAX_PATH]; char ** Filelist; int iFileCountInFolder=0; int iFileSize = 0; _tcscpy(szDirpath,_T("C:\\BMPread_W\\TransitionEffects\\Images")); FileCountInDir(szDirpath,&iFileCountInFolder,&iFileSize); Filelist = (char**)malloc(iFileCountInFolder * sizeof(char *)); if(Filelist == NULL) { fprintf(stderr, "out of memory\n"); return 0; } for(i = 0; i < iFileCountInFolder; i++) { Filelist[i] = (char*)malloc((iFileSize * sizeof(char))+1); if(Filelist[i] == NULL) { fprintf(stderr, "out of memory\n"); return 0; } } FindFilesInDirectory(szDirpath, Filelist); for(i =0;i 0)|| ((*filesize) < _tcslen(ffd.cFileName))) { (*filesize) = _tcslen(ffd.cFileName); } } } while (FindNextFile(hFind, &ffd) != 0); FindClose(hFind); } void FreeMemory(char** myArray, int row) { int i; if(myArray != NULL) { for (i = 0; i < row; i++){

        free (myArray[i]);

        } }

        Richard Andrew x64R Offline
        Richard Andrew x64R Offline
        Richard Andrew x64
        wrote on last edited by
        #3

        Have you stepped through the code and inspected the values of myArray and i? That might tell you why it's failing.

        The difficult we do right away... ...the impossible takes slightly longer.

        1 Reply Last reply
        0
        • T tejaswini_g

          Reposting as thread has grown #include <> // remove extra angle bracket for compilation #include <> #include <> #include <> void FreeMemory(char** myArray, int row); void FileCountInDir(const TCHAR* dirpath, int * filescount, int* filesize); void FindFilesInDirectory(const TCHAR* dirpath, char** filelist); int _tmain(int argc, TCHAR *argv[]) { int i =0; TCHAR szDirpath[MAX_PATH]; //TCHAR Filelist[100][MAX_PATH]; char ** Filelist; int iFileCountInFolder=0; int iFileSize = 0; _tcscpy(szDirpath,_T("C:\\BMPread_W\\TransitionEffects\\Images")); FileCountInDir(szDirpath,&iFileCountInFolder,&iFileSize); Filelist = (char**)malloc(iFileCountInFolder * sizeof(char *)); if(Filelist == NULL) { fprintf(stderr, "out of memory\n"); return 0; } for(i = 0; i < iFileCountInFolder; i++) { Filelist[i] = (char*)malloc((iFileSize * sizeof(char))+1); if(Filelist[i] == NULL) { fprintf(stderr, "out of memory\n"); return 0; } } FindFilesInDirectory(szDirpath, Filelist); for(i =0;i 0)|| ((*filesize) < _tcslen(ffd.cFileName))) { (*filesize) = _tcslen(ffd.cFileName); } } } while (FindNextFile(hFind, &ffd) != 0); FindClose(hFind); } void FreeMemory(char** myArray, int row) { int i; if(myArray != NULL) { for (i = 0; i < row; i++){

          free (myArray[i]);

          } }

          L Offline
          L Offline
          leon de boer
          wrote on last edited by
          #4

          Okay that rubbish won't even compile No compiler is going to let this line thru

          for(i =0;i 0)|| ((*filesize) < _tcslen(ffd.cFileName)))

          I am also suggesting that this line was also supposed to be that function defined but it has ";" rather than open brackets

          FindFilesInDirectory(szDirpath, Filelist);

          The other function is not provided anywhere

          void FileCountInDir(const TCHAR* dirpath, int * filescount, int* filesize);

          Can I suggest you give us some code that actually compiles if you want help There is also some house cleaning you didn't release the pointer array in FreeMemory. To make you life easier can I suggest you make a CreateMemory function to add to your FreeMemory function here is what they look like

          char** CreateMemory(int rows, int stringsize) {
          int i;
          char** p;

          p = (char\*\*) malloc (rows \* sizeof (char\*));
          if(p != NULL){
              for (i = 0; i < rows; i++){
                  p\[i\] = (char\*) malloc(stringsize \* sizeof(char));
              }
          };
          return (p);
          

          };

          void FreeMemory(char** myArray, int row) {
          int i;
          if(myArray != NULL){
          for (i = 0; i < row; i++){
          free (myArray[i]);
          }
          };
          free(myArray); // you forgot this
          };

          This is how you use them

          char** q;

          q = CreateMemory(10, 256); // create 10 rows each 256 chars
          strcpy(q[0], "row 1\0"); // fill row 1
          strcpy(q[9], "row 10\0"); // fill row 10
          FreeMemory(q, 10); // release all the memory

          1 Reply Last reply
          0
          • T tejaswini_g

            Reposting as thread has grown #include <> // remove extra angle bracket for compilation #include <> #include <> #include <> void FreeMemory(char** myArray, int row); void FileCountInDir(const TCHAR* dirpath, int * filescount, int* filesize); void FindFilesInDirectory(const TCHAR* dirpath, char** filelist); int _tmain(int argc, TCHAR *argv[]) { int i =0; TCHAR szDirpath[MAX_PATH]; //TCHAR Filelist[100][MAX_PATH]; char ** Filelist; int iFileCountInFolder=0; int iFileSize = 0; _tcscpy(szDirpath,_T("C:\\BMPread_W\\TransitionEffects\\Images")); FileCountInDir(szDirpath,&iFileCountInFolder,&iFileSize); Filelist = (char**)malloc(iFileCountInFolder * sizeof(char *)); if(Filelist == NULL) { fprintf(stderr, "out of memory\n"); return 0; } for(i = 0; i < iFileCountInFolder; i++) { Filelist[i] = (char*)malloc((iFileSize * sizeof(char))+1); if(Filelist[i] == NULL) { fprintf(stderr, "out of memory\n"); return 0; } } FindFilesInDirectory(szDirpath, Filelist); for(i =0;i 0)|| ((*filesize) < _tcslen(ffd.cFileName))) { (*filesize) = _tcslen(ffd.cFileName); } } } while (FindNextFile(hFind, &ffd) != 0); FindClose(hFind); } void FreeMemory(char** myArray, int row) { int i; if(myArray != NULL) { for (i = 0; i < row; i++){

            free (myArray[i]);

            } }

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            There was no need to repost. It could be spamming. I suggest you read the existing responses carefully before going any further. They may be from members who know something.

            Peter Wasser "The whole problem with the world is that fools and fanatics are always so certain of themselves, and wiser people so full of doubts." - Bertrand Russell

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups