Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Malloc allocation fails!

Status
Not open for further replies.

CMOS

Advanced Member level 3
Joined
Jan 6, 2004
Messages
862
Helped
94
Reputation
186
Reaction score
50
Trophy points
1,308
Location
USA
Activity points
5,673
createimage devc++ -java

Hi,
I am using malloc to dynamically resize 2-D integer array for image processing. But whenever I try to resize the array for values more than 15kb (120x120) memory allocation fails and thus I am not able to process any image greater than 120 x 120 pixels. I am using Turbo C 3.0 operating in DOS Shell from windows.
Is this the maximum memory that can be assigned for a 2-D array? How can I increase it further?
 

Strange...
Can you place a sample code ilustrating....

NeuralC
 

What is the memory model that you use?
Tiny,Large,hughe...
 

neuralc said:
Strange...
Can you place a sample code ilustrating....
NeuralC
This is my code
Code:
void CreateImage(int ***image, int& M, int& N)
{

	int i,j;
	if((*image=(int **)malloc(N*sizeof(int *)))==NULL)
	{
		cout<< "Not enough memory!";
		getch();
		exit(0);
	}

	for(i=0;i<N;i++)
	{
		if(((*image)[i]=(int*)malloc(M*sizeof(int)))==NULL)
		{
			cout<< "Not enough memory!";
			getch();
			exit(0);
		}
	}

proiettile said:
What is the memory model that you use?
Tiny,Large,hughe...
Memory model is small.
 

How big is your compiler's int? 2 or 4 bytes?

Small model probably gives you only 64K of data space. Your second loop allocates N*M*sizeof(int) bytes. That plus whatever the compiler has already consumed probably exceeds 64K. Try a different memory model.
 

I tried it with HUGE model but still there is no change. I even changed the pointers to "far" and used farmalloc instead of malloc. Still allocation falis for large images! :(
 

try this:

int **CreateImage(int M, int N)
{
int i;
int **pt;
if((pt=malloc(N*sizeof(int *)))==NULL)
{
cout<< "Not enough memory!";
getch();
exit(0);
}

for(i=0;i<N;i++)
{
if((pt=malloc(M*sizeof(int)))==NULL)
{
cout<< "Not enough memory!";
getch();
exit(0);
}
}
return pt;
}

With MS compiler I have tryed to build an array of 1200*1200 sucessfully:
void main(void){

int **myImage=NULL;
myImage=CreateImage(1200,1200);
}


HH

NeuralC
 

There is a link how set heap and stack size but for Borland 4.5 . If TC3 does nto support that , may be you can chnage to 4.5 version
https://teaching.idallen.com/c_programming/changingRunTimeStack.html

May be wrong handling different memory model versions is subject to blame in TC3 .
Check the borland site - there could be hints

P.S. Allocating memory for pointers is good to have faster access , but you will use extra 1200 * sizeof(int*) memory . You know array geometry apriori so it is redundant to have additional pointer array .
 

if you don't have any problem to switch to another compiler, it's better to use Microsoft Visual C++ and using the "real" memory allocation routine within the OS itself (win32API), I think it's faster. The Win32API functions are :
1. VirtualAlloc
2. HeapAlloc
3. GlobalAlloc
4. LocalAlloc
IIRC one of this function is called from the malloc library function within the OS itself. What I mean by this, is the malloc function call will be resolved by the "dynamic linking process" into one of the function above.
 

Hi,
The problem is solved now. The compiler was giving problem with farmalloc. If I try to execute my program when compiler is running, it was giving trouble. Running the compiled .exe after closing the compiler IDE works witout problems.
 

Hi,
What is your opinion on Bloodshed Dev C++?
 

hi, what is "getch()"? i have problem compiling the codes above. it says undeclared identifier.

Do i need a header or something?

Thanks!
 

getch() will wait until user presses any key. It actiually returns ascii value of the key. It is defined in "conio.h" For c++ use cin;
 

got it! but i have problems with the exit() now..nieway,i'll figure it out myself..thanks again!!

btw, CMOS, isnt this array thingy u're doing related to mine? remember? I asked u about the threshold stuffs
 

zkai2000 said:
btw, CMOS, isnt this array thingy u're doing related to mine? remember? I asked u about the threshold stuffs
Yes it is related to 2D arrays for image processing.
 

r u going to express it using GUI?

if im gonna use VB as GUI, i'll have to save the c++ codes into a dll file. then use VB to read it rite?
 

zkai2000 said:
if im gonna use VB as GUI, i'll have to save the c++ codes into a dll file. then use VB to read it rite?
Of course that's the path to take if you will use VB as the GUI. I've done thing like this before, but I'm interfacing VB to a dll which is built using C language, not C++.
 

Thats one and faster way of doing it. Or you can write same code in VB too.
 

nope i cant..coz i need C++ to do my change of orientations algorithm. i need to swing the array map around :D
AFAIK, VB cant do that rite? :(
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top