user649558
user649558

Reputation: 57

delete partial memory of the pointer

Is there any way i can delete the partial memory of the pointer.? for example

char *c = new char[1000];
sprintf(c,"this is it");

As it can be seen a lot of memory is getting wasted here. can I free the memory more than the required.?

Upvotes: 0

Views: 419

Answers (3)

AnT stands with Russia
AnT stands with Russia

Reputation: 320631

Well, allocate another memory block with the precise size required for the data, copy the data there and free the original (exceedingly large) memory block. Done.

Upvotes: 0

Martin James
Martin James

Reputation: 24877

Unless your system is a RAM-restricted embedded system, why bother? Just use ginormous buffers and include a 'dataLen' int.

Upvotes: 0

Pete Becker
Pete Becker

Reputation: 76418

Not directly. The best you can do in C++ is to make a new copy that's the right size and delete the old one. There's no analog of C's realloc.

Upvotes: 1

Related Questions