c++: will this memory be automatically deallocated?

Started by
13 comments, last by DevFred 15 years, 4 months ago
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; }
Advertisement
Nope.

In C and C++, memory is never automatically deallocated.
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?
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 pointerdelete [] 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.
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.
Create-ivity - a game development blog Mouseover for more information.
okay thanks a lot. btw i just used char* as an example, my intention was to become a little closer with pointers ;)
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.
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.

"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
Quote:int x = strlen(string);

Why calculate the length of the string if you never use it anyway?
Construct (Free open-source game creator)
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.

This topic is closed to new replies.

Advertisement