Can an invalid pointer clobber the new operator?

This is a discussion on Can an invalid pointer clobber the new operator? within the C++ Programming forums, part of the General Programming Boards category; I have a problem with my code where it chokes on a "new" statement. This is what it looks like: ...

  1. #1
    Unregistered
    Guest

    Can an invalid pointer clobber the new operator?

    I have a problem with my code where it chokes on a "new" statement. This is what it looks like:

    void String::set(char *cString)
    {
    /* This is a function in my own String class which is an
    assignment of the c-style string pointed to by cString
    to the String object. */

    if(str != NULL)
    delete[] str; // str is of type char*

    int len = strlen(cString);
    len++;
    logfile << "about to allocate memory" << endl;
    str = new char[len];
    logfile << "memory allocated" << endl;
    strcpy(str, cString);
    }

    This has been working fine for months, but all of sudden I am getting a run-time error in the program at the same point every time I run it. I have not modified the String class for months. The program successfully calls this function about a hundred times before it reaches the point where it fails. On checking the log file, I find that the "about to allocate memory" message appears, but the "memory allocated" message is not reached.

    I can't see how a call to "new char[n]" can suddenly fail. I'm guessing that an invalid pointer somewhere else in the program is responsible for this. Can an invalid pointer actually reach and screw up the instructions of an operator? If not, any other ideas? It's a relatively small program, and the program fails at the same point if I cut the data by a third , so I'm sure it's not a case of running out of memory. I am using DJGPP.

    Thanks in advance,
    Sean

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    you can see whether its an out of memory problem by enclosing the call to new in a try/catch block. new throws std::bad_alloc when it cannot find the memory......

    i.e.

    try
    {
    char* pDynamic=new char[1000];
    }
    catch (std::bad_alloc&)
    {
    cerr<<"new failed to find memory";
    exit(0);
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    You can delete pointers to NULL so
    instead of

    Code:
    if(str != NULL) 
         delete[] str; // str is of type char*
    do this
    Code:
    delete[] str;
    cString might be invalid but I can't be too sure.
    That would throw of new for sure you might
    want to throw in a assert statement such as

    Code:
    assert(len >= 0 && len < 100);
    I hope you know how to use gdb because this
    bug might require it.

    Code:
    str = new char[len];
    str is global are you sure you release it, it might be
    easier to manage the memory if you do something like this

    Code:
    char* upstring(char* s)
    {
             char* p = NULL;
     
             if (s != NULL) {
                     int n = strlen(s);
                     p = new [n + 1];
                     strcpy(p, s);
             }
             return p;
    }
    Then the class or function that owns str can handle
    it locally. Another way could be just to use c++'s string's class

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> new throws std::bad_alloc when it cannot find the memory......

    To be ANSI compliant, it should, but many compilers including MS VC++ do not. They return NULL, (as they should with the new(nothrow) construction).
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to an overloaded operator...
    By yaya in forum C++ Programming
    Replies: 3
    Last Post: 01-08-2009, 01:50 AM
  2. Replies: 0
    Last Post: 03-20-2008, 08:59 AM
  3. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  5. Flood of errors when include .h
    By erik2004 in forum C++ Programming
    Replies: 14
    Last Post: 12-07-2002, 07:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21