Jump to content
  • Advertisement
Sign in to follow this  
dominikH

c++: will this memory be automatically deallocated?

This topic is 3665 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 wrote this piece of code in order to remove those chars of an chararray, which are blanks. The pointer to this chararray is moved to the right in order to change the beginning. Well, my question is, whats about the heading blanks? Will the memory, they're stored in, be automatically deallocated? sry for my bad english #include <iostream> #include <string.h> using namespace std; char* remove(char* string){ int x = strlen(string); while((int)*string == 32){ string++; } return string; } int main(int argc, char** argv){ char* bla = " test"; cout << remove(bla); return 0; }

Share this post


Link to post
Share on other sites
Advertisement
the memory is allocated on the line

char* bla = " test"; (see as new char[5])

so pushing the pointer to the second address dont make c++ release it.

So I'd say that the memory will all be released together when you quit your main (when you bla is desalocated)

if you want to make sure you dont have any loss you could realloc the bla when your done in your remove function or just use a String?

Share this post


Link to post
Share on other sites
Yes, since you don't allocate anything manually (malloc(), new, etc), all will be taken care of automatically.

Besides you don't actually move the pointer anywhere. In main() bla still points to the same place (as it should), and if you so wish you might need to assign the result of the function call back to bla (or some other char*).

On the other hand, if the buffer was dynamically allocated, and you were to assign the result back to the pointer (or otherwise lose it), it would be impossible to deallocate the memory, since you wouldn't have a valid pointer to the beginning of the allocated memory any more.


char* p = new char[16];
strcpy(p, " hello");
p = remove(p);
//can't call delete[] p, since p doesn't necessarily point to the same
//memory returned by new[]

//vs.
char* q = new char[16];
strcpy(q, " hello");
std::cout << remove(q); //a call by itself doesn't change the pointer
delete [] q; //safe, since q points to the original location



Other notes:


while((int)*string == 32){


This is just an obfuscated way to write:

while (*string == ' ')


But what happens if the string is empty or contains of spaces only? You need to check that you don't go out of bounds too (pehaps you meant to use the result of strlen but this is simpler):

while (*string && *string == ' ')


Then you must know that string literals are constant and attempts to modify the contents might even crash the program. So you should add const before any char* declaration.

Share this post


Link to post
Share on other sites
Since you're using C++, using std::string instead of char*'s would be the real solution. std::string manages a strings memory for you, and just in case you need to pass a char* around (for example when interfacing with C libraries), you can use it's c_str() member function.

EDIT: As for trimming whitespace, most libraries that provide such functionality return a new string without whitespace, rather than modifying the given string. With std::string, you can access characters using the [] operator, comparing them to ' ', then note the first character that isn't a space and call substr() on the given string, to retrieve a string without leading spaces. Or you can simply use the find_first_not_of() function to get the location of the first non-space character.

Share this post


Link to post
Share on other sites
In C++, string literals are of char const[] type, so you mustn't modify them.

Don't cast a string literal to a char*. Ever. That you're allowed to do it implicitly is only for backward compatibility to pass literals to C APIs, provided you guarantee that they never modify the contents. Ever.

Share this post


Link to post
Share on other sites
Quote:
Original post by visitor
[...]But what happens if the string is empty or contains of spaces only? You need to check that you don't go out of bounds too (pehaps you meant to use the result of strlen but this is simpler)[...]
If the pointer points to a space, it can't point to a nul terminator. If it points to a nul terminator, it can't point to a space. Thus, you don't need both conditions.

Share this post


Link to post
Share on other sites
Quote:
Original post by Rattenhirn
Nope.

In C and C++, memory is never automatically deallocated.


Stack objects get allocated/deallocated automatically. Heap objects do not. Ie:


class Foo
{
};

main(..)
{
Foo foo_1; // Allocated on the stack
Foo* pFoo_1 = &foo_1;
Foo* pFoo_2 = new Foo(); // Allocated on the heap

}



"foo_1" get automatically constructed an destructed when it enters and exits scope. pFoo_1 is just the address of foo_1, so doesn't change things. pFoo_2 will not be deallocated since the user allocated memory and is responsible for deallocating it.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!