4

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. 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]);
}
14
  • 2
    I think that StackOverflow occured, due to crazy size of first array a, use dynamic memory instead. Commented May 13, 2016 at 5:44
  • 3
    Gotta love stack overflow on stackoverflow. Commented May 13, 2016 at 5:45
  • 1
    Is 700000 * 120 too big? That should be <100 MB. Commented May 13, 2016 at 5:47
  • I realy have a file with near 700000 lines with near 100 char in each line, that I need to sort by string length . Commented May 13, 2016 at 5:50
  • Then you need allocate the array with malloc. Commented May 13, 2016 at 5:50

5 Answers 5

2

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.

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).

Sign up to request clarification or add additional context in comments.

1 Comment

You should call free or at least to mention about the leak
2

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. You should use malloc() to create large arrays instead.

3 Comments

does the max size of objects depend on the compiler? or is it always 8 MB?
apparently you can set the stack size programmatically - I had no idea stackoverflow.com/questions/2275550/…
The stack size as far as I know is platform independent and of course there is a default stack size. Could you show me some reference where says that the default one is 8MB?
0

I think there is no need of creating str variable, you could have used the a itself. 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.

Comments

0

In Linux Environment, ulimit -s can return the stack size.

root@ubuntu:# ulimit -s
8192

It means the max stack space that the system support is 8192 KB, that is 8MB. test program as follows, try to modify array size from 8*1024 to 7*1024.

#include<stdio.h>

void test()
{
}

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

Comments

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.