Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > C exe on winXP memory limitation

Reply
Thread Tools

C exe on winXP memory limitation

 
 
fermineutron
Guest
Posts: n/a
 
      10-27-2006
I wrote a program to calculate factorials of large numbers. The long
precission integers are stores in arrays. When i specify the length of
these arrays larger than 16000 elements, each element is a long int,
the windows refuses to execute the program. More precicely the code
terminates as soon as it starts to execute. How can i overcome this
issue. Note, if array length is set smaller the code runs with no
problems.

Here is the code:
==========================Start Paste==========================

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

#define al 8024*8
#define base 1000
typedef long int IntegerArrayType;

struct AEI{
IntegerArrayType data[al];
long int digits;
};

void pack(IntegerArrayType i, struct AEI *N1);
void Aadd(struct AEI * A, struct AEI * B, struct AEI * C);
void Ambi(struct AEI * A, IntegerArrayType B, struct AEI * C);
void Lshift(struct AEI * A, IntegerArrayType B, struct AEI * C);
void Amult(struct AEI * A, struct AEI * B, struct AEI * C);
void AEIprintf(struct AEI * N1);
void AEIfprintf(FILE * fp, struct AEI * N1);


int main(void)
{

struct AEI N1, MO;
long i = 0, j = 0, ii, NUM;
FILE *ff;

while (i < al)
{
N1.data[i] = 0;
MO.data[i] = 0;
i++;
}

printf("Enter integer to Factorialize: ");
scanf("%ld", &NUM);

pack(1, &N1);
ff = fopen("Results.txt", "w");
printf("you entered: %ld", NUM);

i=1;
while(i < NUM )
{
ii = 1;

while (ii < al)
{
MO.data[ii] = 0;
ii++;
}

pack((i+1), &MO);

Amult(&N1, &MO, &N1);

printf("\nPogress: %d", i);

i++;

}
if(ff!=NULL){
fprintf(ff,"\n%d\n",i+1);
AEIfprintf(ff, &N1);
}
fclose(ff);

printf("\nPogress: 100%");

return 0;
}


void AEIprintf(struct AEI *N1){

double fieldLength;
double temp;
char format0[8];
char format1[8];
long j;
j = N1->digits-1;
fieldLength = log10(base);
temp = modf(fieldLength, &fieldLength);

format0[0] = '%';
format0[1] = fieldLength + 48;
format0[2] = 'd';
format0[3] = 0x00;
format1[0] = '%';
format1[1] = '0';
format1[2] = fieldLength + 48;
format1[3] = 'd';
format1[4] = 0x00;

printf(format0, N1->data[j]);
j--;

while (j >= 0)
{
printf(format1, N1->data[j]);

j--;
}

return;
}


void AEIfprintf(FILE * fp, struct AEI *N1){
long j = N1->digits-1;

double fieldLength, temp;
char format0[8], format1[8];

fieldLength = log10(base);
temp = modf(fieldLength, &fieldLength);

format0[0] = '%';
format0[1] = fieldLength + 48;
format0[2] = 'd';
format0[3] = 0x00;
format1[0] = '%';
format1[1] = '0';
format1[2] = fieldLength + 48;
format1[3] = 'd';
format1[4] = 0x00;

fprintf(fp,format0, N1->data[j]);
j--;

while (j >= 0){
fprintf(fp, format1, N1->data[j]);
j--;
}


return;
}



void pack(IntegerArrayType i, struct AEI * N1)
{
long t = 1, i1, j = 0;

while (t == 1){
i1 = i % base;
N1->data[j] = i1;
i = (i - i1) / base;
j++;
if (i == 0)
t = 0;
}
N1->digits=j;
return;
}

void Aadd(struct AEI *A, struct AEI *B, struct AEI *C){ /* C = A + B
*/
long carry = 0, result, i =0, d;
if(A->digits>B->digits){
d=A->digits;
}else{
d=B->digits;
}
while (i < d && i<al){
result = A->data[i] + B->data[i] + carry;
C->data[i] = result % base;
carry = (result - result % base) / base;
i++;
}
if(carry!=0 && i<al){
C->data[i]=carry;
d++;
}
if(i==al){
printf("\nAadd Array overflow");
}
C->digits=d;
return;

}

void Ambi(struct AEI *A, IntegerArrayType B, struct AEI *C){
/*C = A * B; B is integer; */
long carry = 0, result, i = 0, d;

d=A->digits;

while (i < d){
result = A->data[i] * B + carry;
C->data[i] = result % base;
carry = (result - result % base) / base;
i++;
}
if(carry!=0 && i<al){
C->data[i]=carry;
d++;
}
if(i==al){
printf("\nAmbi Array overflow");
}
C->digits=d;
return;
}

void Lshift(struct AEI * A, IntegerArrayType B, struct AEI * C){
/*C = A LS B; B is integer; */
long i = 0;

while (i<A->digits && (i + B) < al){
C->data[i + B] = A->data[i];
i++;
}
if(i+B==al){
printf("\nArray overflow");
}
C->digits=A->digits+B;
return;
}

void Amult(struct AEI * A, struct AEI * B, struct AEI * C){
/*C = A * B; */
long i, ii;

struct AEI Temp, Temp1, ANS;

ii = 0;
while (ii < al){
ANS.data[ii] = 0;
ii++;
}
ANS.digits=0;
i = 0;
while (i < B->digits){

ii = 0;
while (ii < al){
Temp1.data[ii] = 0;
Temp.data[ii] = 0;
ii++;
}
Temp.digits=0;
Temp1.digits=0;
Lshift(A, i, &Temp);
if(B->data[i]!=0){
Ambi(&Temp, B->data[i], &Temp1);
Aadd(&Temp1, &ANS, &ANS);
}
i++;

}
i = 0;
while(i<al){
C->data[i]=ANS.data[i];
i++;
}
C->digits=ANS.digits;

return;
}

=========================End Paste=============================

thanks ahead.

 
Reply With Quote
 
 
 
 
fermineutron
Guest
Posts: n/a
 
      10-27-2006
By the way i am on emachine M6810 notebook with AMD Athlon64 3200 CPU
and 512 MB of ram. when i execute the code i have about 300MB of free
ram.

 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      10-27-2006
fermineutron wrote:
> By the way i am on emachine M6810 notebook with AMD Athlon64 3200 CPU
> and 512 MB of ram. when i execute the code i have about 300MB of free
> ram.
>

With lcc-win32 your program runs without any problems.

http://www.cs.virginia.edu/~lcc-win32
 
Reply With Quote
 
Ancient_Hacker
Guest
Posts: n/a
 
      10-27-2006
your problem as nothing to do with the C standard AFAIK.

But for future reference, any problem that shows up near a power of two
(I suspect the actual place it fails is nearer to 16,384) is likely to
be related to some limit, like a limit of 32,767 or 65,536 bytes per
local variable.

Try compiling your program with a 32-bit compiler and I suspect the
problem will go away, (or at least be postponed until the 32-bit limit).

 
Reply With Quote
 
fermineutron
Guest
Posts: n/a
 
      10-27-2006
Compied with lcc-win32,

if al is changeed to

#define al 8024*8

instead of what i had in earlier post, the program fails as it did with
BCpp5.02 compiler.

 
Reply With Quote
 
T.M. Sommers
Guest
Posts: n/a
 
      10-27-2006
fermineutron wrote:
> I wrote a program to calculate factorials of large numbers. The long
> precission integers are stores in arrays. When i specify the length of
> these arrays larger than 16000 elements, each element is a long int,
> the windows refuses to execute the program. More precicely the code
> terminates as soon as it starts to execute. How can i overcome this
> issue. Note, if array length is set smaller the code runs with no
> problems.


Allocate your arrays dynamically.

> Here is the code:
> ==========================Start Paste==========================
>
> #include<stdio.h>
> #include<stdlib.h>
> #include<math.h>
>
> #define al 8024*8
> #define base 1000


Conventionally, #defines are given names in all capital letters,
and variables are not given names with all capital letters. This
is only a convention, but following it does make it easier for
others to read your code.

> ff = fopen("Results.txt", "w");


You need to check that fopen worked.

> printf("you entered: %ld", NUM);


You consistently fail to end output lines with newlines. That is
a bad habit, gains you nothing, and may lose output.

> printf("\nPogress: %d", i);


i is a long, so use %ld. Spell 'progress' with an 'r'.

> printf("\nPogress: 100%");


The '%' needs to be escaped.

> char format0[8];
>
> format0[0] = '%';
> format0[1] = fieldLength + 48;
> format0[2] = 'd';
> format0[3] = 0x00;


> printf(format0, N1->data[j]);


Easier to do this:

printf("%*d", fieldLength, N1->data[j]);

--
Thomas M. Sommers -- http://www.velocityreviews.com/forums/(E-Mail Removed) -- AB2SB

 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      10-27-2006
fermineutron wrote:
> Compied with lcc-win32,
>
> if al is changeed to
>
> #define al 8024*8
>
> instead of what i had in earlier post, the program fails as it did with
> BCpp5.02 compiler.
>


I compiled with lcc-win32 with
#define al 8024*8 and it works.

lcc-win32 gives you 1MB stack.

jacob
 
Reply With Quote
 
fermineutron
Guest
Posts: n/a
 
      10-27-2006

> I compiled with lcc-win32 with
> #define al 8024*8 and it works.
>
> lcc-win32 gives you 1MB stack.
>
> jacob


Theoretically stack size should not matter in this case, since all
arguments are passed by reference.

Did you run it on windows?

I would not be surprized if its the evil windows that is at fault.

 
Reply With Quote
 
Coos Haak
Guest
Posts: n/a
 
      10-27-2006
Op 27 Oct 2006 10:10:07 -0700 schreef fermineutron:

>> I compiled with lcc-win32 with
>> #define al 8024*8 and it works.
>>
>> lcc-win32 gives you 1MB stack.
>>
>> jacob

>
> Theoretically stack size should not matter in this case, since all
> arguments are passed by reference.
>
> Did you run it on windows?


What is the name of the compiler, would it be per chance be lcc-WIN32?

> I would not be surprized if its the evil windows that is at fault.

Jacob writes the lcc-win32 compiler, would he not know?
--
Coos
 
Reply With Quote
 
Bill Medland
Guest
Posts: n/a
 
      10-27-2006
fermineutron wrote:

> I wrote a program to calculate factorials of large numbers. The long
> precission integers are stores in arrays. When i specify the length of
> these arrays larger than 16000 elements, each element is a long int,
> the windows refuses to execute the program. More precicely the code
> terminates as soon as it starts to execute. How can i overcome this
> issue. Note, if array length is set smaller the code runs with no
> problems.
>
> Here is the code:
> ==========================Start Paste==========================
>
> #include<stdio.h>
> #include<stdlib.h>
> #include<math.h>
>
> #define al 8024*8
> #define base 1000
> typedef long int IntegerArrayType;
>
> struct AEI{
> IntegerArrayType data[al];
> long int digits;
> };


Over 500000 bytes

snip

> int main(void)
> {
>
> struct AEI N1, MO;
> long i = 0, j = 0, ii, NUM;
> FILE *ff;
>
> while (i < al)
> {
> N1.data[i] = 0;
> MO.data[i] = 0;
> i++;
> }
>
> printf("Enter integer to Factorialize: ");
> scanf("%ld", &NUM);
>
> pack(1, &N1);
> ff = fopen("Results.txt", "w");
> printf("you entered: %ld", NUM);
>
> i=1;
> while(i < NUM )
> {
> ii = 1;
>
> while (ii < al)
> {
> MO.data[ii] = 0;
> ii++;
> }
>
> pack((i+1), &MO);
>
> Amult(&N1, &MO, &N1);


VC++ ide debug shows stack overflow here

snip

>
> void Amult(struct AEI * A, struct AEI * B, struct AEI * C){
> /*C = A * B; */
> long i, ii;
>
> struct AEI Temp, Temp1, ANS;


1540632 bytes. A bit big for Microsoft's standard stack space (default is
probably only 1MB)

snip
>
> =========================End Paste=============================
>
> thanks ahead.


Allocate more stack space.

--
Bill Medland
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Not able to store data to dictionary because of memory limitation Rama Rao Polneni Python 3 07-31-2011 08:46 AM
Maximum file limitation on digital picture frame internal memory Barney Digital Photography 35 01-08-2008 06:47 PM
WinXP 64 .mp3 summary change to look like WinXP 32? Manquala Windows 64bit 4 01-15-2006 04:01 AM
Memory limitation for Perl Markus Mohr Perl Misc 2 06-17-2004 11:49 AM
WIN 98 Memory limitation? cw@pacific.net.hk Computer Support 2 11-09-2003 04:45 AM



Advertisments