开发者

printf %p segfaulting

        printf("\nframe is: %p",&frame);
        printf("\nframeprev is: %p",&framePrev);

whatever line comes first will always correctly print. The second line will always segfault in the above code, regardless of which pointer it is printing. any ideas whythis is? I have tried fflush( stdout ); after each printf but this seems to make no difference.

the pointer is delcaired using the following

frame =(double**) malloc(cols*sizeof(double));
framePrev =(double**) malloc(cols*sizeof(double));

if(frame==NULL||framePrev==NULL){
    printf("malloc epic fail\n");
    return 0;
}

/*allocate mem for 2nd dimention*/

for(i=0;i<cols;i++){
    frame[i]=(double*) malloc(rows*sizeof(double));
    framePrev[i]=(double*) malloc(rows*sizeof(double));
    /*check for null pointer*/
    if(frame[i]==NULL||framePrev[i]==NULL){开发者_Python百科
        printf("malloc epic fail\n");
        return 0;
    }
}


I don't know why that code you provided would segfault. However, what you're trying to print out is the address of the pointer, not the contents of the pointer. In other words, frame is a pointer variable; it is 4 bytes big and lives on the stack/heap somewhere. You're printing out the address of that somewhere. What I think you want is to print the value of frame; which would be the actual pointer that frame holds. So take off the & in each line and see what you get.

Also, your memory allocation is strange. A (double **) is a pointer to an array that holds pointers to doubles; but your malloc call allocates an array that holds doubles. You probably want malloc(cols * sizeof(double *)). This code works, because double is bigger than double *, so you actually allocate enough memory, but it's still wrong.


There is nothing wrong with your printf statements. If you're segfaulting, its probably because you have a bug in your routines and have smashed memory.

One obvious problem in what you posted is that your frame and frameprev arrays are not arrays of doubles, but double pointers, and its unlikely that they are the same size. The first two lines should be:

frame =(double**) malloc(cols*sizeof(double *));
framePrev =(double**) malloc(cols*sizeof(double *));


just throw the code into a test.cpp file like this:

int main(){

    int i, cols=4, rows=2;
    double **frame =(double**) malloc(cols*sizeof(double));
    double **framePrev =(double**) malloc(cols*sizeof(double));

    if(frame==NULL||framePrev==NULL){
        printf("malloc epic fail\n");
        return 0;
    }

    /*allocate mem for 2nd dimention*/
    for(i=0;i<cols;i++){
        frame[i]=(double*) malloc(rows*sizeof(double));
        framePrev[i]=(double*) malloc(rows*sizeof(double));
        /*check for null pointer*/
        if(frame[i]==NULL||framePrev[i]==NULL){
            printf("malloc epic fail\n");
            return 0;
        }
    }

    printf("\nframe is: %p",&frame);
    printf("\nframeprev is: %p",&framePrev);
}

compiled via g++ test.cpp, run it via ./a.out ->

frame is: 0x7fff5fbffaf8
frameprev is: 0x7fff5fbffaf0

there is no segfaulting at all.

how to you compile? any other changes to your code now shown in your post? how do you initialize your variables?

regs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜