954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

memory allocation

hi,

in turbo C the maximum array size i am able to specify is array[250][250]..
what should i do to enlarge this size to say..array[512][512]..
i dont know a thing about memory allocation,
so please help me out/.. :-|

dalaharp
Light Poster
31 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

Turbo C is a very old DOS compiler.
250*250 just happens to be very near the maximum value for a 16 bit unsigned int which is what under DOS is used as a pointer.

Use a 32 bit compiler if you want to allocate larger arrays.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

WAT YOU CAN DO IS JUST LIKE SUGESTED USE A NEWST COMPILER AND CHANGE YOUR DATA TYPE OF YOUR ARRAY. LIKE IF IT WAS INT ARRAY CHANGE IT TO long int array[] or unsigned long int array[];
hope i have helped you out.
any more queries just post it here or pm any of us :confused:

anastacia
Junior Poster
142 posts since Nov 2004
Reputation Points: 11
Solved Threads: 1
 

hi,
is there no procedure wherein you can allocate memory by malloc etc..
my whole code is in TorboC and i cant change it to another compiler.(atleast now)..

thanks........

dalaharp
Light Poster
31 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

so the only thing to do is use turco c++ modify the code. it is possiblle

anastacia
Junior Poster
142 posts since Nov 2004
Reputation Points: 11
Solved Threads: 1
 

why can't you use another compiler?
Cost can't be an issue as there are several modern compilers available free of charge.
Or are you stuck on a machine running DOS?

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

you should have at least 1 reason 4 not being able to change your code;

anastacia
Junior Poster
142 posts since Nov 2004
Reputation Points: 11
Solved Threads: 1
 
is there no procedure wherein you can allocate memory by malloc etc..

Yes. What have you tried?

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

there is a procedure. the problem is that i cant teach u that coz i myself didnt qiute understand it. i saw that in a book sooo .. maybe somebody else CAN GIVE U A HELPING HAND

anastacia
Junior Poster
142 posts since Nov 2004
Reputation Points: 11
Solved Threads: 1
 
why can't you use another compiler? Cost can't be an issue as there are several modern compilers available free of charge. Or are you stuck on a machine running DOS?

i am using Turbo C 3.0, when you say another compiler, what do you mean,
can you name some good free downloadable ones..

dalaharp
Light Poster
31 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 
frrossk
Posting Whiz in Training
220 posts since Sep 2004
Reputation Points: 17
Solved Threads: 9
 
i am using Turbo C 3.0, when you say another compiler, what do you mean, can you name some good free downloadable ones..

http://www.compilers.net/

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 
Yes. What have you tried?
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
#include<process.h>

int main(void)
{

FILE *fp;
int n,r,c;
unsigned char *array[256];
clrscr();
//allocate memory
for(n=0;n<256;n++)
	{
		if((array[n]=(unsigned char*) malloc (sizeof(unsigned char)*256))== NULL)
		{
			printf("Not enough memory to allocate buffer\n");
			getch();
			exit(1);  /* terminate program if out of memory */
		}
	}

	if((fp = fopen("f16.dat","r")) == NULL)
	{
		printf("Unable to open the specified file.\n");
		getch();
		exit(1);
	}
	else
	{
		for(r=0;r<256;r++)
		{
			for(c=0;c<256;c++)
			{
				array[r][c]=(unsigned char) fgetc (fp);
			}
		}
		fclose(fp);
		printf("%c\n",array[55][55]); //a sample to check

	//free malloc
	for(n=0;n<256;n++)
	{
		free(array[n]);
	}
}
getch();
return(0);
}


this is what i tried to allocate memory for the 2d array..
but it gives a error, insufficient memory..
i compiled in large memory model in turbo C..
whats the problem :?:

dalaharp
Light Poster
31 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

Maybe this will be the repartee, try char instead of int, very old code, but it is Turbo C ...
[php]
/******************** fararray.c ***********************
**
** Examination of the limits of global variables:
** Behaviour of two large arrays exceeding the 64k limit.
** one far global array + one local array
** Written in Turbo C V.2.0 by dns 6/15/89
**
********************************************************/

#define ASIZE 17000

int far array1[ASIZE]; /* put array far, to next 64k segment */

/*
The two arrays combined exceed 64k bytes memory, array1 is global,
array2 is local, but array1 is loaded into its own 64k segment,
so there is no interference.
If array1 exceeds 64k space itself, use huge instead of far.
*/

int main(void)
{
unsigned int array2[ASIZE];
int k;

for (k = 0; k < ASIZE; k++) /* load array1 with 0 to ASIZE */
array1[k] = k;
for (k = 0; k < ASIZE; k++) /* load array2 30000 to 30000+ASIZE */
array2[k] = k + 30000;
printf("last 100 values of array1 (16900 - 16999) =\n");
for (k = (ASIZE-100); k < ASIZE; k++)
printf("%8u",array1[k]);
printf("\n\nfirst 100 values of array2 (30000 - 30099) =\n");
for (k = 0; k < 100; k++)
printf("%8u",array2[k]);
getchar();
return 0;
}
[/php]

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You