LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 11-08-2008, 02:12 AM   #1
sumitshining
LQ Newbie
 
Registered: Oct 2008
Posts: 29

Rep: Reputation: 15
segmentation fault.....


i made a lzw compression program......when i execute it it gives segmentation fault......sum1 told me it might be d problem of memory area of user and kernel.......n told me to use debugger ........can sum1 help me learn how is debugger used...or any other alternative.......
 
Old 11-08-2008, 02:25 AM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
I made an lzw compression program......
Perhaps you wrote your own code, perhaps you compiled some existing code. Going out on a limb, maybe the code is written in C/C++ and maybe you compiled it on Linux with the GCC compiler.
Quote:
When I execute it, it gives a segmentation fault......
Too bad. It sounds like the program needs to be debugged.
Quote:
Someone told me it might be a problem of memory area of user and kernel.......and told me to use debugger ........
"Segmentation violations" are always due to illegal accessing user space memory; they shouldn't have anything directly to do with the kernel.

The suggestion of "using a debugger" is excellent advice.
Quote:
Can someone help me learn how is debugger used...or any other alternative.......
You can easily Google for many excellent tutorials, for example:
http://heather.cs.ucdavis.edu/~matlo...age/Debug.html

PS:
Is it really that difficult to spell out words, write in complete sentences, or use capitalization and punctuation?

Last edited by paulsm4; 11-08-2008 at 02:31 AM.
 
Old 11-08-2008, 12:26 PM   #3
sumitshining
LQ Newbie
 
Registered: Oct 2008
Posts: 29

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by paulsm4 View Post
Perhaps you wrote your own code, perhaps you compiled some existing code. Going out on a limb, maybe the code is written in C/C++ and maybe you compiled it on Linux with the GCC compiler.

Too bad. It sounds like the program needs to be debugged.
Well i'm writing the part of the code which I think might be creating problem.....Please have a look at this.....I'm reading character by character from a file stream named in....


...
char *str;
char t;
t=fgetc(in);
strcat(str,&t);
....
..

Last edited by sumitshining; 11-08-2008 at 12:27 PM.
 
Old 11-08-2008, 12:34 PM   #4
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 115Reputation: 115
You never allocated the string that you are pointing to with str.

Either:

char *str;
char t;
str = calloc(1,numberofbytesneeded);
t=fgetc(in);
strcat(str,&t);

or

char str[numberofbytesneeded];
char t;
t=fgetc(in);
strcat(&str[0],&t);

I used calloc because you probably need a null termination in your string. If you use the second form I gave, you'll have to either write the 0x0 at the end or use memset to clear str before using it.

edit: Also, many people (myself included) are far more likely to respond when you employ correct grammar and punctuation.
 
Old 11-09-2008, 01:14 AM   #5
sumitshining
LQ Newbie
 
Registered: Oct 2008
Posts: 29

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by jiml8 View Post
You never allocated the string that you are pointing to with str.

Either:

char *str;
char t;
str = calloc(1,numberofbytesneeded);
t=fgetc(in);
strcat(str,&t);

or

char str[numberofbytesneeded];
char t;
t=fgetc(in);
strcat(&str[0],&t);

I used calloc because you probably need a null termination in your string. If you use the second form I gave, you'll have to either write the 0x0 at the end or use memset to clear str before using it.

edit: Also, many people (myself included) are far more likely to respond when you employ correct grammar and punctuation.
Thanks for the reply Sir!...but one thing more I would like to ask.....Can I use it this way......

...
...
char *atr;
char t;
t=fgetc(in);
*str=t;
..
..
I used this one ...and tried in TURBO C....It gave it this way....NULL POINTER ASSIGNEMENT....not at this line no...but at the end of main()........And GCC still gave "segmentation fault"....
 
Old 11-09-2008, 01:24 AM   #6
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 115Reputation: 115
Try

str = &t;

though I am not sure this is what you want.

Last edited by jiml8; 11-09-2008 at 01:26 AM.
 
Old 11-09-2008, 01:34 AM   #7
sumitshining
LQ Newbie
 
Registered: Oct 2008
Posts: 29

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by jiml8 View Post
Try

str = &t;

though I am not sure this is what you want.
I have already tried this.....Actually before that I did this one.....but all in vain.....

Last edited by sumitshining; 11-09-2008 at 01:38 AM.
 
Old 11-09-2008, 01:31 PM   #8
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Problem 1: if you're going to use a string, then you need to allocate *space* for the string. Jiml8 suggested a couple of great alternatives.

Problem 2: If you're going to use "stcat", "strcpy" and friends, you should really use *two strings* (not a string and a character).

Code:
  /* Bad */
  char *str;
  char t;
  t=fgetc(in);
  strcat(str,&t);
Code:
#include <stdio.h>
#include <string.h>

#define MAX_STRING 80

int
main (int argc, char *argv[])
{
  /* Declare variables for our strings, an input character, and an index */
  char str1[MAX_STRING], str2[MAX_STRING];
  int c;
  int i = 0;

  /* Read a character at a time, until "<ENTER>", or until string would overflow */
  while ( ((c = getchar ()) != '\n') && (i < MAX_STRING) )
  {
    str1[i++] = c;
  }

  /* Don't forget to null-terminate the string */
  str1[i] = '\0';

  /* Print it out */
  printf ("str1= %s\n", str1);

  /* Copy it to another string */
  strncpy (str2, str1, MAX_STRING);
  printf ("str2= %s\n", str2);
  return 0;
}
Quote:
$ gcc -o tst tst.c
$ ./tst
abc
str1= abc
str2= abc
'Hope that helps .. PSM

Last edited by paulsm4; 11-09-2008 at 01:34 PM.
 
Old 11-10-2008, 12:47 PM   #9
sumitshining
LQ Newbie
 
Registered: Oct 2008
Posts: 29

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by paulsm4 View Post
Problem 1: if you're going to use a string, then you need to allocate *space* for the string. Jiml8 suggested a couple of great alternatives.

Problem 2: If you're going to use "stcat", "strcpy" and friends, you should really use *two strings* (not a string and a character).
'Hope that helps .. PSM
I'm really thankful for your help...Actually I was woking on LZW data compression....But now I need one more help...I want to know how to implement this on images...I have already retreived the image using GTK.....Please help...!!!
 
Old 11-10-2008, 03:48 PM   #10
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

1. What exactly are you trying to do?

2. How far have you gotten?

3. What's going wrong at that point?

SUGGESTIONS:
* Create a new thread
* Try to be as explicit as you can about each of these three questions.

Good luck .. PSM

PS:
One more suggestion:
* If you're on Linux, you're programming in C/C++, and you're using gcc or g++, then...

* Compile and link with "-g" (full debugging on), and ...

* run your program from gdb (the command-line GCC debugger)

* when your program crashes, do a "where" (or, equivalently, "bt")
<= This will give you a "stack traceback" ...
a *vital* clue as to where the error is occurring, and what
should be done to debug it

* Cut/paste this "stack traceback" into your post

Again - good luck!

http://www.cs.cmu.edu/~gilpin/tutorial/
http://heather.cs.ucdavis.edu/~matlo...age/Debug.html
 
  


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
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika Suse/Novell 2 12-02-2005 10:34 AM
Segmentation Fault rupesh_pulikool Linux - Software 7 01-07-2005 06:55 AM
Segmentation fault ( C ) wenta0 Programming 4 12-22-2004 06:10 AM
Segmentation fault marios_auth Programming 1 06-16-2004 09:16 AM
C Segmentation Fault fatman Programming 20 04-02-2003 06:16 PM


All times are GMT -5. The time now is 01:17 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