Thread: Problem with this code

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    22

    Problem with this code

    Hi, can anyone explain why this crashes at the delete part?

    char *args[1000];

    args[0] = new char[10];

    delete [] args[0];

    Thanks.

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    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:
    Code:
    char (*args)[1000];
    I think, but I'm not too sure.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    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

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    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

  5. #5
    Inverse Tangent Studios
    Join Date
    Aug 2004
    Posts
    4
    this code, by itself, works fine:
    Code:
    	char *args[1000];
    	args[0] = new char[10];
    	delete [] args[0];
    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.

  6. #6
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Try this when you delete.

    Code:
    if(args[0] != NULL)
        delete [] args[0]
    Ive actually had a problem where it tries to delete whts not there and crashes

  7. #7
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Quote Originally Posted by Vicious
    Try this when you delete.

    Code:
    if(args[0] != NULL)
        delete [] args[0]
    Ive actually had a problem where it tries to delete whts not there and crashes
    No!
    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:
    Code:
     if (p != NULL)
       delete p;
    Right:
    Code:
     delete p;
    source: http://www.parashift.com/c++-faq-lit....html#faq-16.7
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  8. #8
    Registered User
    Join Date
    Jan 2004
    Posts
    22
    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).

  9. #9
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    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.

  10. #10
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code problem
    By sybariticak47 in forum C++ Programming
    Replies: 9
    Last Post: 02-28-2006, 11:50 AM
  2. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 06:39 PM
  3. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 01:05 PM
  4. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  5. Help with code for simple Y2K problem
    By Mule in forum C++ Programming
    Replies: 3
    Last Post: 03-06-2003, 12:53 AM