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.