ProgrammingThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.......
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.......
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....
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.
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"....
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;
}
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...!!!
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.