Thread: Trying to allocate huge array

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    3

    Trying to allocate huge array

    Hello everyone

    (this is my first post )Im working in a Word Cound program where I need to insert every new word in an array,first i calc the number of words (which is 113000) then i use calloc to allocate this array but i get a seg error every time, can someone help me with this mess...

    thx 4 reading
    Schumy

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    Can you post the code you're trying to use? Specifically the initialization and population of the huge array?

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    3
    Thx for the quick reply.The initialization of the array is:

    char**array1=(char**)calloc(113000,sizeof(char));

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Don't cast the return value of calloc()/malloc(). If you #include <stdlib.h> and are actually using a C compiler to compile your C code then you won't get any warnings.

    Also, a pointer is not the same size as a char. Your second argument to calloc() should be sizeof(char *). That allocation just creates room for the pointers to your words. Then you'll need to allocate more memory for each of your words (the length of the word + 1 is a good amount of memory to allocate for that).
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    Ok, the problem is that you're trying to initalize an array of arrays using a function call that just returns memory for a single array. What you need to do is this.

    Code:
    char** array[how many arrays][size of each array];
    
    for (int x = 0; x < [how many arrays]; x++)
    {
            array[x] = (char*)malloc([size of each array]);
    }
    There's also something you need to do to initialize the char** array, and that needs to be done before the code sample above, but I don't remember how to do that.

    Basically, you first need to allocate memory for the array of arrays, and then you need to allocate memory for each one of the arrays within the array of arrays. That's why you're getting the error.

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    3
    Thank u very much people i'll try to work on that and i'll give u the feedback.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code: An auto expanding array (or how to use gets() safely).
    By anonytmouse in forum Windows Programming
    Replies: 0
    Last Post: 08-10-2004, 12:13 AM
  2. Huge array
    By Shadow12345 in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2002, 03:49 AM
  3. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM
  4. Using a huge 3D array
    By Gabu in forum C++ Programming
    Replies: 3
    Last Post: 07-20-2002, 09:38 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM