简体   繁体   中英

Realloc allocates more memory than requested

I have a problem with below function.

When I try to realloc() memory I get more than I actually asked for!

In this instance I try to concatenate 2 strings, one who is 14 characters long and one who is 11 characters long, but the end result is that memTemp is 38 characters long even though memNewSize shows that it is in fact 25, does anyone know what to do?

int dstring_concatenate(DString* destination, DString source)
{
    assert(destination != NULL); // Precondition: destination ar ej NULL
    assert(*destination != NULL); // Precondition: *destination ar ej NULL
    assert(source != NULL); // Precondition: source ar ej NULL
    //Dstring looks like this = "typedef char* Dstring;"

    int memNewSize = strlen(*destination) + strlen(source);
    char *memTemp;
    memTemp = (char*)realloc(memTemp, sizeof(char) * memNewSize);

    printf("%d\n", memNewSize);
    printf("%d\n", strlen(memTemp));

    if(memTemp == NULL)
    {
        printf("Could not allocate new memory.\n");
        return 0;
    }
    else
    {
        *destination = memTemp;
        strcat(*destination, source);
        return 1;
    }
}

The problem here is, realloc() works ( properly ) on only

  • pointers earlier returned by allocator function
  • NULL pointers.

Quoting C11 , chapter §7.22.3.5

If ptr is a null pointer, the realloc function behaves like the malloc function for the specified size . Otherwise, if ptr does not match a pointer earlier returned by a memory management function, or if the space has been deallocated by a call to the free or realloc function, the behavior is undefined. [....]

In your case, memTemp (being an automatic storage local scoped variable) is just an unitialized pointer, with an indeterminate value , pointing to who knows what address! It's not even guaranteed to be NULL . So, all you have is undefined behavior .

Just a guess: Probably you meant to initialize memTemp with incoming *destination ?


That said, as pointed out in comments under the actual question,

  • The size multiplier supplied in realloc() should be memNewSize + 1 to be able to hold the null-terminator.
  • sizeof(char) is guaranteed to be 1 in C, so using it as a multiplier is redundant.

Alright, I got it to work, thanks so much guys! Updated code below.

int dstring_concatenate(DString* destination, DString source)
{
    assert(destination != NULL); // Precondition: destination ar ej NULL
    assert(*destination != NULL); // Precondition: *destination ar ej NULL
    assert(source != NULL); // Precondition: source ar ej NULL
    // Dstring look like this = "typedef char* Dstring;"

    int memNewSize = strlen(*destination) + strlen(source);
    char *memTemp;
    memTemp = (char*)realloc(*destination, sizeof(char) * memNewSize+1);

    if(memTemp == NULL)
    {
        printf("Could not allocate new memory.\n");
        return 0;
    }
    else
    {
        *destination = memTemp;
        strcat(*destination, source);
        return 1;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM