a=(char*) malloc(sizeof(15));		
		b= bmfh.bfType;
		sprintf(a, "bfType:  %d", b);
		TextOut (hdc, 0, 0,a , strlen(a)) ;
			
		delete[] a;
		a=(char*) malloc(sizeof(15));
		b=bmih.biBitCount;
		sprintf(a, "bitCount:  %d", b);
		TextOut (hdc, 0, 20,a , strlen(a)) ;

is it necessary to leave the a undeleted when i gave it another value or i should delete it first and create a new allocation?

thank you

Recommended Answers

whats the difference between malloc and new?
[inlinecode]malloc[/inlinecode]'s for C. [inlinecode]new[/inlinecode] is for C++.

Using C functions in C++ programs is bad enough, but using [inlinecode]malloc[/inlinecode] to allocate something, and [inlinecode]delete[/inlinecode] to deallocate spells pure disaster.

Jump to Post

All 7 Replies

Re: necessarry or not (memory managment) 80 80

You should definitely deallocate any memory you allocate. Otherwise you'll experience those awful memory leaks.

And why in the world are you using malloc for allocating the memory, and delete for deallocating? That's a bug right there. When in C++, do like the C++'ers do, that is, use new and delete .

Re: necessarry or not (memory managment) 80 80

the program crashed.
waaa

if i delete a the program actually crashes

Re: necessarry or not (memory managment) 80 80

whats the difference between malloc and new?

where are they usually used?

Re: necessarry or not (memory managment) 80 80

Change this line:

a=(char*) malloc(sizeof(15));

to

a = new char[15];
Re: necessarry or not (memory managment) 80 80

>whats the difference between malloc and new? malloc 's for C. new is for C++.

Using C functions in C++ programs is bad enough, but using malloc to allocate something, and delete to deallocate spells pure disaster.

commented: nice +2
Re: necessarry or not (memory managment) 80 80

done. my program is running smoothly. thanks dude.

a rep power for you

Re: necessarry or not (memory managment) 80 80

>Using C functions in C++ programs is bad enough, but using malloc to allocate something, and delete to deallocate spells pure disaster.

hehehe. cant even tell which functions is c or c++.

just started learning c you know. and im using a visual c++. so basically c code are acceptable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.