|
-
April 25th, 2003, 02:01 PM
#1
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.
-
April 25th, 2003, 04:31 PM
#2
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.
-
April 25th, 2003, 04:58 PM
#3
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.
-
April 25th, 2003, 09:24 PM
#4
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
-
April 26th, 2003, 10:59 AM
#5
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.
-
April 26th, 2003, 11:16 AM
#6
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.
-
April 27th, 2003, 10:33 AM
#7
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.
-
April 27th, 2003, 12:47 PM
#8
Just make sure that every time you use malloc() [or a malloc()
derivative] you have a corresponding free().
--Paul
-
April 28th, 2003, 01:04 AM
#9
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;}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|