1

Consider the following code:

int main()
{
    char* s = (char*) malloc(sizeof(char)*10);
    s="hello";
    free(s);
}

When executing this program I get an error:

** glibc detected *** free(): invalid pointer: 0x0000000000400b2c

My research on this error indicates it may be caused by not assigning enough memory space via malloc(). But the program already calls malloc(), producing enough space for 10 chars.

carlpett
  • 12,203
  • 5
  • 48
  • 82
Ruchi
  • 693
  • 3
  • 14
  • 27
  • This is not how modern C++ looks like, even though that is valid C++. Pick up [a good introductory book on C++](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and learn proper C++. For example, instead of using `malloc()`, you can use the vastly superior and significantly easier to use `std::string` class in C++. – In silico Sep 09 '11 at 05:50
  • Just pointing out, the question is C related, not C++ – EddieBytes Sep 09 '11 at 05:53
  • @EddieBytes: the question was originally tagged C++ only. I'm not sure I approve of the edit that tagged it C, that introduces another problem with casting the result of `malloc`. – Mat Sep 09 '11 at 07:10

5 Answers5

10
s="hello";

You are assigning another address to s, to a statically allocated memory. Freeing it is not correct. Also, since you are doing this, you are basically leaking the memory you have allocated here:

char* s = (char*) malloc(sizeof(char)*10);

Try:

int main()
{
    static const size_t kBufferSize = 10;
    char* s = (char*) malloc(sizeof(char) * kBufferSize);
    strncpy(s,"hello", kBufferSize); // better than strcpy, you are protecting
                            // yourself from a buffer overflow
    free(s);
}
EddieBytes
  • 1,333
  • 9
  • 20
  • 1
    typecast of `malloc`result is bad C. `sizeof (char)` is per defintion 1. `strncpy` fills the buffer with 0 (in that buffer of 10 no problem, in bigger buffers it can be a proble), and doesn't guarantee a `NUL`termination. Your `main` lacks a `return`. If you give an example, try to write correct C. – Patrick Schlüter Sep 09 '11 at 08:02
  • The example is an edit of what the OP gave. I can not assume the value he wishes to return from his main. As for the rest of your arguments, please elaborate on your statements and post better code. Thanks. Also, strncpy, of course it doesn't NULL terminate, it is a memory function not a string type aware function, despite the name. – EddieBytes Sep 09 '11 at 08:06
  • 3
    A byte is a char is a byte by definition, so sizeof (char) will always be 1, a better implementation would be sizeof(*s). In C, casting the result of malloc is unneccesary and can hide other errors (such as forgetting to define malloc, resulting in an implicit definition of malloc which returns an int) so is considered poor form. If that helps clear things up. – James Greenhalgh Sep 09 '11 at 11:25
9

After:

s="hello";

s no longer points to the memory you dynamically allocated. It points to that string literal "hello". You can't free that since it wasn't malloced in the first place. And you've leaked that allocation since you no longer have a pointer to it.

Look at the strncpy function to copy one C string to another.

Mat
  • 202,337
  • 40
  • 393
  • 406
2

You are reassigning s from the malloc'd pointer to a constant string literal, which you then try to free. As the string literal was not allocated with malloc, freeing it unsurprisingly leads to Bad Things.

Oh, and I see you've cast malloc's return. If you're using C, you should not do this; if you're using C++, then you should be using new/delete rather than malloc/free.

Hugh
  • 8,872
  • 2
  • 37
  • 42
1

It is an error to pass to free() anything not coming from malloc().

Assigning "hello" to s and then attempting to free it violates this rule.

wallyk
  • 56,922
  • 16
  • 83
  • 148
0

the error is that you are freeing memory you do not own. you are freeing a string literal, rather than explicitly created memory requested via malloc/new & co..

justin
  • 104,054
  • 14
  • 179
  • 226