Determine the error [duplicate] - c

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Debugging a program that crashes 10 times in different places
You are given a the source to a application which is crashing when
run. After running it 10 times in a debugger, you find it never
crashes in the same place. The application is single threaded, and
uses only the C standard library. What programming errors could be
causing this crash? How would you test each one?

Pointer error. You're trying to read/write an undefined pointer - do a find and replace for free(x) -> free(x);x=NULL.
Edit: Should also check for assignment; are you doing anything like this?
int *a;
a++;

Related

What determines whether segmentation fault occurs? [duplicate]

This question already has answers here:
Why don't I get a segmentation fault when I write beyond the end of an array?
(4 answers)
C - Off by one error, but no segmentation fault?
(3 answers)
No out of bounds error
(7 answers)
Closed 2 years ago.
#include <stdio.h>
int main()
{
int array[10];
array[50] = 5;
printf("array[50] = %i \n", array[50]);
}
Above code works without a problem when is shouldn't. Why is that? This behaviour could make finding a potential bug in the program quite tricky.
The behaviour of an out of bounds array access such as this is undefined.
Undefined behaviour can manifest itself as the program working as you intended!
This does indeed make programming in C tricky, and the language draws criticism for being unsafe in this respect (cf. FORTRAN which grew up contemporaneously). Such pernicious behaviour can be obviated somewhat by adopting appropriate programming practices that have grown up since C was invented in the 1970s. Bounds checking software has also been invented which will weed out bugs such as this.

Is there a shell for quickly experimenting in C? [duplicate]

This question already has answers here:
Is there an interpreter for C? [closed]
(13 answers)
Closed 7 years ago.
I'm not looking for csh, I'm looking for a shell for C similar to the Python or the Scala shells.
I understand that C is a compiled language, but is there anything out there that would let me quickly play around with things so I can e.g. better learn how pointers work? It should at least be theoretically possible to do this, wondering if anyone has taken the time to implement it.
As you well know that C is a compiled language. It is better to write C code than compile it, do some breakpoints, learn what value is in memory, where the pointer points etc.
But I think you mean this. Is there an interpreter for C?

C Programming Increment and Decrement Operator Problem [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Undefined behavior and sequence points
(5 answers)
Closed 8 years ago.
$void main()
{
int a=10,c;
c= ++a + ++a;
printf("%d",c);
}
this program Actualy Print Value Of c=24 but By Calculation we can say it should be
c=23 ,how it possible?
Your program has a bug -- you modify the same variable twice without an intervening sequence point. Fix the bug and the mystery will go away.
A very deep understanding of not just how the language works but how compilers work is required to understand why buggy code happens to do what it happens to do. I would just suggest not writing buggy code and, when you find a bug, simply fix it instead of trying to understand precisely why and how it broke.
My advice to you is to stop. You learned the right lesson -- code that triggers undefined behavior is unpredictable and frequently doesn't do what you might expect it to do. That's all you need to know about UB until you're an expert at using the language correctly.
'++' > '+'
Here post increment operation is done before.Since you gave it two times if does post increment two times so the value of 'a' becomes 12 and adds it up (12+12).So the final value is 24.

code won't compile generates infinite 2222s [duplicate]

This question already has answers here:
C code won't finish running
(4 answers)
Closed 9 years ago.
The purpose of this code is to construct a char Hadamard matrix of the size of my choosing.
This question is related to a previous question I asked. The answer given there was an integer not char matrix, but the code here is pretty much the same format.
The code compiles but when executed it doesn't finish and I don't know why. When executed infinite 2's are printed.
I get the same result when swap the dynamic Hadamard matrix section for one of a fixed size.
Note: I've no idea what your program does, but obviously this is wrong. You failed to actually change the control variable in your for-loop (which can be done in the final expression or the loop body itself).
Change this:
for (ind=1;ind<=sizeH;ind*2)
to this:
for (ind=1;ind<=sizeH;ind*=2) // << note *=

C on Xcode: bad access [duplicate]

This question already has answers here:
EXC_BAD_ACCESS signal received
(36 answers)
Closed 9 years ago.
I am trying to finish an assignment for my C programming class on Xcode. It's simple. It reads a text file, which contains a list of student IDs and their test scores. And then it calculates the overall score based on these data, and writes them to another text file. However, I keep getting this Bad Access error message that says:
Thread 1: EXC_BAD_ACCESS(code=1, address=0x7fff00000de4)
I don't know what it means. What should I do?
The EXC_BAD_ACCESS means that you tried to attempt the incorrect address in memory.
You should start with the following:
Check that all pointers are initialized before using
If you have an array, so possibly you are trying to access the element that is out of bounds
And of course, use the debugger.

Resources