delete char

Jul 17, 2011 at 4:47pm


Hello all,

why does the following works on build time but the program crashes on runtime?


char *seedx = new char;
char *seedy = new char;
seedx = "FastMarching:seedPositionx";
seedy = "FastMarching:seedPositiony";


delete seedx;
delete seedy;

same here:

char *seedx = new char[200];
char *seedy = new char[200];
seedx = "FastMarching:seedPositionx";
seedy = "FastMarching:seedPositiony";


delete [] seedx;
delete [] seedy;

and here:

char *sex = "FastMarching:seedPositiony";
char *sey = "FastMarching:seedPositiony";


delete [] seedx;
delete [] seedy;

and here:


char *sex = "FastMarching:seedPositiony";
char *sey = "FastMarching:seedPositiony";


delete seedx;
delete seedy;


Thanks for any help!
Jul 17, 2011 at 4:58pm
closed account (3hM2Nwbp)
You should have a matching number of new's and delete's. From what you've posted, you're calling delete on pointers multiple times.
Jul 17, 2011 at 5:29pm
Thank for the reply.
But there is four different examples, which i have tried.
There is a matching number of news and deletes .
Although in the last two examples sex and sey should be seedx and seedy. just a mistake here...
Jul 17, 2011 at 6:49pm
noob reply
Last edited on Jul 17, 2011 at 7:07pm
Jul 17, 2011 at 7:02pm
You're assigning the address of the beginning of a string literal to those pointers (thus overwriting the previous address returned by new/new[] and creating a memory leak). You're not allowed to call delete or delete[] on those, their storage is provided by the implementation.
Jul 17, 2011 at 7:04pm
1
2
3
4
5
6
7
8
char *seedx = new char[200];
char *seedy = new char[200]; 
seedx = "FastMarching:seedPositionx";
seedy = "FastMarching:seedPositiony";


delete [] seedx;
delete [] seedy;


This makes little sense to me. The first line creates an array of 200 char, and returns a pointer to it.

You then take that pointer, and you move it. You make it point somewhere else entirely. "FastMarching:seedPositionx" is a string literal that exists somewhere in memory for the entire duration of your program, and you've now taken your pointer and made it point at that memory.

You then try to delete that memory. You must not delete memory that wasn't allocated with new. That memory was not allocated with new. You've also got no way of deleteing the memory that was allocated with new, because you don't have a pointer to it anymore; you've got a memory leak. I think you've misunderstood what new and delete are for.

It crashes on runtime because at build, the compiler has to trust you. it has no choice but to hope that you're not trying to delete memory that you didn't allocate. At runtime, the error happens and you get a crash. If you are unlucky, it does not crash and you don't even know that it's all gone wrong.

Your other code examples likewise. When you use new, you're allocating memory and you get back a pointer to that memory. When you delete, you must delete using a pointer to that exact same memory (doesn't have to be the same pointer, but it must point to the same memory). Changing the pointer halfway doesn't change this.
Last edited on Jul 17, 2011 at 7:07pm
Jul 17, 2011 at 9:18pm

So it seems i am the true newbie of the day!

i tried to make this as simple as possible, that is why there is no point in it.
My goal was:



int ctr;

for(ctr=1;ctr<=20; ctr++)
{
char *buffer = new char[10];
char *seedx = new char[200];
char *seedy = new char[200]; 
seedx = "FastMarching:seedPositionx";
seedy = "FastMarching:seedPositiony";

itoa (ctr,buffer,10);
strcat(seedx,buffer);
strcat(seedy,buffer);                       // so it becomes "Fastmarching:seedPositiony1" and so on...

// DO SOMETHING WITH THIS SEEDY AND SEEDX

delete [] seedx;
delete [] seedy;
delete [] buffer;
} 


So how could i define a char * with "FastMarching::seedpositionx" and be able to delete it?

Thanks!
Jul 17, 2011 at 9:32pm
Like this:
1
2
3
4
5
for(int ctr=1;ctr<=20; ctr++)
{
  string buffer="FastMarching:seedPositionx"+lexical_cast<string>(ctr);
  //do something with buffer
}


or this (needs <sstream> header):

1
2
3
4
5
6
7
for(int ctr=1;ctr<=20; ctr++)
{
  stringstream ss;
  ss << "FastMarching:seedPositionx" << ctr;
  string buffer=ss.str();
  //do something with buffer
}


The respective includes:
1
2
3
4
#include <string>
#include <boost/lexical_cast.hpp>
using std::string;
using boost::lexical_cast;
Jul 17, 2011 at 10:16pm
Huge thanks!!
Jul 18, 2011 at 2:22am
1
2
#include <boost/lexical_cast.hpp>
using boost::lexical_cast;


I believe the above is not part of Standard C++ *yet*. So if you use it in your program it may not compile on other platforms that do not have Boost library installed.

The point now is why is Standard C++ committee members SO SLOW in getting Boost library endorsed as Standard C++ llibraries !?!?!?! They are really working at *snail pace* :P

Oops don't remind me of the story of Tortoise vs Hare race. Slow and steady win the race ?!?!
Topic archived. No new replies allowed.