printf的命令导致赛格故障? [英] printf command causing a seg fault?

查看:116
本文介绍了printf的命令导致赛格故障?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试初始化一个大型双维字符数组,它完美的罚款。但是,当我添加一个简单的打印命令,它给了我一个分段错误。任何想法,为什么发生这种情况?

When I try to initialize a large double dimensional character array, it works perfectly fine. But when I add a simple print command, it gives me a segmentation fault. Any ideas as to why this is happening?

#include<stdio.h>
int main(void)
{
    printf("!");  
    char f[10000][10000];
}

它工作正常,没有printf命令,或者即使printf命令打印什么,(即)。如果我让打印任何东西它给人的错误。

It works fine without the printf command, or even if the printf command prints nothing, (i.e. ""). If I make it print anything at all it gives the error.

任何帮助吗?

推荐答案

这可能是因为您已超出堆栈。你的 F的的定义,只要你实际使用的堆栈采用100MB空间栈(10000x10000个字节),可能时,系统会发现,没有那么多的房间在栈和段错误。您可能会发现同样与调用任何其他功能。

This is probably because you are exceeding the stack. Your definition of f takes 100MB Stack space (10000x10000 bytes) and probably as soon as you actually use the stack, the system will find that there isn't that much room on the stack and segfault. You'll probably find the same with calling any other function.

这大小的分配应该通过的malloc()来完成。

Allocations of that size should be done through malloc().

   char *f= malloc(10000*10000);

   // access two dimensionally (equivalent of f[5][8])
   char z= f[5*10000 + 8];

这篇关于printf的命令导致赛格故障?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆