3

I have a problem when I am trying to free memory at the end of my program. It breaks all the time. Can you tell me where is the problem please?

int main() {

char* word = NULL;
int i = 0;
char str1[12] = "oko";

while (str1[i]) {
    str1[i] = tolower(str1[i]);
    i++;
}

printf("%s", str1);

word = (char *)malloc(strlen(str1) + 1);
word = str1;
printf("%s", word);

free(word);
system("pause");

return 0;
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Jax-p
  • 7,225
  • 4
  • 28
  • 58
  • 3
    [Please see this discussion on why not to cast the return value of `malloc()` and family in `C`.](http://stackoverflow.com/q/605845/2173917). – Sourav Ghosh Jan 19 '16 at 19:13
  • @SouravGhosh I am really tired seeing this phrase at every second question with c-tag... – Ctx Jan 19 '16 at 19:33
  • "*It breaks all the time.*" -- Please update your question to indicate exactly *how* it breaks. – Keith Thompson Jan 19 '16 at 19:34
  • 1
    @Ctx maybe, but it's true. – Sourav Ghosh Jan 19 '16 at 19:34
  • @Ctx It's not his fault. Casting the result of `malloc` is a plague descended upon the C programming community, he's just trying to fight it. – user4520 Jan 19 '16 at 19:34
  • @SouravGhosh What is true? That there are arguments for and against it, as in the linked question? Why do you need to mention that always? – Ctx Jan 19 '16 at 19:35
  • 1
    @Ctx: Please blame the origin, not the messenger. Sourav does right telling people it is bad practise. Maybe that will change code for the better some day. (Although I'm afraid there always will be teachers who refuse to learn themselves and teach bad coding style). – too honest for this site Jan 19 '16 at 19:39
  • 1
    @Ctx: There are valid arguments against casting the result of `malloc`. I'm not aware of any valid arguments in favor of it (unless you need to compile the same code as C and as C++, but that's a rare requirement, or unless you need to use a pre-ANSI C compiler, which is even rarer). – Keith Thompson Jan 19 '16 at 19:40
  • @Olaf I wonder why I have never seen a single question here on SO yet where the problem was a casted malloc(). It is clearly a matter of style, you yourself are a big advocate of standard conformity, which this clearly is. – Ctx Jan 19 '16 at 19:40
  • @KeithThompson So you completely deny the answer of "Ron Burk" on the linked site, which has 187 upvotes, too? This is not quite fair I think. – Ctx Jan 19 '16 at 19:43
  • @Ctx: I prefer clean and clear code. Every unnecessary cast is a potential problem, as it suppresses type checking. Don't see that is bad attitude. For standard advocacy (is that even a word? - anyway): I very well use some gcc compiler extensions, iff they enhance code quality and type-safety. E.g. `-fplan9-extensions` are one of my favorite. – too honest for this site Jan 19 '16 at 19:46
  • @Ctx: Ignoring the 52 downvotes of that answer certainly was just coincidence, I assume. Not to mention the votes of the comments. – too honest for this site Jan 19 '16 at 19:48
  • @Ctx: I don't "completely deny" it. I disagree with it. The number of upvotes doesn't alter my opinion. – Keith Thompson Jan 19 '16 at 19:49
  • @Olaf No need for sarkasm, I wanted to show that many people also agree with him. That others disagree is obvious here, isn't it? Ok, for the record: 82 people voted it down. What does that change now? And I am really curious: _Are_ there questions where the problem was a cast of malloc()? – Ctx Jan 19 '16 at 19:51
  • @Ctx: I certainly am not someone to blindly follow the majority. But so far I have not seen a single reason **why** to cast `void *`. But quite some against. The most times brought up "reason" "compatibility with C++" is nonsense. If you restrict to the common subset which is _really_ identical, you will have a hard time writing any non-trivial program. People who eat burgers in Italy, France, Germany, Japan, Greece or many other coutries might think different. – too honest for this site Jan 19 '16 at 20:00
  • @Olaf It is nonsense in terms of the programming language semantics, of course. As is to write brackets in `(a && b) || (c && d)`. Still at some places and for some people it may enhance readability. Same is with this malloc cast, it is valid and a matter of style. And unless somebody can show up that it caused relevant trouble in _real life situations_ (are there questions here on SO?) I think it is unappropriate to recite it constantly every time someone casts malloc. – Ctx Jan 19 '16 at 20:06
  • @ctx: How does a cast enhance readability, how do the parentesis reduce code quality? Maybe I'm getting old, but I prefer quality over style - even more, as it does not contribute to readability. Anyway, you have the final word. – too honest for this site Jan 19 '16 at 20:14
  • @Olaf Readability is subjective. I could show examples where it would enhance readability to _me_ (i.e. when it is not immediately clear from the context for _which_ datatype space is alloced), but I spare this to you, since it would be easy to reject that, as it is with most style-questions. I would have loved to see some (non-artificial) examples where the cast has hurt (i.e. was the cause of a bug or at least shadowed it) to give at least _some_ justification to this whole issue, but I think it simply isn't the case. Remember: _I don't like it_ is not the same as _Don't do it_ – Ctx Jan 19 '16 at 20:24

1 Answers1

7

In your code, by saying

word = str1;
  1. you're overwriting the malloc()-ed pointer
  2. creating memory leak.

Later , by calling free() on word, you're invoking undefined behavior, as the pointer is no longer returned by a malloc() or family of function.

Solution: You should use strcpy() to copy the content of a string.

That said,

  1. Please see this discussion on why not to cast the return value of malloc() and family in C..
  2. int main() should be int main(void) at least, to conform to the standard.
Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/101137/discussion-on-answer-by-sourav-ghosh-cant-free-memory-after-char). – George Stocker Jan 19 '16 at 23:16