Thread: problem with "nothrow"

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    67

    problem with "nothrow"

    Moin,

    I want to get a NULL-Pointer instead of a bad_alloc exception in use with new.

    But my Program crashes if I use "nothrow" !
    What's wrong with my code ?

    Please, could somebody give me a hint !?

    Code:
    #include <new>
    
    // This works, but not with (std::nothrow) ..., why ???
    pObj = new /*(std::nothrow)*/ OBJ[newDim];
    Thanks.


    Greetz
    Greenhorn

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Where exactly does the program crash?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    67
    Quote Originally Posted by CornedBee View Post
    Where exactly does the program crash?
    In the debugger exactly at/after this line !
    There comes an error message if I go one step forward: "There is no source code available at this line"

    Screenshot

    This is the function.
    Code:
    bool TabCtrl::InsertTabItem (unsigned numID) {
    
    	TAB_ITEM *pTempBuf = NULL;
    	unsigned oldDim  = tabNumItems;
    	unsigned newDim  = ++tabNumItems;
    
    
    	if (oldDim == 0) {
    
    		tabItems = new /*(std::nothrow)*/ TAB_ITEM[newDim];
    	} else {
    		pTempBuf = new (std::nothrow) TAB_ITEM[oldDim];
    
    		if (pTempBuf == NULL)
    			return false;
    
    		// Das alte Array Zwischenspeichern.
    		for (size_t i = 0; i < oldDim; i++) { pTempBuf[i] = tabItems[i]; }
    		// Den Speicher freigeben.
    		delete [ ] tabItems;
    
    		// Den Zwischenspeicher zur&#252;ck kopieren.
    		tabItems = new /*(std::nothrow)*/ TAB_ITEM[newDim];
    
    		if (tabItems == NULL)
    			return false;
    
    		for (size_t i = 0; i < oldDim; i++) { tabItems[i] = pTempBuf[i]; }
    		// Den Zwischenspeicher wieder freigeben.
    		delete [ ] pTempBuf;
    	}
    
        // Das neue Tab-Item initialisieren
    	SetTabItemRect (oldDim);
    
        // Das neue Tab-Item als "selected" setzen
        tabCurSelItem = oldDim;
    
    	// Alle Tab-Items updaten
    	AlignTabItems ();
    
    	return true;
    }

    Greetz
    Last edited by Greenhorn__; 09-07-2008 at 11:16 AM.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Very peculiar. I could have imagined VC++6 doing something absurd like this, but not 2008. No idea what could cause this crash.

    On a different note, there's no need to allocate a temporary array, copy the data, then allocate a new one and copy the data again. You can do it like this:
    Code:
    pTempBuf = new TABITEM[newDim];
    // copy stuff from tabItems to pTempBuf
    delete[] tabItems;
    tabItems = pTempBuf;
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    67
    Thank you very much, CornedBee.

    I will try it.

    The "nothrow"-Thing I will try in Code::Blocks and MinGW and then I'll give a report ...


    Greetz

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It looks a bit that you are reinventing a (square) std::vector<TAB_ITEM>?

    Are you sure that you can't use one? In general, I don't believe the implementation of high level classes such as TabCtrl should be implementing really low-level behaviour such as resizing dynamic memory.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    67
    Quote Originally Posted by anon View Post
    It looks a bit that you are reinventing a (square) std::vector<TAB_ITEM>?

    Are you sure that you can't use one? In general, I don't believe the implementation of high level classes such as TabCtrl should be implementing really low-level behaviour such as resizing dynamic memory.
    You're right, it could be done easier with std::vector<TAB_ITEM>.
    But I'm a Rookie and so I want to learn the basics at first.

    Thanks for the hint, anyway.


    Greetz

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    IMO, in C++ vector is the basics. What goes on behind the facade of vector definitely isn't the basics.

    Also, it helps you come up with better designs if you know how using SC++L stuff works. You want to reimplement your own vector? Fine - but make it a separate class, that is used by TabCtrl to implement its high-level functionality.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Registered User
    Join Date
    Jul 2008
    Posts
    67
    Quote Originally Posted by anon View Post
    IMO, in C++ vector is the basics. What goes on behind the facade of vector definitely isn't the basics.

    Also, it helps you come up with better designs if you know how using SC++L stuff works. You want to reimplement your own vector? Fine - but make it a separate class, that is used by TabCtrl to implement its high-level functionality.
    I don't want to create a container class like vector, just an dynamic array, that's all.


    Greetz

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    But ... but ... vector IS "a dynamic array".

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    A vector is just a dynamic array?

    The thing is that your implementation of the dynamic array is built into a class, whose task seems to be providing a completely different service.

    What InsertTabItem does is:
    a) mess with dynamic allocations (16 lines)
    b) initialize the tab item (1 line)
    c) selects the new item (1 line)
    d) update all tabs (1 line)

    Doesn't this look a bit disproportionate? It would seem that the classes reason to exist is to handle dynamic allocations?

    Wouldn't it be better if those 16 lines of memory reallocation were handled elsewhere too, by a smart RAII object that has been well tested in isolation?

    Anyway, the cause of crashes may well be elsewhere. How's your constructors, copy constructors and such?

    One mistake that I see is that you increment tabNumItems member regardless of whether the allocations succeed or fail (I wouldn't expect new to fail that often, though, that you might have noticed).
    Last edited by anon; 09-07-2008 at 03:40 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Greenhorn__ View Post
    I don't want to create a container class like vector, just an dynamic array, that's all.Greetz
    That's kinda like saying you don't want to drink H20, but want to drink water.

    The problem is that the "dynamic array" you're writing has absolutely shockingly terrible performance for adding new items, compared to a vector. Yours also is pretty buggy, unsafe, and incomplete right now. To add item number 1000, your code does 1999 constructions, 1999 destructions and 1999 assignments. A vector usually does just one copy-construction, (ignoring for the moment amortized constant time operations).
    You should probably learn how to make your own vector another time. Right now you may as well get this finished quickly by simply using what the standard library provides.
    Last edited by iMalc; 09-07-2008 at 04:40 PM.

  13. #13
    Registered User
    Join Date
    Jul 2008
    Posts
    67
    I agree with all of you, and in the end it could be that I'll use a vector instead of what I'm trying now.
    But, as I said, I also want to learn how to work with dynamic arrays under use with "new" and "delete".

    I don't need 1000 Tabs, in practice I think I never need more than 50 Tabs.

    @anon
    You're right, this error I didn't noticed until now, thanks a lot.

  14. #14
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I wouldn't criticize this so much for run-time inefficiency.

    TabCtrl probably won't be your last class to use some kind of dynamic storage for something. Do you think it is wise to keep reimplementing exactly the same memory handling functionality for all of them (and one that's easy to get wrong), or would it be better if there was a (generic) class like vector that would take care of this task once and for all?

    If you want to implement a dynamic-array class for practice, go ahead, but it should still be a separate class (that TabCtrl, among others might use - making it a template is a good idea). Otherwise, unless you are implementing classes whose only purpose is to manage memory, you should practically never need the array form of new and delete in your code.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM