New vs Malloc again!

This is a discussion on New vs Malloc again! within the Windows Programming forums, part of the Platform Specific Boards category; Okay, I am hopeless. I thought I understood the use of new, but I am doing something incorrectly. My code ...

  1. #1
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396

    New vs Malloc again!

    Okay, I am hopeless. I thought I understood the use of new, but I am doing something incorrectly.

    My code works great if I use malloc, but when I change it to new and delete I get the "DAMAGE: after normal block" debug error (see attached)

    Code:
    BOOL CALLBACK CenterCoordinates(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
    	int nLen = 0;
    	int Decimal = 0;
    	int i;
    	LPTSTR pbuffer = NULL; //char* pbuffer = NULL;
    
    	switch(Message)
        {
            case WM_INITDIALOG:
    
            return TRUE;
            case WM_COMMAND:
                switch(LOWORD(wParam))
                {
                    case IDOK:
    					{
    						hdlg = GetDlgItem(hwnd,IDC_XBAR );  //(hOwner, hDialogEditField)
    						nLen = GetWindowTextLength(hdlg);
    
    					//	if(pbuffer = (LPTSTR)new int(nLen + 1))
    					//	if(pbuffer = (LPTSTR)new char(nLen + 1))
    						if(pbuffer = (LPTSTR)malloc(nLen + 1))
    						{
    							GetDlgItemText(hwnd,	// handle to OWNER of the control
    								IDC_XBAR,			// control identifier
    								pbuffer,			// pointer to buffer for text
    								nLen+1				// maximum size of string + NULL term.
    							);
    					// validation goes here
    							subsectionData[SectionNumber].ptX[0] = strtod(pbuffer, NULL);// Center X coord of circle.
    							delete(pbuffer);
    							//free(pbuffer);
    						}
    						else
    						{
    							MessageBox (NULL, TEXT ("Memory allocation failed."), 
    								TEXT (""), MB_ICONINFORMATION) ;
    							exit(-1);
    						}
    
    						//... more code here.
    P.S. If I rem out the delete(pbuffer); line my proggy runs without choking!
    Last edited by Bajanine; 05-12-2003 at 09:33 PM.
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  2. #2
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    If you're running it the way you posted it (boy, you use a lot of space!):
    Code:
    // if(pbuffer = (LPTSTR)new int(nLen + 1))
    // if(pbuffer = (LPTSTR)new char(nLen + 1))
    if(pbuffer = (LPTSTR)malloc(nLen + 1))
    {
      // ....
      delete(pbuffer);				
      //free(pbuffer);
    }
    the problem is possibly that you allocated with malloc() and freed with delete. If you alloc with malloc() you have to release with free() and if you alloc with new you have to release with delete.

    I assume you commented out the lines:
    >> //if(pbuffer = (LPTSTR)new int(nLen + 1))
    >> //if(pbuffer = (LPTSTR)new char(nLen + 1))
    because of a compile error perhaps. It appears you are attempting to allocate an array of ints and an array of chars however your syntax is incorrect; you have to use square brackets to allocate an array with new.
    ie:
    pbuffer = new char[nLen + 1];
    Then you would subsequently have to delete with the square brackets:
    delete [] pbuffer;
    Whereas if you were dealing with a single element you'd do this (no square brackets):
    pbuffer = new int;
    delete pbuffer;

  3. #3
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396
    Buy do I feel stupid! Maybe I need to quit mixing 12 hour work days with trying to program!

    Anyway, thanks for the pointers.
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  4. #4
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    You got it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 11:26 AM
  3. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  4. malloc and realloc
    By odysseus.lost in forum C Programming
    Replies: 3
    Last Post: 05-27-2005, 08:44 AM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 08:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21