I'm getting a Seg Fault (Segmentation fault (core dumped)) and for the life of me can't find where it's coming from. Relevant code.
int datapoints = 4000000;
float * ntmp = new float [datapoints]();
k=0;
for (i=0;i<BUNCHES_PER_BLOCK;i++) {
ntmp[k] = cckov.n[i];
k++;
printf("k is %i\n",k);
}
float * n = new float [k];
for (m = 0;m<k;m++) {
printf("first\n");
printf("k is %i\n",k);
printf("m is %i\n",m);
n[m] = ntmp[m];
}
printf("second");
delete[] ntmp;
The printf for printing k stops at 2919111 which means that ntmp has non zero values for indices from 0:2919110. Running this with the printf's ends with
first
k is 2919111
m is 2919110
Segmentation fault (core dumped)
Which is what it should read. But the printf("second") line is never reached.
Thanks. BUNCHES_PER_BLOCK is some value that I do not know ahead of time. It's inside the file being read. That for statement is inside of a case statement so that the size of k is different than BUNCHES_PER_BLOCK. The reason for using the variable k is that before running I have no idea how many entries there are going to be. ntmp is an array of some large arbitrary value the unknown number of entries are copied into it. Then I want put all the information in an array that is only the size needed.
Rollie: The prinf("second") statement is never reached before the seg fault. Just for the hell of it I put it anyway and...what the crap "second" printed that time but then seg faulted again...
I am repeating again that this code is not relevant. That you will be sure then make a compiled program from your code and run it.
And the next time show a relevant code that can be compiled as is.
Not sure what you mean, Vlad. I pinpointed exactly where in the code it's sig faulting and posted the active parts of the code being used just before it happens. I suppose I can copy all of the code...
Lark, it's a very annoying nuance of stdout, but it works by potentially queuing up data before writing it to the screen, then writing it all at once (usually flushing when it encounters a newline '\n', iirc). So you were always making it past the printf("second"), but because stdout wasn't flushed before the crash, you didn't see it printed. I suspect printf("second\n"); will work as well.
Vlad is correct, the error is not in the code you posted. Put more printfs with flushes in, see where the error REALLY is, then give us more code to look at (if you can't figure it out yourself).
You should consider using a debugger. That would have given you the exact position of the crash right away. Note that the crash doesn't always happen on the erroneous line.
It looks like you read some invalid value from the file because the value 2919111 is strange enough. Also it is not clear what is the size of array cckov.n
I've always find visual studio to be the easiest to use for debugging. This is a screenshot of mine (with your code). Note that I put a breakpoint on printf("second"), so I could stop and see the state of everything the program. If you use linux, someone else will have to recommend something - I've used gdb, but never found it very easy, not sure if there is a more user friendly option out there.