Re: about new operator



harshalshete@xxxxxxxxx wrote:

hi group here is my second problem

i am allocating a buffer

char *p = new char[1024];

p = regBuffer.GetBuffer(regBuffer.GetLength());

and after doing all the work i am trying to free that memory with

1)delete []p;
p=NULL;
2)delete p;
p=NULL;





but in both the cases i get error as
debug assertion failed
File dbgheap.c
Line: 1011
Expression: _CrtIsValidHeapPointer(pUserData)

can anybody tell me what's wrong with this code


harshalshete:

What do you expect? Your have overwritten your variable p (which was pointing to your new'd memory) with something else, so you cannot use it to free the memory any more. You don't need to allocate any memory here; you are getting it from regBuffer (which is a CString, yes?).

BTW when you use array new, you must use array delete

char *p = new char[1024];
// don't reassign to p !!!
delete [] p;

David Wilkinson

.



Relevant Pages

  • Re: Library Design, f0dders nightmare.
    ... first demo mistake but I suggest to you that a sequence of blunders ... The stack is only used temporarily while creating the argv array. ... in the input buffer. ... so you are not wasting memory. ...
    (alt.lang.asm)
  • Re: J4 - presentation/discussion on "Future of the COBOL Standard"
    ... there is obviously no problem with allocating the maximum ... array size: all that's actually needed in the compiled program is a ... gigantic physical memory spaces are no longer news. ... compilers that actually allocate space as part of the compiled unit will ...
    (comp.lang.cobol)
  • Re: Difference?
    ... > allocating memory for an array of objects. ... > that malloc will align the memory assuming that the memory allocated ...
    (comp.lang.c)
  • Re: Why delete [] for aray?
    ... Does he use of delete instead of deletewill cause a memory leak? ... the pointer points to an array. ... Moreover, when allocating arrays of classes with non-trivial destructor, ...
    (microsoft.public.vc.language)
  • Re: [linux-usb-devel] [PATCH] visor: Fix Oops on disconnect
    ... > smells like some type of resource leak, probably memory, to me. ... > difficulty allocating memory. ... Reusing the buffer and urb is _slow_. ... send the line "unsubscribe linux-kernel" in ...
    (Linux-Kernel)

Quantcast