LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-16-2014, 11:27 AM   #1
sfasfeh77
LQ Newbie
 
Registered: Sep 2014
Posts: 3

Rep: Reputation: Disabled
Smile Segmentation Fault


Hello There,

I have a segmentation Fault when I ran my C program on Ubuntu 14.04. I have a probelm when I try to copy string to a pointer.

How Can I fix this probelm.

thanks
 
Old 09-16-2014, 12:19 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
You could start by posting applicable part of the code.
 
Old 09-16-2014, 01:26 PM   #3
sfasfeh77
LQ Newbie
 
Registered: Sep 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
thanks for reply,

this is a prt of my code, I debugged it using gdb and it stuck at strcpy(tempdone,done);


tempprllflag = (char *) malloc(sizeof (char) *100);
program[0].PRLLFLAG="YES";
tempdone = (char *) malloc(sizeof (char) *100);
program[0].DONE="1";

if(flag1==1 || flag2==1 || flag3==1)
{
prllflag="NO";
strcpy(tempprllflag,prllflag);
done="0";
strcpy(tempdone,done);
NOcounter++;
YEScounter=opcodecounter-NOcounter+1;
}

else
{
prllflag="YES";
strcpy(tempprllflag,prllflag);

done="1";
strcpy(tempdone,done);
}
program[count-1].PRLLFLAG = tempprllflag;
program[count-1].DONE = tempdone;
 
Old 09-16-2014, 01:45 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
How are your variables being declared?
Could you reduce your code into a stand-alone program that reproduces the problem?

For example, this:
Code:
#include <stdlib.h>
#include <string.h>

int main(void)
{
  char *done;
  char *prllflag;
  char *tempprllflag;
  char *tempdone;
  int flag1, flag2, flag3;
  flag1 = 0;
  flag2 = 0;
  flag3 = 1;

  prllflag = (char *) malloc(sizeof (char) *100);
  done = (char *) malloc(sizeof (char) *100);
  tempprllflag = (char *) malloc(sizeof (char) *100);
  tempdone = (char *) malloc(sizeof (char) *100);

  if(flag1==1 || flag2==1 || flag3==1)
  {
    prllflag="NO";
    strcpy(tempprllflag,prllflag);
    done="0";
    strcpy(tempdone,done);
  }
  else
  {
    done="1";
    strcpy(tempdone,done);	
  }

  return 0;
}
runs without issue, so what is different about yours?
 
Old 09-16-2014, 02:12 PM   #5
sfasfeh77
LQ Newbie
 
Registered: Sep 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
I read from file, number of lines around 500,000, when I tried to read them in wnidow, each one is 1500 line, and when wnidow numbers reach to the end of file, I have taht segmentation fault at this line strcpy(tempdone,done);
while(((c = fgetc(myfile)) != EOF))
{
while(opcodecounter<1500)
{
fgets(line, sizeof line, myfile) !=NULL;

///////////Instruction Address///////////
tempaddress = (char *) malloc(sizeof(char) * 100);
insaddress = strtok(line, "| \t ");
if(insaddress != NULL)
{
count++;
runcounter++;
strcpy(tempaddress, insaddress);
}

else
{
strcpy(insaddress, "");
}

prllflag = (char *) malloc(sizeof (char) *100);
done = (char *) malloc(sizeof (char) *100);
tempprllflag = (char *) malloc(sizeof (char) *100);
tempdone = (char *) malloc(sizeof (char) *100);

if(flag1==1 || flag2==1 || flag3==1)
{
prllflag="NO";
strcpy(tempprllflag,prllflag);
done="0";
strcpy(tempdone,done);
}
else
{
done="1";
strcpy(tempdone,done);
}

return 0;
}
 
Old 09-16-2014, 03:03 PM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Please post a complete, stand-alone program that reproduces the error. You still haven't provided any variable declarations, and the code as-posted will not even compile, much less run, and because none of the mallocs are free'd it will also have a massive memory leak (not to mention the infinite loop since opcodecounter is never incremented).
 
Old 09-16-2014, 03:06 PM   #7
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,774
Blog Entries: 1

Rep: Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339
If you can, put your code up on github in a repo.

And then provide the history of your shell, to show us what you are attempting to do. And version information about your glibc and compiler might help as well.

Providing a small code section when you are getting segfaults, and unable to troubleshoot, isn't going to help anyone help you.

Last edited by szboardstretcher; 09-16-2014 at 03:08 PM.
 
  


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
Segmentation Fault? iBasher Programming 3 12-23-2010 02:56 PM
Why segmentation fault? Superdude Programming 15 12-08-2006 04:01 AM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 10:34 AM
Segmentation fault ( C ) wenta0 Programming 4 12-22-2004 06:10 AM
Segmentation Fault (What is that?) jlacroix Fedora 6 11-17-2003 09:32 PM


All times are GMT -5. The time now is 10:46 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration