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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
11-08-2008, 01:12 AM
|
#1
|
|
LQ Newbie
Registered: Oct 2008
Posts: 29
Rep:
|
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.......
|
|
|
|
11-08-2008, 01:25 AM
|
#2
|
|
Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,861
Rep: 
|
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 01:31 AM.
|
|
|
|
11-08-2008, 11:26 AM
|
#3
|
|
LQ Newbie
Registered: Oct 2008
Posts: 29
Original Poster
Rep:
|
Quote:
Originally Posted by paulsm4
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 11:27 AM.
|
|
|
|
11-08-2008, 11:34 AM
|
#4
|
|
Senior Member
Registered: Sep 2003
Posts: 3,171
Rep: 
|
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.
|
|
|
|
11-09-2008, 12:14 AM
|
#5
|
|
LQ Newbie
Registered: Oct 2008
Posts: 29
Original Poster
Rep:
|
Quote:
Originally Posted by jiml8
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"....
|
|
|
|
11-09-2008, 12:24 AM
|
#6
|
|
Senior Member
Registered: Sep 2003
Posts: 3,171
Rep: 
|
Try
str = &t;
though I am not sure this is what you want.
Last edited by jiml8; 11-09-2008 at 12:26 AM.
|
|
|
|
11-09-2008, 12:34 AM
|
#7
|
|
LQ Newbie
Registered: Oct 2008
Posts: 29
Original Poster
Rep:
|
Quote:
Originally Posted by jiml8
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 12:38 AM.
|
|
|
|
11-09-2008, 12:31 PM
|
#8
|
|
Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,861
Rep: 
|
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 12:34 PM.
|
|
|
|
11-10-2008, 11:47 AM
|
#9
|
|
LQ Newbie
Registered: Oct 2008
Posts: 29
Original Poster
Rep:
|
Quote:
Originally Posted by paulsm4
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...!!!
|
|
|
|
11-10-2008, 02:48 PM
|
#10
|
|
Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,861
Rep: 
|
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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 08:24 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|