Avoid Memory Leaks

char* GetString()
 char *a = new char[10]; <-- alocation
 a[9] = '';
 return a;
void Leak()
 char *name = GetString();
 printf("%s\n", GetString()); <-- don't call GetString() directly()

7 thoughts on “Avoid Memory Leaks

  1. Standard functions don’t leak:

    char *foo = malloc(sizeof(char)*10);
    printf("%s",strcpy(foo,"gio nakal"));
  2. Try looking at the last element of the string, it’s properly terminated with null so it won’t leak definitely. A garbage–yes, but definitely not a leak.


    Also malloc() won’t likely to overlap one another.

    Anyway, what is it with ‘c’ tag and ‘new’ from C++? And why won’t you just use std::String rather than silly C string?

  3. what is different between garbage and leak, no diff I guess.

    c tag?

    At many times C string still handy in c++, BTW this is just example

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s