Array Seg Fault

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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;


code tags make it much easier to read. what is BUNCHES_PER_BLOCK?

that's not it - call fflush(stdout); after printf("second"), see if you get different output.
Last edited on
It is not a relevant code.
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.
The value of k is the same as BUNCHES_PER_BLOCK after the first loop.:)

This is true vlad.

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.

Also nobody knows what is cckov.n[i]?
Last edited on
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...
To say that the code is relevant you should compile it as is and get the error. Did you do so?
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).
Last edited on
That information alone was worth setting up an account here. Thanks. I will work on down the code with that and see where it leads.
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.
I was missing a single letter in a variable a lot farther down the code! What debugger would you recommend? I'm using Ubuntu.
Last edited on
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.

http://i.imgur.com/KgtPn.jpg
I recommend valgrind.
Topic archived. No new replies allowed.