Author |
Message |
sunult.. #1 / 17
|
 Intersting malloc() / free() question
Hi, Here is an interesting malloc() / free() question main() { char *s1, *s2, *p; s1 = (char *) malloc(100); if (s1 == NULL) { return; } s1 = "this_is_a_string"; s2 = s1; s2[4] = '\0'; p = (s2 + 5); free(p); Quote: }
The code core dumps with a "Bus Error" when you call free(). Why? Sent via Deja.com http://www.*-*-*.com/ Before you buy.
|
Tue, 01 Oct 2002 03:00:00 GMT |
|
 |
Michael P. Souli #2 / 17
|
 Intersting malloc() / free() question
Quote:
>main() >{ > char *s1, *s2, *p; > s1 = (char *) malloc(100); > if (s1 == NULL) { > return; > } > s1 = "this_is_a_string"; > s2 = s1; > s2[4] = '\0'; > p = (s2 + 5); > free(p); >} >The code core dumps with a "Bus Error" when you call free(). Why?
Why not?? You didn't allocate the memory at the address pointed to by s1 + 5. You have no idea how the memory is provided by the OS, you can't simply assume that you can free from another position. If you want to shrink the allocated block of memory, use realloc to the use size. Mike
|
Tue, 01 Oct 2002 03:00:00 GMT |
|
 |
Richard Heathfiel #3 / 17
|
 Intersting malloc() / free() question
Quote:
> Hi, > Here is an interesting malloc() / free() question > main() > { > char *s1, *s2, *p; > s1 = (char *) malloc(100); > if (s1 == NULL) { > return; > } > s1 = "this_is_a_string"; > s2 = s1; > s2[4] = '\0'; > p = (s2 + 5); > free(p); > } > The code core dumps with a "Bus Error" when you call free(). Why?
I'm afraid it's not really that interesting at all. You start off with undefined behaviour, meaning all bets are off straight away. Why? Because you called malloc without having a prototype in scope, and then stopped your compiler from warning you about it by casting malloc's return value. Don't you read comp.lang.c? This seems to come up several times a day. Why do we have to keep saying it? #include <stdlib.h> and don't cast malloc. Next, having (not very) carefully assigned s1 the return value of malloc, you then proceed to completely lose that value, choosing instead to assign s1 the value of the base address of a string instead. Then you assign a spare pointer to the same value, and then you try to write into space you don't own (s2[4] = '\0'); Finally, you point p into that space you don't own, and try to free it. I hope this is a troll because, if it isn't, I fear for your work colleagues and your customers when you eventually burst into the commercial programming world. What C text are you using? Have you actually opened it yet? If not, consider doing so Real Soon Now. -- Richard Heathfield "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999. C FAQ: http://www.eskimo.com/~scs/C-faq/top.html 29 K&R Answers: http://users.powernet.co.uk/eton/kandr2/index.html (68 to go)
|
Tue, 01 Oct 2002 03:00:00 GMT |
|
 |
Dan #4 / 17
|
 Intersting malloc() / free() question
Quote:
> Hi, > Here is an interesting malloc() / free() question > main() > { > char *s1, *s2, *p; > s1 = (char *) malloc(100); > if (s1 == NULL) { > return; > } > s1 = "this_is_a_string"; > s2 = s1; > s2[4] = '\0'; > p = (s2 + 5); > free(p); > } > The code core dumps with a "Bus Error" when you call free(). Why?
OK...this one just HAS to be a troll. He's pushed just about every button to be found here. Dan
|
Tue, 01 Oct 2002 03:00:00 GMT |
|
 |
Erik Max Franci #5 / 17
|
 Intersting malloc() / free() question
Quote:
> The code core dumps with a "Bus Error" when you call free(). Why?
Actually, your compiler is clearly substandard. A good compiler would have erased your source files and emailed your boss. --
Alcyone Systems | web http://www.alcyone.com/max/ | q3a Product San Jose, CA | languages en, eo | icbm 37 20 07 N 121 53 38 W USA | 969.916 Ms p.L. | 261 days left | &tSftDotIotE __ / \ Grub first, then ethics. \__/ Bertolt Brecht
|
Tue, 01 Oct 2002 03:00:00 GMT |
|
 |
-hs- #6 / 17
|
 Intersting malloc() / free() question
Quote: >Hi, > Here is an interesting malloc() / free() question
#include <stdlib.h> Quote: >main()
int main(void) Quote: >{ > char *s1, *s2, *p; > s1 = (char *) malloc(100);
s1 = malloc(100); Quote: > if (s1 == NULL) { > return;
return EXIT_FAILURE; Quote: > } > s1 = "this_is_a_string";
You have changed s1. You are now unable to free s1. Maybe you wanted strcpy(s1,"this_is_a_string"); /* include <string.h> */ Quote: > s2 = s1; > s2[4] = '\0';
You can't. s2 is pointing to s1 which is a literal. Modifying a literal produces an undefined behaviour. Quote: > p = (s2 + 5); > free(p);
You want to free any pointer ? Try a random one... Quote: >} >The code core dumps with a "Bus Error" when you call free(). Why?
Simply because the parameter of free() must be the exact value returned by malloc(). If you had written char *const s1 = malloc(100); you would have an error here Quote: > s1 = "this_is_a_string";
"can't modify a constant object" Finally, call free(s1); when finished. -- -hs- "Stove" CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html ISO-C Library: http://www.dinkum.com/htm_cl "Really? When run on my machine, a printed copy of the C FAQ leaps from the monitor and whacks me over the head.." -- Chris Mears CLC
|
Wed, 02 Oct 2002 03:00:00 GMT |
|
 |
Eric Amic #7 / 17
|
 Intersting malloc() / free() question
Quote:
> Here is an interesting malloc() / free() question > main() > { > char *s1, *s2, *p; > s1 = (char *) malloc(100);
Include <stdlib.h> and skip the cast. Quote: > s1 = "this_is_a_string";
You've blithely thrown away the only pointer to that memory you just allocated. Something tells me you wanted to use strcpy() instead. Quote: > s2 = s1; > s2[4] = '\0';
Attempting to modify a string literal invokes undefined behavior. Quote: > p = (s2 + 5); > free(p);
The value passed to free() is neither NULL nor one produced by malloc() or its kin, so undefined behavior results. -- Eric Amick Columbia, MD
|
Wed, 02 Oct 2002 03:00:00 GMT |
|
 |
Raghavendra Nagara #8 / 17
|
 Intersting malloc() / free() question
Hello guys, you have been beating around the bush and not attempting to answer the question. the question was "why is it giving bus error", the question was not to evaluate his program. We know that bus error occurs when there is memory mis-alignment. he is trying to free the memory which doesn't start from a multiple of word size. if you can run the same program by just changing, p = (s2 + 0); or p = (s2 + 4); or p = (s2 + 8); or p = (s2 + 12); it won't give any bus error. -rags Quote:
> Hi, > Here is an interesting malloc() / free() question > main() > { > char *s1, *s2, *p; > s1 = (char *) malloc(100); > if (s1 == NULL) { > return; > } > s1 = "this_is_a_string"; > s2 = s1; > s2[4] = '\0'; > p = (s2 + 5); > free(p); > } > The code core dumps with a "Bus Error" when you call free(). Why? > Sent via Deja.com http://www.deja.com/ > Before you buy.
|
Wed, 02 Oct 2002 03:00:00 GMT |
|
 |
John Winte #9 / 17
|
 Intersting malloc() / free() question
Quote:
>Hi, > Here is an interesting malloc() / free() question >main() >{ > char *s1, *s2, *p; > s1 = (char *) malloc(100);
^^^^^^^^ You forgot to include stdlib.h and this cast masks the diagnostic which the compiler would otherwise be obliged to give you. Never cast the return value from malloc(). This alone could cause your problem but a more likely cause is below. Quote: > if (s1 == NULL) { > return; > } > s1 = "this_is_a_string";
Now you've thrown away your only pointer to the allocated memory. You'll never be able to free it. Presumably you really want something like: strcpy(s1, "this_is_a_string"): (after first including string.h obviously). Quote: > s2 = s1; > s2[4] = '\0'; > p = (s2 + 5); > free(p);
Now even if you hadn't corrupt s1 above, p is a calculated value which doesn't point to the allocated block of memory. The only values you can pass to free() are previous results from malloc() and NULL. If you want to re-size the block of allocated memory, look at realloc(). Quote: >} >The code core dumps with a "Bus Error" when you call free(). Why?
HTH John -- John Winters. Wallingford, Oxon, England. The Linux Emporium - the source for Linux CDs in the UK See http://www.linuxemporium.co.uk/
|
Thu, 03 Oct 2002 03:00:00 GMT |
|
 |
vrml3d.co #10 / 17
|
 Intersting malloc() / free() question
Quote:
> Hi, > Here is an interesting malloc() / free() question > main()
int main() Quote: > { > char *s1, *s2, *p; > s1 = (char *) malloc(100);
the cast is considered bad form by many. Quote: > if (s1 == NULL) { > return; > }
OK, except of course that main should return an int. Quote: > s1 = "this_is_a_string";
s1 is now set to the value of the starting address for the string constant "this_is_a_string". The memory that you malloced is now lost and gone forever. What you most likely really wanted to do was strcpy(s1,"this_is_a_string"); Quote: > s2 = s1;
s2 now points to the same location as s1 Quote: > s2[4] = '\0'; > p = (s2 + 5); > free(p);
even if s2 were properly allocated with malloc, free(p) would be wrong. I don't believe the free function is smart enough to realize that a block must be deleted just because you have supplied a pointer to something that is located within that block. However, it's a moot point, because the memory that you are trying to free is actually on the stack, not the heap. Quote: > } > The code core dumps with a "Bus Error"
Take the subway instead. Quote: >when you call free(). Why?
see above. --Steve
|
Fri, 04 Oct 2002 03:00:00 GMT |
|
 |
vrml3d.co #11 / 17
|
 Intersting malloc() / free() question
Quote: > > p = (s2 + 5); > You just created a memory leak. The address returned by malloc() is now > lost and gone forever. Please read the C FAQ.
Wow, we used the same phrase. Although the memory was really "lost and gone forever" when he assigned the string constant to the pointer. OTOH, I forgot to tell him to #include <stdlib.h>. There were just too many bugs in this. This may be a good patch for the comp.lang.c Ultimate Flamebait Program. --Steve
|
Fri, 04 Oct 2002 03:00:00 GMT |
|
 |
k.. #12 / 17
|
 Intersting malloc() / free() question
Quote: > Hi, > Here is an interesting malloc() / free() question > main() > { > char *s1, *s2, *p; > s1 = (char *) malloc(100); > if (s1 == NULL) { > return; > } > s1 = "this_is_a_string"; > s2 = s1; > s2[4] = '\0'; > p = (s2 + 5); > free(p); > } > The code core dumps with a "Bus Error" when you call free(). Why?
Becuase you're trying to free something (namely, the literal (part) "is_a_string") which you didn't mallocate. On the WAX11, trying to do this will polish your shoes and then set fire to them. -- Chris "well ... only on the WAX11/1830" Dollin C FAQs at: http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.c.html
|
Fri, 04 Oct 2002 03:00:00 GMT |
|
 |
Erik Max Franci #13 / 17
|
 Intersting malloc() / free() question
Quote:
> you have been beating around the bush and not attempting to answer the > question. the question was "why is it giving bus error", the question > was > not to evaluate his program.
Almost all of the responses pointed out what was causing the crash. They also, while they were at it, pointed out some of the multitude of other things that was wrong with his program. --
Alcyone Systems | web http://www.*-*-*.com/ | q3a Product San Jose, CA | languages en, eo | icbm 37 20 07 N 121 53 38 W USA | 970.161 Ms p.L. | 258 days left | &tSftDotIotE __ / \ Whoever contends with the great sheds his own {*filter*}. \__/ Sa'di
|
Fri, 04 Oct 2002 03:00:00 GMT |
|
 |
David Thompso #14 / 17
|
 Intersting malloc() / free() question
Quote:
> [ code evidencing complete nonunderstanding of pointers and malloc ] > > The code core dumps with a "Bus Error" when you call free(). Why? > Actually, your compiler is clearly substandard. A good compiler would > have erased your source files and emailed your boss.
Maybe his compiler (or runtime!) actually posted the code to c.l.c as a more effective way of humiliating him. <G> -dt
|
Sat, 05 Oct 2002 03:00:00 GMT |
|
|