LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 03-20-2010, 05:47 PM   #1
golmschenk
Member
 
Registered: Nov 2009
Posts: 144

Rep: Reputation: 15
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!
 
Old 03-20-2010, 05:53 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by golmschenk View Post
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.
 
Old 03-20-2010, 05:56 PM   #3
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,363
Blog Entries: 52

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
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.
 
Old 03-21-2010, 03:21 PM   #4
golmschenk
Member
 
Registered: Nov 2009
Posts: 144

Original Poster
Rep: Reputation: 15
Great, that helps a bunch. Thanks!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
malloc anoosh Programming 1 03-15-2006 04:41 PM
malloc eagle683 Programming 6 05-22-2005 02:40 PM
malloc() vijeesh_ep Programming 4 08-25-2004 03:50 PM
strcpy causing segfault jpbarto Programming 17 04-07-2004 09:40 PM
malloc/free and segfault - advanced question iTux Programming 3 12-10-2003 04:51 PM


All times are GMT -5. The time now is 03:07 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration