Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
03-20-2010, 05:47 PM
|
#1
|
|
Member
Registered: Nov 2009
Posts: 144
Rep:
|
C - Malloc causing segfault
Code:
#include <stdio.h>
#include <stdlib.h>
#include "mm_public.h"
int main (){
struct timeval time_start, time_end;
int j;
void **Mem_Stuff;
int i;
/* start timer */
fprintf (stderr, "Good Thus Far 1\n");
j = gettimeofday (&time_start, (void *)NULL);
fprintf (stderr, "Good Thus Far 2\n");
for (i=0;i<10000;i++){
fprintf (stderr, "Good Thus Far 3\n");
Mem_Stuff[i] = malloc(64);
fprintf (stderr, "Good Thus Far 4\n");
}
for (i=0;i<10000;i++){
free (Mem_Stuff[i]);
}
j = gettimeofday (&time_end, (void *)NULL);
fprintf (stderr, "Time taken = %f msec\n",comp_time (time_start, time_end)/1000.0);
return 0;
}
Output gets to "Good Thus Far 3" then segfaults. Not sure why. Any suggestions? Thanks!
|
|
|
|
03-20-2010, 05:53 PM
|
#2
|
|
Senior Member
Registered: May 2005
Posts: 4,386
|
Quote:
Originally Posted by golmschenk
Code:
#include <stdio.h>
#include <stdlib.h>
#include "mm_public.h"
int main (){
struct timeval time_start, time_end;
int j;
void **Mem_Stuff;
int i;
/* start timer */
fprintf (stderr, "Good Thus Far 1\n");
j = gettimeofday (&time_start, (void *)NULL);
fprintf (stderr, "Good Thus Far 2\n");
for (i=0;i<10000;i++){
fprintf (stderr, "Good Thus Far 3\n");
Mem_Stuff[i] = malloc(64);
fprintf (stderr, "Good Thus Far 4\n");
}
for (i=0;i<10000;i++){
free (Mem_Stuff[i]);
}
j = gettimeofday (&time_end, (void *)NULL);
fprintf (stderr, "Time taken = %f msec\n",comp_time (time_start, time_end)/1000.0);
return 0;
}
Output gets to "Good Thus Far 3" then segfaults. Not sure why. Any suggestions? Thanks!
|
And why do you think it's 'malloc' which causes segmentation fault and not the assignment ?
Modify your code to become, say
Code:
fprintf (stderr, "Good Thus Far 3\n");
void *tmp = malloc(64);
fprintf (stderr, "Good Thus Far 4\n");
Mem_Stuff[i] = tmp;
fprintf (stderr, "Good Thus Far 5\n");
and recheck.
|
|
|
|
03-20-2010, 05:56 PM
|
#3
|
|
Senior Member
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Squeeze (Fluxbox WM)
Posts: 1,357
|
The Mem_Stuff that you have declared is a 'pointer to a pointer to something'. You are treating it as a 'array of pointers', which is also fine. However, you have not made space for the array, so when you attempt to assign to one of the pointers, you get the fault.
You need to first allocate space for the array itself, before allocating the spaces that the pointer elements are going to point to.
=== edit - see above post! ===
Last edited by neonsignal; 03-20-2010 at 05:58 PM.
|
|
|
|
03-21-2010, 03:21 PM
|
#4
|
|
Member
Registered: Nov 2009
Posts: 144
Original Poster
Rep:
|
Great, that helps a bunch. Thanks!
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 11:07 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|