4
int getmin(int a, int b)
{
    return a<b?a:b;
}


void *reallocation(void *ptr, size_t size) //size_t in bytes
{

    void *newptr;


    int msize;
    msize = getsize(ptr);

    msize = getmin(msize, size);

        printf("msize = %d", msize);

    newptr = malloc(size);
    newptr = memcpy(newptr, ptr, msize);
    free(ptr);


    return newptr;

}

I have implemented my own realloc, and in order to get the size of the allocated memory using malloc(however i know there isn't any method for this in c).

My reallocation function is working fine on my system How do we get the size of the memory allocated by malloc().

Also can we do inplace reallocation if the size of the previously allocated memory is greater than the new required?

1
  • 1
    What is your overall goal? Are you just rewriting realloc or are you trying to create an entire set of memory allocation routines? Commented Jun 2, 2012 at 18:04

3 Answers 3

14

There is no portable way to get the size of memory allocated by malloc().

However, one can always do something like that to simulate what you want.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void myfree(void * p) {
    size_t * in = p;
    if (in) {
        --in; free(in);
    }
}

void * mymalloc(size_t n) {
    size_t * result = malloc(n + sizeof(size_t));
    if (result) { *result = n; ++result; memset(result,0,n); }
    return result;
}

size_t getsize(void * p) {
    size_t * in = p;
    if (in) { --in; return *in; }
    return -1;
}

#define malloc(_x) mymalloc((_x))
#define free(_x) myfree((_x))

void *reallocation(void *ptr,size_t size) {
    void *newptr;
    int msize;
    msize = getsize(ptr);
    printf("msize=%d\n", msize);
    if (size <= msize)
        return ptr;
    newptr = malloc(size);
    memcpy(newptr, ptr, msize);
    free(ptr);
    return newptr;
}
int main() {
    char * aa = malloc(50);
    char * bb ;
    printf("aa size is %d\n",getsize(aa));
    strcpy(aa,"my cookie");
    bb = reallocation(aa,100);
    printf("bb size is %d\n",getsize(bb));
    printf("<%s>\n",bb);
    free(bb);
}
Sign up to request clarification or add additional context in comments.

1 Comment

getsize shouldn't return -1 if the return type is declared unsigned
3

malloc does not initialize memory to zero. (calloc is the equivalent that does.) If you are seeing things set to zero, it's accidental.

I believe the library version of realloc uses length information in the heap that is not directly available. (And it may overestimate the original allocation, which means it might copy a little extra memory when using realloc to expand the allocation. This generally has no effect.)

realloc likely doesn't do a copy when shrinking an allocation.

Also, I should note that in same cases, you don't have to do a copy even when realloc increases the size, for example, if the next block in the heap is free.

4 Comments

It might not be accidentally set to zero, most OS's fill a newly allocated page with zeroes for security reasons.
@JustSid Yes, but you don't know that this is a newly allocated page. It may be previously allocated and freed memory.
Try this fun code in g++: int *ip=new int;*ip=42;delete ip;int *ip2 = new int; std::out << *ip2 << endl;. It'll happily print "42".
I never said that it was deterministic wether or not you get a new page. I just thought it was worth mentioning that its not accidentally zero in some cases.
1

the memory allocated by malloc gets initialized to zero, so i am checking for that condition.

That's incorrect. From the draft:

Description

2 The malloc function allocates space for an object whose size is specified by size and whose value is indeterminate.

Your getsize needs to be fixed.

My reallocation function is working fine.

You are not even fixing the alignment -- it may fail for certain types. Read this SO question.

Also can we do inplace reallocation if the size of the previously allocated memory is greater than the new required?

What would in-place reallocation mean? Shouldn't this be a simple no-op?

1 Comment

How can we fix the alignment?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.