Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 14th, 2005, 7:51 AM   #1
conlau
Newbie
 
Join Date: Mar 2005
Posts: 3
Rep Power: 0 conlau is on a distinguished road
Segmentation Fault with malloc and free

Hi all,

probably I`m missing out something very basic here, but I`m having problems with using malloc and free under c. I shrinked the affected part of my code to the following.

#include <stdlib.h>

int main(){

char* a;
a = (char *) malloc(25);
a = "test";
printf("%s\n",a);
free(a);
}

It compiles fine, but when I run the code I get a segmentation fault after it prints out "test". I`m running on a linux os btw.

Any help would be greatly appreciated.

Regards
conlau is offline   Reply With Quote
Old Mar 14th, 2005, 12:17 PM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
You want:
strcpy(a, "test");
printf("%s\n", *a);
(I think).
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Mar 14th, 2005, 12:33 PM   #3
Mad_guy
Hobbyist Programmer
 
Mad_guy's Avatar
 
Join Date: Oct 2004
Location: Sandstorm, Techno Club
Posts: 239
Rep Power: 4 Mad_guy is on a distinguished road
Send a message via AIM to Mad_guy Send a message via MSN to Mad_guy
Urg, this happens to me all the time. DMA is frusterating, and Segmentation violations are lame. But it gets easier.

#include <stdio.h>
#include <malloc.h>

int main(){

char blah[] = "test", *a = (char *) malloc(strlen(blah));
strcpy(a,blah);
printf("%s\n",a);
getchar();
free(a);
}

Also, Ooble, once you've set data at some allocated memory (whethere you're using memset(), or strcpy() or integers or floats, it's all the same) you NEVER refer to it as a pointer. Because there's then actually data at the newly allocated memory.

Go ahead, try to refer to a as a pointer in that printf() statement and it'll STILL die of segmentation violation. I've learned that on my own from too many segementation faults.

I would also like to note about DMA, this: when you're doing DMA, in every type of memory you're allocating like floats or doubles or longs or ints or whatever, you always use a sizeof() statement in the malloc() statement.

In other words:
int *a = (int *)malloc(4); //WRONG
int *a = (int *)malloc(4 * sizeof(int)); //CORRECT
char *a = (char *)malloc(strlen(stuff) * sizeof(char)); //WRONG
char *a = (char *)malloc(strlen(stuff)); //CORRECT

But why? Simple: no matter what platform you're working on, chars are always one byte. ALWAYS. By doing * sizeof(char) you're just wasting system resources. However, on other platforms, ints and floats and longs and such can be a lot more bytes. Normally ints are 4 and I believe longs are 8.

Just thought you should know.


PS: If you ever want to learn to do REALLY cool stuff with Dynamic Memory Allocation, look up Buffer Overflows.

Last edited by Mad_guy; Mar 14th, 2005 at 1:15 PM.
Mad_guy is offline   Reply With Quote
Old Mar 14th, 2005, 12:42 PM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Knew it. I just play with it until it works, usually.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Mar 14th, 2005, 1:14 PM   #5
Mad_guy
Hobbyist Programmer
 
Mad_guy's Avatar
 
Join Date: Oct 2004
Location: Sandstorm, Techno Club
Posts: 239
Rep Power: 4 Mad_guy is on a distinguished road
Send a message via AIM to Mad_guy Send a message via MSN to Mad_guy
Quote:
Originally Posted by Ooble
Knew it. I just play with it until it works, usually.
I normally just try to get it right the first time, because segmentation faults suck anus, and they're not my friend.
Mad_guy is offline   Reply With Quote
Old Mar 14th, 2005, 1:20 PM   #6
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
i started using valgrind and gdb more now i really can't afford to be stuck on segfaults, give 'm a try.
__________________
naked pictures of you | PFO F@H stats
Dizzutch is offline   Reply With Quote
Old Mar 21st, 2005, 9:00 AM   #7
conlau
Newbie
 
Join Date: Mar 2005
Posts: 3
Rep Power: 0 conlau is on a distinguished road
Hi all,

many thanks for all your help!

Just noticed that the following modification does not produce a seg fault -

#include <stdlib.h>

int main(){
char* test;
if ( (test = malloc(25)) == NULL){
printf("Error\n");
exit(1);
}
test = "test";

printf ("%s\n",test);
free(test);
}

The only difference here is that the malloc section is checked to see whether a NULL is returned and hence request has failed. I cannot explain why this is not causing any segmentation faults, while the original code is however....

Regards
conlau is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:31 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC