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.
are there any difference between (vc++ and g++) or (window and linux)?
any sugesstion to solve this problem..? plz help
There are certainly differences between the two environments. The C/C++ standards allow for varieties in implementation.
When a segmentation fault occurs, it usually implies a memory problem (i.e. using an un-initialized pointer, freeing a pointer twice, memory overflow, etc). I'd suggest that you run the program under the debugger and see where the segfault occurs. The name of the GNU debugger is "gdb." You can run it like "gdb <prog_name>". Make sure you compile your program with "-g" so debugging symbols will be compiled in.
Debugger is your friend. It may seem like a difficult tool to use at first, but once you get the hang of it, you will find gdb to be an invaluable tool that helps you find and fix problems in your code.
VC++ is, in general, a lot more lax and a lot less conforming than GCC.
To use "gdb", you must compile/link with "-g".
Here's a quick "cheat sheet" for some of the main gdb commands:
Code:
gdb myprog # Start program in debugger
b main # Set breakpoint in function "main()"
r abc # "run" program with cmd-line arguments "abc"
next # "n": Single step (over functions)
step # "s": Single step (into functions)
where # Get stack traceback of current location in program
p i # Print the value of variable "i"
del 1 # Delete breakpoint #1 (your "b main")
h # Gdb "help"
q # "Quit": Exit debugger
And here's a sample program with one illustration of how different platforms can treat arrays and pointer storage allocation differently (which might be precisely your problem):
Code:
/*
* Test SEGV
*
* 1) Microsoft compile/run:
* ---------------------
* - cl x.cpp => OK
* - x 123 =>
* Original strings: s1= abc, s2= abc...
* Updated strings: s1= 123, s2= 123...
*
* - x 123456 =>
* Original strings: s1= abc, s2= abc...
* <= FIRST ONE OK, SECOND DIES IMMEDIATELY
*
* 2) G++ compile/debug:
* -----------------
* - g++ -g -Wall -pedantic -o x x.cpp
* <= BE SURE TO INCLUDE "-g" SWITCH, FOR DEBUG INFO
* - gdb x
* (gdb) run
* <= THIS WILL PROMPT FOR USAGE
* (gdb) r 123
* Starting program: /home/paulsm/proj/temp/x 123
* Original strings: s1= abc, s2= abc...
* Program received signal SIGSEGV, Segmentation fault.
* 0x4017f3d6 in strcpy () from /lib/tls/libc.so.6 *
* <= THIS DIES...
* (gdb) where
* <= "WHERE" TELLS US OUR CURRENT TRACEBACK
* #0 0x4017f3d6 in strcpy () from /lib/tls/libc.so.6
* #1 0x08048518 in main (argc=2, argv=0xbffff3f4) at x.cpp:42
* (gdb) list
* <= "LIST" PRINTS THE CURRENT SOURCE
* 42 strcpy (s1, argv[1]);
* 43 strcpy (s2, argv[1]);
* 44 printf ("Updated strings: s1= %s, s2= %s...\n", s1, s2);
* 45
* (gdb) break main
* Breakpoint 1 at 0x80484c8: file x.cpp, line 34.
* <= "B MAIN" SETS A BREAKPOINT AT THE VERY BEGINNING OF THE PROGRAM
* (gdb) r 123
* <= START OVER
* Breakpoint 1, main (argc=2, argv=0xbffff3f4) at x.cpp:34
* 34 if (argc != 2)
* (gdb) n
* 41 printf ("Original strings: s1= %s, s2= %s...\n", s1, s2);
* (gdb) n
* Original strings: s1= abc, s2= abc...
* 42 strcpy (s1, argv[1]);
* <= "NEXT" SINGLE-STEPS THROUGH A SOURCE LINE AT A TIME
* (gdb) p s1
* $1 = 0x8048678 "abc"
* (gdb) print argv[1]
* $2 = 0xbffff5b0 "123"
* <= "PRINT" PRINTS THE CURRENT VALUE/TYPE OF A VARIABLE
*/
#include <stdio.h>
#include <string.h>
int
main (int argc, char *argv[])
{
static char *s1 = "abc";
static char s2[] = "abc";
// Check cmd-line
if (argc != 2)
{
printf ("SAMPLE USAGE: x 123!\n");
return 1;
}
// Copy cmd-line into static string
printf ("Original strings: s1= %s, s2= %s...\n", s1, s2);
strcpy (s1, argv[1]);
strcpy (s2, argv[1]);
printf ("Updated strings: s1= %s, s2= %s...\n", s1, s2);
// Done
return 0;
}
Usually computers don't have much trouble dividing. Do you know what the values of b and Kappa[] were immediately prior to the crash. My suspicion is that global Kappa[] is hidden by a local scalar version and thus not properly set up at the point of execution.
Usually computers don't have much trouble dividing. Do you know what the values of b and Kappa[] were immediately prior to the crash. My suspicion is that global Kappa[] is hidden by a local scalar version and thus not properly set up at the point of execution.
I would also make sure all elements of Kappa are properly initialized. How did you do the initialization?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.