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

All 7 Replies

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 .

the program crashed.
waaa

if i delete a the program actually crashes

whats the difference between malloc and new?

where are they usually used?

Change this line:

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

to

a = new char[15];

>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

done. my program is running smoothly. thanks dude.

a rep power for you

>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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.