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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with array memory in C

Status
Not open for further replies.

gacon113

Programmer
Nov 13, 2007
3
VN
I need to print an array[256x65536], I made a simple program but alway get error 2 if a=256 and b=65536.
Please help.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>

typedef unsigned int WORD;
typedef WORD* WORDPTR;

WORDPTR *S;
WORDPTR D;

WORD x_num;
WORD y_num;

WORD a;
WORD b;
WORD i, j;
int main()
{
puts("------------------ Start --------------------");
puts("");

a = 0;
printf("Enter a = ");
scanf("%u", &a);


b = 0;
printf("Enter b = ");
scanf("%u", &b);

x_num = 1 << a;
y_num = 1 << b;

S = (WORDPTR *)malloc(x_num*sizeof(WORDPTR));
if (S == NULL)
{
puts("Error 1!");
return 1;
}
for (i = 0; i < x_num; i++)
{
S = (WORDPTR)malloc(y_num*sizeof(WORD));
if (S == NULL)
{
puts("Error 2!");
return 1;
}
}

D = (WORDPTR)malloc(y_num*sizeof(WORD));
if (D == NULL)
{
puts("Error 3!");
return 1;
}
puts("OK!");

return (0);
}

 
Arrays are zero based, so if you have 256 elements, the positions are 0-255.
 
IMHO, you can't print array 256x65536 (it's tons of paper;).
What platform? You can't allocate 65536 elements on 16-bit platform (bad allocation then segment faults - 100%). This program allocates S on 32-bit...
 
Thank you all for quick response.

IMHO, you can't print array 256x65536 (it's tons of paper;).
--> I will store in file

What platform?
--> I use windows XP

You can't allocate 65536 elements on 16-bit platform (bad allocation then segment faults - 100%). This program allocates S on 32-bit...
--> DO you have any solution




My favorite site:
 
Well, I have no problems in this code on old good VC++ 6.0 (Windows console application).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top