Is it efficient to use realloc to free up small chunks of memory C++ [duplicate]

I’m writing an application that generates a bunch of n strings (bit)length l and then does a comparison between all possible pairs.

I first allocate memory for n strings of length l

char* pStr = (char*)(malloc(n * l)); 

Then I run my comparison of the last char of the last string with the last char of all other strings and store my results. After this I technically do not need to store that char anymore so I could use pStr = realloc(pStr, n*l - sizeof(char*)) to free up that memory.

Im wondering if this makes sense to do, say for every char, or for every string after the whole string has been compared. If you increase size of the pointer and realloc can’t do so contiguously it will copy the memory to a new location instead which isn’t very efficient. Could something like this ever happen when shrinking the allocated memory or not since it can always be contiguous?

Also am I assuming correctly that the memory freed when resizing using realloc is the last char in the pointer? If so would there be any way to instead remove the first char and shift the remaining ones to the pointer address?

12

Unless you are allocating memory in-between the reallocations, there is no chance that you will run out of memory, so do not need to be reallocating anything.

If you are running out of memory, then freeing up tiny chunks is probably not going to save you.

Reallocations to a smaller size will typically happen in-place, although no guarantees are given about that. (And yes, you are assuming correctly, that it is always the end of the block that gets chopped off.)

When you reallocate a block to a smaller size, what typically happens under the hood is that the standard library first sees if it can create a new free block right after the end of the newly reallocated block to hold the freed up memory. Every free block begins with a header, which typically contains two pointers (doubly-linked free list) and one length; this is a whole 4 + 4 + 4 = 12 bytes on a 32-bit system, double that on a 64-bit system.

If the number of bytes you are releasing is smaller than that, then the standard library cannot create a new free block, so one of the following two things will happen:

  • Either the reallocation will result in zero memory being freed, or
  • The reallocated block will be moved elsewhere, with all the overhead that this represents.

Generally, dynamic memory allocation (and reallocation, and freeing,) is expensive, so the fewer of it you do, the better.

For C++ applications that do a lot of dynamic memory allocation, one of the biggest problems is memory fragmentation, and the more you churn the memory around, the more fragmentation you will have.

So, bottom line is:

For a vaguely defined situation such as this, there is no definitive answer, but the reallocations you are contemplating are not likely to be efficient. As a matter of fact, most chances are that they are going to be very inefficient.

P.S.

If you are an application programmer, and you find yourself thinking of saving memory of the order of bytes here and there, or saving processing time of the order of clock cycles here and there, the savings are never worth the mental effort.

am I assuming correctly that the memory freed when resizing using realloc is the last char in the pointer? If so would there be any way to instead remove the first char and shift the remaining ones to the pointer address?

No, your assumption is not correct.

Could something like this ever happen when shrinking the allocated memory or not since it can always be contiguous?

You can try to use this Trial Demo Code to dynamically allocate an array of characters, and then remove a part from the front while shrinking the size of the allocated memory accordingly.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật