CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2003
    Posts
    13

    Question allocating memory for a large 2-D array

    hi,
    i am writing a program in C which histograms data into a 2-D array. so i would like to work with a 4096x4096 array of type int which on my machine is 4-bytes (i'm running linux on a pentium II). after debugging, i've found that upon runtime, even the declaration:
    int Array[4096][4096];
    causes a "segmentation fault". the maximum value is:
    int Array[1447][1447];

    why the number 1447 i have no idea. i have searched this forum for help and found a suggestion using the function "malloc" but i'm not sure how to use this. could someone explain how i might override this limit and use a 4k x 4k array?

    Thank you for any help you might be able to give.
    -R.G.

  2. #2
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    You are running out of stack space. It is never a good idea to allocate this much memory on the stack. Allocate on the heap instead.

  3. #3
    Join Date
    Apr 2003
    Posts
    13

    thanks but still need help

    thanks for your help. so in order to use the heap instead of the stack, i must use the function "malloc"? i'm not experienced with this and my C book does not go into enough detail. could you give me an example so that i may make the array as large as i require?
    thanks so much for your time.
    -R.G.

  4. #4
    Join Date
    Apr 2003
    Location
    china
    Posts
    15
    malloc
    Synopsis
    #include <stdlib.h>
    void *malloc(size_t size);

    Description
    The malloc function allocates space for an object whose size is specified by size and whose value is indeterminate.

    Returns
    The malloc function returns either a null pointer or a pointer to the allocated space.

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


    int main(void)
    {
    char *str;

    /* allocate memory for string */
    if ((str = (char *) malloc(10)) == NULL)
    {
    printf("Not enough memory to allocate buffer\n");
    exit(1); /* terminate program if out of memory */
    }

    /* copy "Hello" into string */
    strcpy(str, "Hello");

    /* display string */
    printf("String is %s\n", str);

    /* free memory */
    free(str);

    return 0;
    }
    Victory

  5. #5
    Join Date
    Apr 2003
    Posts
    13

    thanks

    ok, thanks for the example. it doesn't make much sense yet since i'm still a beginner, but i'll study it and hopefully i figure out how to apply it to a 2-dimensional array.
    thanks for your time.
    -R.G.

  6. #6
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Here is your answer

    Code:
    int **pArray;
    // Allocate memory for rows
    pArray=(int**)malloc(4096*sizeof(int*));
    // further allocate memory for column (for each row)
    for(i=0;i<4096;i++) // upto # of rows
       pArray[i]=(int*)malloc(4096*sizeof(int));
    This will serve your purpose. Ask for any problem.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  7. #7
    Join Date
    Apr 2003
    Posts
    13

    Cool great help

    this is great - thanks for your help. i see what's going on now. i had just never used malloc before and i'm still learning about pointers too.
    ok, thanks again!
    -R.G.

  8. #8
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    Just make sure that every time you use malloc() [or a malloc()
    derivative] you have a corresponding free().

    --Paul

  9. #9
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871
    Originally posted by PaulWendt
    Just make sure that every time you use malloc() [or a malloc()
    derivative] you have a corresponding free().
    --Paul
    That's true. Along with this one should also check if memory is allocated or not:
    Code:
    p=malloc...;
    if(p==NUL) // or p==0 or !p
    {   printf("Allocation failed"); return;}
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured