Click to See Complete Forum and Search --> : allocating memory for a large 2-D array
robgramer
April 25th, 2003, 02:01 PM
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.
mwilliamson
April 25th, 2003, 04:31 PM
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.
robgramer
April 25th, 2003, 04:58 PM
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.
PeiFeng
April 25th, 2003, 09:24 PM
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;
}
robgramer
April 26th, 2003, 10:59 AM
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.
Ajay Vijay
April 26th, 2003, 11:16 AM
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.
robgramer
April 27th, 2003, 10:33 AM
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.
PaulWendt
April 27th, 2003, 12:47 PM
Just make sure that every time you use malloc() [or a malloc()
derivative] you have a corresponding free().
--Paul
Ajay Vijay
April 28th, 2003, 01:04 AM
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:
p=malloc...;
if(p==NUL) // or p==0 or !p
{ printf("Allocation failed"); return;}
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.