printf %p 段错误

发布于 2024-10-30 02:15:19 字数 771 浏览 1 评论 0原文

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

无论哪一行先出现,都将始终正确打印。 在上面的代码中,第二行总是会出现段错误,无论它打印的是哪个指针。有什么想法吗?我尝试过 fflush( stdout );在每个 printf 之后但这似乎没有什么区别。

使用以下命令释放指针

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){
        printf("malloc epic fail\n");
        return 0;
    }
}
        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){
        printf("malloc epic fail\n");
        return 0;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

虐人心 2024-11-06 02:15:19

我不知道为什么您提供的代码会出现段错误。但是,您尝试打印的是指针的地址,而不是指针的内容。也就是说,frame是一个指针变量;它有 4 个字节大,位于堆栈/堆上的某个地方。你正在某处打印出该地址。我认为你想要的是打印框架的值;这将是框架所持有的实际指针。所以脱掉 &在每一行中看看你会得到什么。

另外,你的内存分配很奇怪。 A (double **) 是指向数组的指针,该数组保存指向双精度数的指针;但是您的 malloc 调用分配了一个包含双精度数的数组。您可能需要 malloc(cols * sizeof(double *))。这段代码是有效的,因为 double 比 double * 大,所以你实际上分配了足够的内存,但它仍然是错误的。

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.

闻呓 2024-11-06 02:15:19

你的 printf 语句没有任何问题。如果出现段错误,可能是因为您的例程中存在错误并且破坏了内存。

您发布的内容中的一个明显问题是您的frame和frameprev数组不是双精度数组,而是双指针数组,并且它们不太可能具有相同的大小。前两行应该是:

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

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 *));
浅笑依然 2024-11-06 02:15:19

只需将代码放入 test.cpp 文件中,如下所示:

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);
}

通过 g++ test.cpp 编译,通过 ./a.out -> 运行它

frame is: 0x7fff5fbffaf8
frameprev is: 0x7fff5fbffaf0

根本不存在段错误。

你如何编译?您的帖子中现在显示了对您的代码的任何其他更改吗?
你如何初始化你的变量?

法规。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文