LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   segmentation fault..... (https://www.linuxquestions.org/questions/programming-9/segmentation-fault-681920/)

sumitshining 11-08-2008 01:12 AM

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.......

paulsm4 11-08-2008 01:25 AM

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?

sumitshining 11-08-2008 11:26 AM

Quote:

Originally Posted by paulsm4 (Post 3335136)
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);
....
..

jiml8 11-08-2008 11:34 AM

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.

sumitshining 11-09-2008 12:14 AM

Quote:

Originally Posted by jiml8 (Post 3335444)
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"....

jiml8 11-09-2008 12:24 AM

Try

str = &t;

though I am not sure this is what you want.

sumitshining 11-09-2008 12:34 AM

Quote:

Originally Posted by jiml8 (Post 3335870)
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.....

paulsm4 11-09-2008 12:31 PM

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

sumitshining 11-10-2008 11:47 AM

Quote:

Originally Posted by paulsm4 (Post 3336274)
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...!!!

paulsm4 11-10-2008 02:48 PM

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


All times are GMT -5. The time now is 09:17 PM.