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
864
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?
 

neuralc

Full Member level 4
Joined
Nov 6, 2001
Messages
236
Helped
9
Reputation
18
Reaction score
3
Trophy points
1,298
Activity points
2,202
Strange...
Can you place a sample code ilustrating....

NeuralC
 

proiettile

Junior Member level 2
Joined
Dec 20, 2002
Messages
20
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
76
What is the memory model that you use?
Tiny,Large,hughe...
 

CMOS

Advanced Member level 3
Joined
Jan 6, 2004
Messages
864
Helped
94
Reputation
186
Reaction score
50
Trophy points
1,308
Location
USA
Activity points
5,673
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.
 

echo47

Advanced Member level 5
Joined
Apr 7, 2002
Messages
3,942
Helped
638
Reputation
1,274
Reaction score
89
Trophy points
1,328
Location
USA
Activity points
33,176
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.
 

CMOS

Advanced Member level 3
Joined
Jan 6, 2004
Messages
864
Helped
94
Reputation
186
Reaction score
50
Trophy points
1,308
Location
USA
Activity points
5,673
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! :(
 

neuralc

Full Member level 4
Joined
Nov 6, 2001
Messages
236
Helped
9
Reputation
18
Reaction score
3
Trophy points
1,298
Activity points
2,202
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
 

artem

Advanced Member level 4
Joined
May 22, 2003
Messages
1,350
Helped
126
Reputation
252
Reaction score
32
Trophy points
1,328
Location
Turkey
Activity points
13,450
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 .
 

Pinczakko

Full Member level 2
Joined
Jul 29, 2004
Messages
138
Helped
13
Reputation
26
Reaction score
0
Trophy points
1,296
Location
Taka Bonerate National Park, Indonesia
Activity points
1,592
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.
 

CMOS

Advanced Member level 3
Joined
Jan 6, 2004
Messages
864
Helped
94
Reputation
186
Reaction score
50
Trophy points
1,308
Location
USA
Activity points
5,673
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.
 

CMOS

Advanced Member level 3
Joined
Jan 6, 2004
Messages
864
Helped
94
Reputation
186
Reaction score
50
Trophy points
1,308
Location
USA
Activity points
5,673
Hi,
What is your opinion on Bloodshed Dev C++?
 

zkai2000

Member level 5
Joined
Jul 25, 2004
Messages
94
Helped
0
Reputation
0
Reaction score
2
Trophy points
1,286
Activity points
907
hi, what is "getch()"? i have problem compiling the codes above. it says undeclared identifier.

Do i need a header or something?

Thanks!
 

CMOS

Advanced Member level 3
Joined
Jan 6, 2004
Messages
864
Helped
94
Reputation
186
Reaction score
50
Trophy points
1,308
Location
USA
Activity points
5,673
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;
 

zkai2000

Member level 5
Joined
Jul 25, 2004
Messages
94
Helped
0
Reputation
0
Reaction score
2
Trophy points
1,286
Activity points
907
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
 

CMOS

Advanced Member level 3
Joined
Jan 6, 2004
Messages
864
Helped
94
Reputation
186
Reaction score
50
Trophy points
1,308
Location
USA
Activity points
5,673
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.
 

zkai2000

Member level 5
Joined
Jul 25, 2004
Messages
94
Helped
0
Reputation
0
Reaction score
2
Trophy points
1,286
Activity points
907
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?
 

Pinczakko

Full Member level 2
Joined
Jul 29, 2004
Messages
138
Helped
13
Reputation
26
Reaction score
0
Trophy points
1,296
Location
Taka Bonerate National Park, Indonesia
Activity points
1,592
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++.
 

CMOS

Advanced Member level 3
Joined
Jan 6, 2004
Messages
864
Helped
94
Reputation
186
Reaction score
50
Trophy points
1,308
Location
USA
Activity points
5,673
Thats one and faster way of doing it. Or you can write same code in VB too.
 

zkai2000

Member level 5
Joined
Jul 25, 2004
Messages
94
Helped
0
Reputation
0
Reaction score
2
Trophy points
1,286
Activity points
907
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.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top