-7
// C Program to find average of numbers given by user
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
    double sum = 0;
    int ii = 0, c;
    char buf[256], *token;

    printf
        ("Enter the numbers to average on a single line, separated by space and press enter when done\n");
    fgets(buf, 255, stdin);
    token = strtok(buf, " ");
    while (token != NULL)
    {
        sum += atof(token);
        ii++;
        token = strtok(NULL, " ");
    }
    printf("Average is %lf", sum / (double)ii);
}

On line 8: char buf[256], *token; when I change array limit to any 8 or more digit numbers like 11111111, 6829786907 (and so on..) then the program gets complied but on ouput it shows 'Segmention Error'. How can I increase array limit? I am using a UNIX based system. Please help me :)

lurker
  • 56,987
  • 9
  • 69
  • 103
user2583127
  • 51
  • 1
  • 5
  • Hmmmmm. Again!! I think there is something wrong with your compiler. – haccks Oct 09 '13 at 11:13
  • i didnt get answers on that lol – user2583127 Oct 09 '13 at 11:14
  • @haccks can you try dat 99999999 array size on ur compiler? – user2583127 Oct 09 '13 at 11:15
  • If you want to enter so many numbers that you need `99999999` bytes to hold them on the input string, then this is absolutely the wrong approach to accept the input. You should be reading them one by one from the standard input or from a file. – lurker Oct 09 '13 at 11:18
  • 2
    You don't get any answers after 20 minutes so you make a duplicate post? Clever. – Lundin Oct 09 '13 at 11:18
  • lol :P i tried char *buf = malloc(9999999999); and it worked! But adding more numbers like 999999999999999999999 (ok thats ridiculous!) then it gives this error:: integer constant overflow – user2583127 Oct 09 '13 at 11:21
  • @user2583127 I am not aware about UNIX, but in x32 Windows OS each process has 4GB virtual memory, so maximum allowed memory to alloc is 2^32 - 1. I suppose there is some restrictions in UNIX systems too. – Qwerty Oct 09 '13 at 12:08

1 Answers1

1

You can't. If you ask for an 6829786907 array, it requires 6829786907 Bytes. which is almost 7Go of ram. One does not simply alloc 7Gio at once. Furthermore, what is the goal to have a giant buffer ?

edit: when you write char buf[256];, you ask your OS to give you a contiguous memory chunk of size 256 * sizeof(char) = 256. if you write char buf[6829786907];, you ask 7Go, which is enormous, and your OS just don't give this memory chunk. You may verify this with a printf("buf adress: %p", buf); which will rint, I bet, 0x00000000. This means you don't have any space for you. Then, when you use the pointer (in the fgets() call), segmentation fault occurs. Seg fault happens when you try to read and/or write an memory adress you don't own. Here buf aka 0x00000000.

johan d
  • 2,798
  • 18
  • 26
  • i tried char *buf = malloc(9999999999); and it worked! But adding more numbers like 999999999999999999999 (ok thats ridiculous!) then it gives this error:: integer constant overflow – user2583127 Oct 09 '13 at 11:22
  • i used char data type. what should I use?? also, is it possible to define infinite size of array??! – user2583127 Oct 09 '13 at 11:25
  • 1
    It's an absolute non-sens. Read books about programming – johan d Oct 09 '13 at 11:27
  • Hey, stackoverflow will ot work for you. Stop asking times the same question. Thanks for the copypast btw. bye. – johan d Oct 09 '13 at 11:29
  • @user2583127 obviously you can't alloc array of infinite size. computer is not the universe ;) – Qwerty Oct 09 '13 at 12:01