简体   繁体   English

使用文件中的字符串数组进行分段错误

[英]segmentation fault with array of string from file

I think I get understand how string works, but some how get a segmentation error when try to run this. 我想我会理解字符串是如何工作的,但有些人在尝试运行时会遇到分段错误。 I'm trying to create array of string that i read from file f. 我正在尝试创建从文件f中读取的字符串数组。 Also any comments for optimisation and or a better way to code (especially using pointer)is appreciated. 此外,任何评论优化和/或更好的代码方式(特别是使用指针)是值得赞赏的。

char a[700000][120];
char str[120];
int i=0,l,p;
while (fgets(str,120,f)) {
    strcpy(a[i],str);
    i++;
    }
int n=i;
for (i=0;i<=3;i++) {
    printf("%s\n",a[i]);
}

See if this helps 看看这是否有帮助

char **a;
int i;

a = malloc(700000 * sizeof(char*));
for (i = 0; i < 700000; i++) {
    a[i] = malloc(120*sizeof(char));
}

// read file here instead
strcpy(a[0],"hello");
strcpy(a[1],"goodbye");
strcpy(a[2],"yes");

for (i=0;i<=3;i++) {
    printf("%s\n",a[i]);
}

Per Michi, remember to free the memory afterwards. Per Michi,记得事后释放记忆。

for (i = 0; i < 700000; i++) {
    free(a[i]);
}
free(a);

Appendix Turns out you can check the stack size AND change it. 附录结果您可以检查堆栈大小并进行更改。 Consider this 考虑一下

struct rlimit rl;
int result;

result = getrlimit(RLIMIT_STACK, &rl);

printf("stack limit %d\n", rl.rlim_cur);
printf("stack limit %d\n", rl.rlim_max);
return 0;

It gives me 它给了我

stack limit 8388608
stack limit -1

(there is the 8MB). (有8MB)。

There is a limit of array allocation, the size u are trying causes stack overflow of the function, because your array can't fit into the memory allocated to the function stack . 有一个数组分配的限制,大小你正在尝试导致函数堆栈溢出 ,因为你的数组不能适应分配给函数堆栈的内存。

There is a limit of 8MB on the maximum size of objects, due to internal compiler implementation limits. 由于内部编译器实现限制,对象的最大大小限制为8MB You should use malloc() to create large arrays instead. 您应该使用malloc()来创建大型数组。

I think there is no need of creating str variable, you could have used the a itself. 我认为没有必要创建str变量,你可以使用a本身。 Moreover as mentioned in the comments too, try using dynamic memory, as most programmers don't use stack for huge allocations. 此外,如评论中所述,尝试使用动态内存,因为大多数程序员不使用堆栈进行大量分配。 May be heap may have greater size than stack. 可能堆可能比堆栈更大。

In Linux Environment, ulimit -s can return the stack size. 在Linux环境中,ulimit -s可以返回堆栈大小。

root@ubuntu:# ulimit -s
8192

It means the max stack space that the system support is 8192 KB , that is 8MB . 它表示系统支持的最大堆栈空间为8192 KB ,即8MB test program as follows, try to modify array size from 8*1024 to 7*1024. 测试程序如下,尝试将数组大小从8 * 1024修改为7 * 1024。

#include<stdio.h>

void test()
{
}

int main()
{
    char a[7*1024][1024];
    test();
    return 0;
}

You can try this. 你可以试试这个。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void main(void) {

    FILE *fp = NULL;
    fp = fopen("read.txt", "rb");
    if(fp == NULL)
        printf("failure\n");
    else
        printf("success\n");

    char buffer[4096];
    while (fgets(buffer, sizeof(buffer), fp) != 0)
        fputs(buffer, stderr);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM