Thread: problems with memory allocation

  1. #1
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001

    problems with memory allocation

    I have one bug with my memory usage. I'm doing this:
    Code:
    char * szBuffer=(char*)malloc(200);
    ...
    
    
    			LPDWORD lpNumberOfBytesRead=new unsigned long;
    			if(ReadFile(hFileRead,szBuffer,sizeof(szBuffer),lpNumberOfBytesRead,NULL)==0){
    				ErrorHandler("Could Not Read File"); //process error
    			}else{//.....
    basically evertime i fire up the program and go through the motions, i get my error of "could not read file" and debug says it has to do with szBuffer...what am I doing wrong here? As a side note: i wasn't using malloc() before...i thought it would solve my problems, before i was doing something like this:
    Code:
    char * szBuffer=new char[200];
    but i found that it is cleaner to use malloc() (and free()) i actually made it farther through the program without it crashing (one line or so, so i believe the error occurs when i'm using szBuffer, not when it's declared). thanks for any help.
    PHP and XML
    Let's talk about SAX

  2. #2
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    sizeof(szBuffer) will not work because szBuffer is a pointer. But the size seems to be 200 which you are using in malloc.

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    but i found that it is cleaner to use malloc() (and free())
    They aren't cleaner. Realize that the new and delete operators
    were designed for a reason.

    For one they are type-safe (No casting needed) also new and
    delete fires the constructors and destructors of class respectively.
    (malloc/free doesn't)

    If you wanted to use new and delete, remember that if you new
    with [], you must delete with [].

    Eg.
    Code:
      Dog* ptr = new char[20]; //call the Dog's constructor
      delete [] ptr; //calls the destructor
    
    //One more example
     
     int i;
     char** StringArray = new char*[10];
     for(i=0;i<10;i++)
         delete [] StringArray[i];
    Trust me when I say, use new and delete rather than malloc/free.

  4. #4
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    char** StringArray = new char*[10];
    for(i=0;i<10;i++)
    delete [] StringArray[i];
    Sorry 'bout this, should've been
    Code:
     char** StringArray = new char*[10];
     delete [] StringArray;

  5. #5
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    alright, i changed back to new[] and delete[]...but I'm still having on more memory problem, and i'm not sure what it is to tell the truth, It's hard to tell from the debug information whats going wrong where. I've attached the source.

    In case it's pertinent, I'm using MSVC++ 6 SP5 (has different output files than earlier service packs i believe)
    PHP and XML
    Let's talk about SAX

  6. #6
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    I noticed that you assign NULL to szBuffer in start search quite a
    few times.

    The problem is (maybe) that you aren't deleting the pointer.
    By doing this -> szBuffer = NULL <- without deleting szBuffer first
    leaves the string in memory. Now on top of losing the data, you
    haven't got any allocated space for szbuffer anymore!!

    Just do this :
    Instead of assigning NULL to szBuffer rather do this:
    Code:
    delete [] szBuffer;
    szBuffer = new char[200];
    Now you always have allocated memory for szBuffer.

  7. #7
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    well that didn't fix it...basically the error message that msvc pops up is "debug assertion failed" at dbgheap.c line 1050...it has to do with a bad pointer (as said in the commented lines in the .c file) I also tried running release mode but the error came up but no debugging.


    I've uploaded a screenshot of the debug output in html format at
    http://www.angelfire.com/ab6/djpimp/...artFinder.html
    PHP and XML
    Let's talk about SAX

  8. #8
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Not to worry!!
    I've re-read your code and OH, MY GWOSH!!! ( Yes GWOSH)

    There's a zillion bugs here. Now pay attention.

    When you declared these variables, what were you trying?

    char * tempFilePathBuffer=new char[];
    char * tempFilePath=new char[];
    char * tempFileName=new char[];
    char * setDir=new char[];
    char * renameCommand=new char[];

    If you not using them in your program then comment all of it,
    otherwise, be so kind as to tell your compiler how much memory
    you want allocated. eg. char* setDir = new char[200];

    Also, you are assigning all the pointers in the world!
    Try using strcpy() instead. Don't do things like :

    tempFileName = fileData.cFileName;
    Rather use strcpy() to copy the data from fileData.cFileName to
    tempFileName.

    And things like these aren't necessary:
    currentDir = strcat(currentDir,"\\");
    Just do : strcat(currentDir,"\\");

    strcat() will append the data to the buffer which is passed to it.

    When you edit your code, edit one thing at a time, then compile
    to check if you're winning.

  9. #9
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    GWOSH!!!
    thanks. See, I've mostly self taught myself, and memory management is definetly something I never looked into, I just sort of did what i could to get the compiler to accept it (yes, bad coding practice) anyhow, thanks for all the tips, tomorrow at work i'll see if I can work out the bugs....thanks.
    PHP and XML
    Let's talk about SAX

  10. #10
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    One reason I'm using all the pointers in the world,
    is because many of the functions I'm using, particularly the API file handling functions require pointers to chunks of memory as their arguments.
    PHP and XML
    Let's talk about SAX

  11. #11
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001

    edited source....

    ok, I've went through, and based on what you've said, changed all the strcat and strtok and strcpy and any other str... i missed. I have not tested it yet (it locks up after debug and i have to restart my computer becuase it thinks the program is still open, so i can't compile until i restart. Any how I've attached the edited source.
    PHP and XML
    Let's talk about SAX

  12. #12
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Could you please comment your code to state what it is you want to do.

    Eg. For code like this
    Code:
    CharUpper(szBuffer);
    strchr(szBuffer, '(' );
    strcpy(a,szBuffer);
    if (a ==NULL)
    {
                    stop=TRUE;
    }else
    {
    	stop=FALSE;
    }	
    if (stop!=TRUE)
    {
    	strtok(a,")");
    	strcpy(b,a);
    }
    Comment your functions so that ppl can see what you want the function to do.

    ALSO
    Your main menu function returns a char*. Because currentDir is declared GLOBALLY, you don't have to return anything.

    void mainMenu( void );

    AND
    strchr(szBuffer,'(')) won't change szBuffer, so it's not doing anything. Same with strtok(a,")").

    One other thing, you might want to lookup the swicth statement and try use that rather than if/else, if/else, if/else, if/else.

  13. #13
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    ok, heres the source with all the important and or strange parts commented
    PHP and XML
    Let's talk about SAX

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. POSIX Threads and dynamic memory allocation
    By PING in forum Linux Programming
    Replies: 1
    Last Post: 04-02-2009, 10:28 AM
  2. Increasing memory allocation in function
    By Ramses800 in forum C Programming
    Replies: 3
    Last Post: 12-16-2008, 05:30 AM
  3. Relate memory allocation in struct->variable
    By Niara in forum C Programming
    Replies: 4
    Last Post: 03-23-2007, 03:06 PM
  4. memory allocation ptr to array? how?
    By kokopo2 in forum C Programming
    Replies: 8
    Last Post: 09-01-2005, 05:06 PM
  5. Memory allocation at runtime?
    By electrolove in forum C Programming
    Replies: 6
    Last Post: 02-05-2003, 11:39 AM