Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a peculiar problem where my program crashes at strcmp.
Upon gdb core analysis I see that both the strings being checked are sane meaning their pointers are not NULL and they contain finite null terminated values.
However one of the strings is garbage (not the one the variable is intended to contain)
Leaving aside the fact that it is garbage, I really want to know why strcmp would crash for 2 sane strings ? What could be the possible causes of this ?
Thanks in advance!
EDIT:
An example,
a = strcmp(b,c);
(gdb) p b
$92 = 0x7f58d3a36b89 "H\205\300uRH\215}\320\350\a\300\361\377A\276"
(gdb) p c
$93 = 0x2041e48 "MAIN"
The values for b and c seem OK, but due to optimizations, especially around such functions as strcmp() that may undergo intense macro expansion, the actual values might not be available to the debugger.
The problem might be easy to spot in the source code, you should post the source to the offending function.
The value 0x7f58d3a36b89 printed by gdb for variable b is surprisingly large! You could try and modify your code this way:
static char bb = *b;
static char cc = *c;
a = strcmp(b, c);
Forcing an access to b and c before the strcmp() may move the crash up and let you verify if b is indeed what gdb prints it to be.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I would like to store 2 variables into char array, and print our the first one as shown below.
const char *a[2];
a[0] = getCapital(bufferStore); //"Australia"
a[1] = getCurrencyCode(bufferStore); "9876.00"
printf("%s", a[0]);
However, I did not get any output. The code of getCapital and getCurrencyCode should be redundant here. The main thing I want to find out is how I can print out "Australia". I'm new to C language and pointers are really hard to understand, and my assignment is due in 2 hours. Any help will be greatly appreciated!
The file stdout, which is what printf writes to, is by default line buffered. That means everything you write to it is buffered, i.e. stored in memory, and is flushed (and actually printed) when you print a newline.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a program, divided in multiple files and with a lot of code containing arrays, pointers, structs and what not.
To come to the point: these are my last lines of my main() method;
...
printf("\nLast line!");
return 0;
}
After I compiled this program without warnings, I executed it and in my console I get the text: Last line! in my CMD and after that the program crashes (doesn't respond).
I often use printf()'s to debug my code and determine the line in my code where the program crashes. In this case I cannot wrap my head around the fact that the program crashes after the last line of code.
My questions:
Could return 0; in the main function cause a crash?
Is it possible the program crashes due to undefined behaviour earlier in my program?
If so, how can I debug my code? (I'm using Windows 7)
Note: I know for sure there is only one printf("\nLast line!"); in my code.
could return 0; in the main() function cause a crash?
As for the statement alone, No, it won't cause a crash.
And is it possible the program crashes due to undefined behavior earlier in my program?
Yes. Most likely so. You invoked undefined behaviour somewhere earlier in your code, and the result, as you know, is undefined.
If so, how can I debug my code?
Try to run your program through a debugger and memory checker, like gdb on linux and valgrind. Also, ry to add breakpoint in your code in tricky areas and step through the debugger while checking the actual value against the expected value. Most likely, you'll be able to spot the error.
Also stop using printf(); try a good debugger like(gdb) it's much more simpler and more faster to debug C code
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm debugging a program and ran across something I've never seen before. Below is an excerpt from gdb.
1236 size = init_text_buffer(fn);
(gdb) p fn
$13 = 0x7fff1cd22d80 "-"
(gdb) s
init_text_buffer (fn=0xd00 <error: Cannot access memory at address 0xd00>)
at editors/vi.c:720
720 {
The function init_text_buffer is called with a char pointer with the value 0x7fff78136bd0. As I step into the function the argument suddently has a different value.
What are possible causes of this? I'm not asking you to debug my code (I didn't include any so how could you?), I just need a pointer in the right direction. This thing has left me with no clues as to what to look for at all.
You should go into hybrid assembly mode (Ctrl+x 2) and do stepi to examine which instructions are actually being performed. I had this recently - in my case it was an optimization that the C code of course didn't reveal. In your case, it could reveal a memory overrun.
Worth a shot.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I wrote some c code and compiled it with clang everything works fine until end when its time to return it gives segmentation fault which is weird because
last line of code are like :
printf("End of program\n");
return 0;
}
it even prints End of program and then gives me segmentation fault.
also when I tried to compile it on gcc it gives me segmentation fault quite early without doing much ( almost at the beginning) at all.
is there any way to find cause of these kinds of errors in c? i mean some more info than segmentation fault?
The code is actually quite long to paste it here so I giving links from Dropbox so you can look at it in-case you want to and then see what's the problem.
https://dl.dropboxusercontent.com/u/39063416/conway.tar.gz
its an implementation of conway's game of life program
strcpy( game.name , &result );// result is only char.
There's absolutely no way to be sure, even with you posting all your code. The only way to really determine what is going on is to run your code with valgrind and look for memory corruption issues.
Here's a quick start guide. http://valgrind.org/docs/manual/QuickStart.html
It's probably stack corruption. main() is not the first function executed in your application and it's not the last either. main() is just an user supplied function called from crt0.c (or whatever name has your toolchain supplied startup runtime module). If main() writes on stack area beyond its allocated stack frame, it will probably overwrite the return address, so when it finishes, won't return to its original caller, but to.... who knows.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am running into a segmentation fault error in my code and I am not sure what may be causing it. What is even more odd is that when I run I compile/run in gcc-4.4.6 RH6 I obtain no error, but on other compilers/linux distros I get a segmentation fault error.
Here is a snippet of the part of the code that I think may be generating the issue:
int BIN_SIZE=(2*width)/bins;
//binCounts and binCounts2 store the fragment counts in each bin. mask=1 flags histone modification site
float **binVals;
binVals = (float **)malloc(chromNum*sizeof(int *));
//Initialize the arrays
totalBinNum = 0;
for (i=0;i<chromNum;i++)
{
totalBinNum += chromInfo[i].chromSize/BIN_SIZE+1;
binVals[i] = (float *)malloc((chromInfo[i].chromSize/BIN_SIZE+1)*sizeof(float));
memset(binVals[i], 0, (chromInfo[i].chromSize/BIN_SIZE+1)*sizeof(float));
}
If you know some easy catch on what may be causing the error please let me know? Otherwise it could also be in some other part of the code leading to not a smart Q :(
It would be more precise to do so:
binVals = malloc(chromNum*sizeof(float *));
But it is not likely that this is the cause of the error, as you can expect that 2 pointers, even if to different types int* and float*, will have the same size. In short, the source of the error is probably somewhere else in your code.
Some other suggestions:
I would suggest removing the other type cast in the other malloc.
I would use some temporary variable to store chromInfo[i].chromSize/BIN_SIZE+1, so that you do not have to repeat the expression 3 times with very likely cut and past errors.
You can compact the malloc and the memset to zero in one calloc call.