Hi, can anyone explain why this crashes at the delete part?
char *args[1000];
args[0] = new char[10];
delete [] args[0];
Thanks.
Hi, can anyone explain why this crashes at the delete part?
char *args[1000];
args[0] = new char[10];
delete [] args[0];
Thanks.
I think the first line is creating a pointer to an array of 1000 chars, as opposed to an array of 1000 char pointers. Add these brackets:
I think, but I'm not too sure.Code:char (*args)[1000];
[email protected]
Microsoft Visual Studio .NET 2003 Enterprise Architect
Windows XP Pro
Code Tags
Programming FAQ
Tutorials
you should use the delete keyword whenever you create new memory....
Not sure if this is what you are trying to do.. .but here is an example of how you would create a dynamic array... and then properly delete it.
Code:#include<iostream> using namespace std; int main() { int capacity; cout << "Enter the size of the array: "; cin >> capacity; //create a dynamic array based on user input char *args; args = new char[capacity]; //At this point, you can manipulate the args[] dynamic array size and content as you wish //When you are done with args[], you should delete this newly allocated memory delete [] args; return 0; }
As you can see.. with a dynamic array, you can adjust to the size of the array to meet the needs of your program... unlike a regular array which must be declared with a fixed number of elements
Last edited by The Brain; 08-31-2004 at 05:27 AM.
- "Problem Solving C++, The Object of Programming" -Walter Savitch
- "Data Structures and Other Objects using C++" -Walter Savitch
- "Assembly Language for Intel-Based Computers" -Kip Irvine
- "Programming Windows, 5th edition" -Charles Petzold
- "Visual C++ MFC Programming by Example" -John E. Swanke
- "Network Programming Windows" -Jones/Ohlund
- "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
- "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel
I'd post more code. The code that you did post compiled and ran for me.
If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein
this code, by itself, works fine:
so there must be something else happening inbetween. some possible causes can be you overwrite the pointer stored at args[0] and then delete the new pointer, or your code manages to delete args[0] more than once. a common mistake is to say args[0] = someotherstring; in an attempt to copy a string, but this will only copy the pointer to the string, and thus, when you try to delete args[0], it's really trying to delete 'someotherstring.' if this is your problem, use strcpy() or memcpy() or some string copying routine instead of the = operator.Code:char *args[1000]; args[0] = new char[10]; delete [] args[0];
Try this when you delete.
Ive actually had a problem where it tries to delete whts not there and crashesCode:if(args[0] != NULL) delete [] args[0]
No!Originally Posted by Vicious
The C++ language guarantees that delete p will do nothing if p is equal to NULL. Since you might get the test backwards, and since most testing methodologies force you to explicitly test every branch point, you should not put in the redundant if test.
Wrong:
Right:Code:if (p != NULL) delete p;
source: http://www.parashift.com/c++-faq-lit....html#faq-16.7Code:delete p;
source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense
Hi, thanks for the help. I think I found the problem. I had
args[argnum] = new char[sizeof(superbuffer) + 1];
I think I meant strlen(superbuffer).
I read in deitel and deitel I think that you should set the pointer to null after deletion. Is my memory failing or is there some valid reason for doing this.
Unless the pointer goes out of scope immediately after you delete it, the reason for setting it to null is so that if any other code tries to delete it again it will not crash, and any code that checks for a null pointer will not crash when trying to use it after you delete it.
In general your code should be smart of enough not to use deleted pointers, but there are some cases when you don't know one way or another, and so setting it to null tells you.