g++,How to find where there is a error? [closed] - c++

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Guys!
I have written a c++ source.
But I find someplace there is a error.
"Segmentation fault: 11"
But I don't know the details about the error and where to fix?
Do you guys knows some commands can show where the error is.
Thanks

With gcc generated code you typically use a gdb-based tool or gdb itself. Just run
gdb <program>
... and then inside gdb:
run <arguments>
... and it will stop where the crash happens.
To get reasonable information about the program location you want to compile with debug information,i.e., using the -g option. To avoid confusion you might want to compile without optimization, i.e., without any -O... option. However, some errors are only triggered when optimization is turned on (note: these are generally still errors in your code and not in the optimizer).

Use the GNU Debugger (GDB). Add break points and run the code. Here is where you can find stuff. http://www.unknownroad.com/rtfm/gdbtut/gdbtoc.html

It sounds like your program is trying to access an invalid (non-existent) address. It is also possible that it's trying to access misaligned data. I've seen this before when attempting to access misaligned structures.
Read up on segmentation faults: http://en.wikipedia.org/wiki/Segmentation_fault

Related

C++ segmentation fault in std::string function [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I'm developing an algorithm based on Skyline queries using C++, using a RTree for storing my data. The algorithm works fine if I process up to 5 points then if I try 6 points it gives a segmentation fault.
Using gdb I have discovered that the problem is here:
Program received signal SIGSEGV, Segmentation fault.
std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string (
this=0x7fffffffd7c0, __str=
<error reading variable: Cannot access memory at address 0xffffffffffffffe8>)
at /usr/src/debug/gcc-4.7.2-20120921/obj-x86_64-redhat-linux/x86_64-redhat-linux/libstdc++-v3/include/bits/basic_string.tcc:175
175 __str.get_allocator())
Can anybody help me to understand where the error is or ar
Mitch Wheat already suggested this in a comment, but I think it is in fact the best answer for your problem, so:
Use valgrind.
Valgrind is a tool (or rather, a set of tools) for which the default mode of operation is to run your program and check for memory errors, such as leaks, buffer overruns, uninitialized reads, and more.
Literally all you need to do is to build your program, preferably with the -g option to make it easier to debug, and then run it like valgrind my-prog args.... Valgrind will then print out detailed error reports with stack traces when something bad is going on. Your problem here will likely be found without ever needing to use a regular debugger, nor have us guess at the problem (if this were my own problem, I'd use valgrind too).
Given only that, my best guess is that you're trying to create a string using invalid source data, possibly a char pointer that's unitialized or that's pointing to a string that's already been freed

fast on-demand c++ compilation [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm looking at the possibility of building a system where when a query hits the server, we turn the query into c++ code, compile it as shared object and the run the code.
The time for compilation itself needs to be small for it to be worthwhile. My code can generate the corresponding c++ code but if I have to write it out on disk and then invoke gcc to get a .so file and then run it, it does not seem to be worth it.
Are there ways in which I can get a small snippet of code to compile and be ready as a share object fast (can have a significant start up time before the queries arrive). If such a tool has a permissive license thats a further plus.
Edit: I have a very restrictive query language that the users can use so the security threat is not relevant. My own code translates the query into c++ code. The answer mentioning clang is perfect.
Running Clang in JIT mode should provide the speed you need, and example can be found here, safety on the other hand is something else...
Ch also had a JIT added, and seeing as its an interpreter, it might provided an easier sandboxed/controlled environment.
In addition to Necrolis answer, there's also specialized C++ parser Cling. Might come in handy.

Segmentation fault. 0xb7d9e768 in memmove () from /lib/libc.so.6 [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
GDB gives me the above error WRT my C++ program. Nowhere I have used any memory function including new and delete etc.
I want to understand the 'MEANING' of this error.
If run your program under gdb, you should be able to print out the backtrace and see what part of your code is causing the segmentation fault. memmove() may being called indirectly through a different system call.
It is possible that an array operation in your code gets optimized as a call to memmove: this is probably why the compiled code uses memmove, whereas your source code doesn't.
I think you should check that you are not accessing your arrays out of bounds.
memmove tried to access (read or write) a memory segment it shouldn't touch.
The reasons can be manifold, but probably pointer corruption. Check it with a debugger, valgrind, check stack trace, etc...

C++: More and more complex bugs misleading the debugger [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
It seems like the bugs in my project are evolving. While in the beginning time, causers of program crashes were easily to spot using the debugger (the line it points to after the program crashes), it's different now.
Many bugs cause the program to crash in completely arbitary places not even closely related to them.
How can I spot bugs "hiding" from the debugger in a better way?
(I use Visual Studio 2010)
Profile your code for memory corruption, memory overwrites and memory leaks.
Root cause analysis.
When you find an obvious bug, don't just fix the bug, fix the coding style that allowed it.
If you have any code using raw memory and pointers, replace it by memory allocated using std::vector and its iterators. It will compile to exactly the same speedy code in Release mode, but in Debug mode it will used checked iterators, which will catch some bugs as early as possible.
Use memory corruption checking utilities, like gflags or debug heap. "Floating" crushes almost always come from corrupted memory in C++ programs

Finding a totally nasty, complex, Schröding-Bohr-Bug [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I have a really nasty bug in my program, which grew quite complex over time. It's probably the worst bug I've ever had.
I think that it might be related to a static variable initialization fiasco, but how can I ensure myself of that?
When the bug strikes, the program crashes due to heap corruption at a random point after startup, but far inside the main() function.
To be honest, I don't know what to do.
I'm on Windows 7 using Microsoft Visual Studio 2010
my program, which grew quite complex
over time
Do you keep backups of previous versions?
Find an older version that worked and continue working based on that version...
There is a famous quote out there:
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." --Brian Kernighan
If this program has become more complicated than you can handle then it may be time to think about refactoring.
(This is in no way intended to be demeaning or to be taken as a personal attack...)
Run your program in the debugger, and step through the code until you see what's wrong. Place breakpoints liberally anywhere you think the bug might be caused.
Try debugging your program with gdb.