Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7794725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:51:31+00:00 2026-06-01T22:51:31+00:00

Surprisingly simple/stupid/basic question, but I have no idea: Suppose I want to return the

  • 0

Surprisingly simple/stupid/basic question, but I have no idea: Suppose I want to return the user of my function a C-string, whose length I do not know at the beginning of the function. I can place only an upper bound on the length at the outset, and, depending on processing, the size may shrink.

The question is, is there anything wrong with allocating enough heap space (the upper bound) and then terminating the string well short of that during processing? i.e. If I stick a ‘\0’ into the middle of the allocated memory, does (a.) free() still work properly, and (b.) does the space after the ‘\0’ become inconsequential? Once ‘\0’ is added, does the memory just get returned, or is it sitting there hogging space until free() is called? Is it generally bad programming style to leave this hanging space there, in order to save some upfront programming time computing the necessary space before calling malloc?

To give this some context, let’s say I want to remove consecutive duplicates, like this:

input “Hello oOOOo !!” –> output “Helo oOo !”

… and some code below showing how I’m pre-computing the size resulting from my operation, effectively performing processing twice to get the heap size right.

char* RemoveChains(const char* str)
{
    if (str == NULL) {
        return NULL;
    }
    if (strlen(str) == 0) {
        char* outstr = (char*)malloc(1);
        *outstr = '\0';
        return outstr;
    }
    const char* original = str; // for reuse
    char prev = *str++;       // [prev][str][str+1]...
    unsigned int outlen = 1;  // first char auto-counted

    // Determine length necessary by mimicking processing
    while (*str) {
        if (*str != prev) { // new char encountered
            ++outlen;
            prev = *str; // restart chain
        }
        ++str; // step pointer along input
    }

    // Declare new string to be perfect size
    char* outstr = (char*)malloc(outlen + 1);
    outstr[outlen] = '\0';
    outstr[0] = original[0];
    outlen = 1;

    // Construct output
    prev = *original++;
    while (*original) {
        if (*original != prev) {
            outstr[outlen++] = *original;
            prev = *original;
        }
        ++original;
    }
    return outstr;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T22:51:32+00:00Added an answer on June 1, 2026 at 10:51 pm

    If I stick a ‘\0’ into the middle of the allocated memory, does

    (a.) free() still work properly, and

    Yes.

    (b.) does the space after the ‘\0’ become inconsequential? Once ‘\0’ is added, does the memory just get returned, or is it sitting there hogging space until free() is called?

    Depends. Often, when you allocate large amounts of heap space, the system first allocates virtual address space – as you write to the pages some actual physical memory is assigned to back it (and that may later get swapped out to disk when your OS has virtual memory support). Famously, this distinction between wasteful allocation of virtual address space and actual physical/swap memory allows sparse arrays to be reasonably memory efficient on such OSs.

    Now, the granularity of this virtual addressing and paging is in memory page sizes – that might be 4k, 8k, 16k…? Most OSs have a function you can call to find out the page size. So, if you’re doing a lot of small allocations then rounding up to page sizes is wasteful, and if you have a limited address space relative to the amount of memory you really need to use then depending on virtual addressing in the way described above won’t scale (for example, 4GB RAM with 32-bit addressing). On the other hand, if you have a 64-bit process running with say 32GB of RAM, and are doing relatively few such string allocations, you have an enormous amount of virtual address space to play with and the rounding up to page size won’t amount to much.

    But – note the difference between writing throughout the buffer then terminating it at some earlier point (in which case the once-written-to memory will have backing memory and could end up in swap) versus having a big buffer in which you only ever write to the first bit then terminate (in which case backing memory is only allocated for the used space rounded up to page size).

    It’s also worth pointing out that on many operating systems heap memory may not be returned to the Operating System until the process terminates: instead, the malloc/free library notifies the OS when it needs to grow the heap (e.g. using sbrk() on UNIX or VirtualAlloc() on Windows). In that sense, free() memory is free for your process to re-use, but not free for other processes to use. Some Operating Systems do optimise this – for example, using a distinct and independently releasble memory region for very large allocations.

    Is it generally bad programming style to leave this hanging space there, in order to save some upfront programming time computing the necessary space before calling malloc?

    Again, it depends on how many such allocations you’re dealing with. If there are a great many relative to your virtual address space / RAM – you want to explicitly let the memory library know not all the originally requested memory is actually needed using realloc(), or you could even use strdup() to allocate a new block more tightly based on actual needs (then free() the original) – depending on your malloc/free library implementation that might work out better or worse, but very few applications would be significantly affected by any difference.

    Sometimes your code may be in a library where you can’t guess how many string instances the calling application will be managing – in such cases it’s better to provide slower behaviour that never gets too bad… so lean towards shrinking the memory blocks to fit the string data (a set number of additional operations so doesn’t affect big-O efficiency) rather than having an unknown proportion of the original string buffer wasted (in a pathological case – zero or one character used after arbitrarily large allocations). As a performance optimisation you might only bother returning memory if unusued space is >= the used space – tune to taste, or make it caller-configurable.

    You comment on another answer:

    So it comes down to judging whether the realloc will take longer, or the preprocessing size determination?

    If performance is your top priority, then yes – you’d want to profile. If you’re not CPU bound, then as a general rule take the "preprocessing" hit and do a right-sized allocation – there’s just less fragmentation and mess. Countering that, if you have to write a special preprocessing mode for some function – that’s an extra "surface" for errors and code to maintain. (This trade-off decision is commonly needed when implementing your own asprintf() from snprintf(), but there at least you can trust snprintf() to act as documented and don’t personally have to maintain it).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've checked through other questions and surprisingly this question doesn't seem to have been
Mercurial newbie here, I have a simple question. I deleted one of my files
Very simple question (surprisingly I can't find a similar question anywhere): how do I
I used a very simple code to resize image with PHP; but surprisingly it
Surprisingly can't find a match for my question. I have one table that I
It must be simple, but surprisingly I couldn't find an answer to this problem
I'm surprisingly frustrated by this, and I suspect the answer is simple. I have
Surprisingly I was only able to find one previous question on SO about this
I'm sure there are a million posts about this out there, but surprisingly I'm
A couple of month ago I wrote a simple program in Java. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.