Memory Allocation and Freeing using New and Delete Operators 
Author Message
 Memory Allocation and Freeing using New and Delete Operators

Hi,

I am not able to free the memory which was dynamically allocated. Coulp you
point out where I am going wrong. Thanks in advance

Here is the code snippet.

#include "iostream.h"
#include "string.h"

void main (void)
{
 char* szVerify = new char[2048];
 strcpy(szVerify, "");

 strcpy(szVerify, "C:\\Temp\\Boot.ini");

 char* szTemp1 = new char[strlen(szVerify) + 1];
 szTemp1 = strstr(szVerify, "\\");

 cout << szVerify << endl << szTemp1 << endl;

// It fails here
 if
Temp1){
  delete [ ] szTemp1;
 }

 if (szVerify){
  delete [ ] szVerify;
 }

 return;

Quote:
}



Mon, 08 Apr 2002 03:00:00 GMT  
 Memory Allocation and Freeing using New and Delete Operators


Quote:
> Hi,

> I am not able to free the memory which was dynamically allocated. Coulp
you
> point out where I am going wrong. Thanks in advance

> Here is the code snippet.

> #include "iostream.h"
> #include "string.h"

> void main (void)

Uhhg!  Don't say void main, say int main.  And yes, I know, many
teaching texts use void main.  They should not.

Quote:
> {
>  char* szVerify = new char[2048];
>  strcpy(szVerify, "");

>  strcpy(szVerify, "C:\\Temp\\Boot.ini");

>  char* szTemp1 = new char[strlen(szVerify) + 1];
>  szTemp1 = strstr(szVerify, "\\");

You need to read up on the care and feeding of the strstr()
function.  This line stomps on the old pointer to the previously
allocated memory.  That means a memory leak and a pointer
to memory that you did not allocate through a new.

Quote:
>  cout << szVerify << endl << szTemp1 << endl;

> // It fails here
>  if
> Temp1){
>   delete [ ] szTemp1;
>  }

I presume this is supposed to be the following.

if(szTemp1){
  delete [ ] szTemp1;

Quote:
}

Since szTemp1 now points at the wrong spot, this will fail.
Also, testing against the pointer does not add any value.
If the pointer is NULL, deleting it is safe.  If it is not NULL,
knowing that does not make calling delete any safer.

Quote:
>  if (szVerify){
>   delete [ ] szVerify;
>  }

Same comment about the test here.

--
Dan Evens
Standard disclaimers etc. No spam please.



Mon, 08 Apr 2002 03:00:00 GMT  
 Memory Allocation and Freeing using New and Delete Operators


Quote:
> Hi,

> I am not able to free the memory which was dynamically allocated. Coulp
you
> point out where I am going wrong. Thanks in advance

> Here is the code snippet.

> #include "iostream.h"
> #include "string.h"

> void main (void)
> {
>  char* szVerify = new char[2048];
>  strcpy(szVerify, "");

>  strcpy(szVerify, "C:\\Temp\\Boot.ini");

>  char* szTemp1 = new char[strlen(szVerify) + 1];
>  szTemp1 = strstr(szVerify, "\\");

>  cout << szVerify << endl << szTemp1 << endl;

> // It fails here
>  if

p1){
>   delete [ ] szTemp1;
>  }

No wonder.  Check the return value of strstr().  It returns
a value other than what you sent it.  In essence you've lost
track of the memory formally known as szTemp1.

char* szTemp1 = strstr(szVerify, "\\");

should fix the problem, and don't delete[] szTemp1 since
it wasn't allocated with new[].

marco

P.S.  Don't bother checking for NULL on delete.  It's a
waste of time and space.  'delete NULL' and 'delete[] NULL'
are well defined to do nothing.



Mon, 08 Apr 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. delete Operator doesnt free Memory ???

2. Memory Allocation and DeAllocation (new and free) !?

3. win32 memory management - new and delete operator overloading

4. memory allocation using malloc and free

5. Pointer to object memory problems using new operator.

6. cannot release memory allocated using new operator

7. dynamic memory overrun when using new and delete

8. Shared Memory, NT 4.0, object memory allocation (new)

9. Tree memory allocation, pool memory allocation

10. Problems freeing memory using "free"

11. overloading the new and delete operator

12. Memory allocation/free problem

 

 
Powered by phpBB® Forum Software