Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Large arrays and images

Status
Not open for further replies.

sod32

Programmer
Apr 12, 2001
3
IE
I want to be able to create a multi-dimensional array that is 512*512 in dimension to represent the pixels in an image:
unsigned char myArray[512][512]
When I compile it (using Borland C++ 3.2) I get an error message saying array size too large. Is there any way I could get around this? Is there a way by using dynamic memory allocation?

Thanks in advance
 
because
char myArray[512][512]; allocate array in stack memory and it is not so big.
Just use dynamic memory:
char (*myArray)[512] = NULL;

myArray = malloc (512 * sizeof (char[512]));
if (myArray == NULL) exit; /* not enough memory */
....
free(myArray);
 
You can do like this

#include <malloc.h>
#include <stdlib.h>
#include <stdio.h>

int main()
{
char (*array)[512];
array = (char(*)[512]) malloc(512 * 512 * sizeof(char));
if(array == NULL)
{
printf(&quot;Not enough memory..&quot;);
exit(-1);
}
....
printf(&quot;\n %d %d&quot;, array[0][0], array[511][511]);
....
free(array);
return 0;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top