LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 02-15-2006, 11:16 AM   #1
babycorn
LQ Newbie
 
Registered: Feb 2006
Posts: 2

Rep: Reputation: 0
Help needed in segmentation fault


Hi! I am facing problems here... This is the coding that i compile in linux terminal :
#include <stdio.h>
void *mymalloc(size_t request);
int main()
{
char * test;
int i = 0;
test = (char *)mymalloc(sizeof(char) * 6);
if (test == NULL)
{
printf("Malloc failed\n");
exit(1);
}
while(1)
{
test[i] = 'a';
printf("%d bytes written to memory\n", i+1);
i++;
}
return 0;
}
void * mymalloc(size_t request)
{
return sbrk(request);
}

and this is my output:
2565 bytes written to memory
2566 bytes written to memory
2567 bytes written to memory
2568 bytes written to memory
Segmentation fault

Can anyone explain to me what's actually happenning? If want to improve, how?? Thanks a lot for your help!!!
 
Old 02-15-2006, 11:48 AM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Code:
while(1)
{
test[i] = 'a';
printf("%d bytes written to memory\n", i+1);
i++;
}
This infinite loop is what is causing the seg fault, but I've never seen the sbrk func?
 
Old 02-15-2006, 12:22 PM   #3
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
You're definitely overflowing your buffer by a long shot... you allocate a 6 byte buffer and then dump several hundred bytes into it.
 
Old 02-15-2006, 12:43 PM   #4
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
change
Code:
while(1)
to
Code:
while(i<6)
and please use code tags

edit> also according to the man page sbrk is not defd in c standard..

Last edited by xhi; 02-15-2006 at 12:46 PM.
 
  


Reply



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
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM
Segmentation fault santhosh_o Programming 3 10-26-2004 05:45 AM
Segmentation Fault (What is that?) jlacroix Fedora 6 11-17-2003 08:32 PM
Segmentation Fault redhatnoob Linux - Software 3 11-13-2003 02:11 AM
Segmentation fault compjinx Linux - Newbie 4 11-09-2003 04:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:31 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration