![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Posts: 3
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
You want:
strcpy(a, "test");
printf("%s\n", *a); |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
|
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. |
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Knew it. I just play with it until it works, usually.
|
|
|
|
|
|
#5 | |
|
Hobbyist Programmer
|
Quote:
|
|
|
|
|
|
|
#6 |
|
Professional Programmer
|
i started using valgrind and gdb more now i really can't afford to be stuck on segfaults, give 'm a try.
|
|
|
|
|
|
#7 |
|
Newbie
Join Date: Mar 2005
Posts: 3
Rep Power: 0
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|