Jump to content
  • Advertisement
Sign in to follow this  
abeylin

C++ string question

This topic is 4681 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm a C programmer, and this is how I use dynamic memory in C: <code> char *p = (char *) malloc(256); if (p == NULL) { fprintf(stderr, "get_hello: Cannot allocate 256 bytes\n"); return (NULL); } strcpy(p, "hello world!"); } </code> In C++ I invision doing this: <code> string s("hello world!"); </code> How do I know that there was enough memory to store the "hello world!" string? It's possible the constructor failed to allocate memory, what would happen in that case? What if I do this: <code> string s; s = "hello"; s += " world!"; </code> Than what? it had to do a realloc (ok in the simple example above it probably didn't, but if the second string was big enough that it hadn't preallocated enough memory, it would have to reallocate more space). How do I check the memory allocation that's happening behind the scenes in the string class?

Share this post


Link to post
Share on other sites
Advertisement
If the constructor cannot allocate memory it should throw an exception.
An exception is a method commonly used in C++ to handle critic errors.
Another possibility, less common, is to use your own memory allocators (see custom std allocators).

Share this post


Link to post
Share on other sites
Quote:
Original post by abeylin
In C++ I invision doing this:
<code>
string s("hello world!");
</code>

How do I know that there was enough memory to store the "hello world!" string?


Because the program keeps running. See below...

Quote:
It's possible the constructor failed to allocate memory, what would happen in that case?


Assuming a halfway decent compiler, and normal usage, an exception will be thrown to indicate the out-of-memory condition. This is what happens any time the C++ (default) operator new[] (or plain operator new, for that matter) is invoked and the needed memory is not available. It does not return null.

If the exception is not "caught", the program will terminate immediately, with very nicely defined behaviour (and depending on your compiler, possibly something like a stack trace or short debug dump). This is much, much nicer than the undefined behaviour (normally segfaults) that you get if you forget to check for a null return from malloc() and the memory isn't available. Also, you have much more freedom WRT how you catch the exception than you do with checking a return code. (Although in C++, you will need to be careful about "exception safety" if you deliberately design for exceptions to be thrown across function boundaries.)

[google] 'exception handling' for more info.

Quote:
How do I check the memory allocation that's happening behind the scenes in the string class?


You don't; the whole point of the class is to save you the effort of caring about all that stuff - because it's much more difficult than what you *really* want to get done, and not relevant to your high-level conception of the task.

Share this post


Link to post
Share on other sites
wow, so "new()" throws an exception. It makes sense.
With that information I went to google to double check, and found that it doesn't always throw an exception: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/_pluslang_the_new_and_delete_operators.asp
But you can force an exception to always be thrown.

Thanks for the info.

Share this post


Link to post
Share on other sites
Quote:
Original post by abeylin
wow, so "new()" throws an exception. It makes sense.
With that information I went to google to double check, and found that it doesn't always throw an exception: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/_pluslang_the_new_and_delete_operators.asp
But you can force an exception to always be thrown.

Thanks for the info.


No, new does not throw usually an exception. I write usually because in C++ you can also override the global operators (new and delete in particular).
So you could force new to throw an exception if the system cannot allocate memory ( simply redefine the new 'function' )
Commonly an exception is thrown inside a constructor if new returns NULL as failure response.

Share this post


Link to post
Share on other sites
The c++ standard specifies that (excepting the nothrow versions) the new operator and new[] operator throw exceptions if they fail to allocate storage:
Quote:
C++ Standard, 3.7.3.1.3:
An allocation function that fails to allocate storage can invoke the currently installed new_handler (18.4.2.2). [Note: A program supplied allocation function can obtain the address of the currently installed new_handler using the set_new_handler function (18.4.2.3). ] If a nothrow allocation function (18.4) fails to allocate storage, it shall return a null pointer. Any other allocation function that fails to allocate storage shall only indicate failure by throwing an exception of class std::bad_alloc (18.4.2.1) or a class derived from std::bad_alloc.

Microsoft Visual C++ 6.0 was written pre standardisation, and it's new and new[] operators return null on allocation failure. In order to retain backwards-compatability newer versions of Visual C++ all provide some method to have the new and new[] operators return null instead of throwing. This is only for backwards compatability and should never be used otherwise. If you require a non throwing new or new[] operation then use the standard-defined nothrow forms of the new and new[] operators.

Enigma

Share this post


Link to post
Share on other sites
Quote:
Original post by Enigma

Quote:
C++ Standard, 3.7.3.1.3:
An allocation function that fails to allocate storage can invoke the currently installed new_handler (18.4.2.2). [Note: A program supplied allocation function can obtain the address of the currently installed new_handler using the set_new_handler function (18.4.2.3). ] If a nothrow allocation function (18.4) fails to allocate storage, it shall return a null pointer. Any other allocation function that fails to allocate storage shall only indicate failure by throwing an exception of class std::bad_alloc (18.4.2.1) or a class derived from std::bad_alloc.

Enigma


Interesting.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!