-1

can you please check this code to see why my implementation of a realloc function without using memcpy doesn't work? I am trying to figure out a way to transfer the payload size and tags from one block to another block. I got an invalid pointer error when i tried to run this code.

How do I transfer payload from one block to another without using memcpy or any other native c memory copy functions.

void* realloc(void* ptr, size_t size) {
  BlockInfo * oldBlockInfo;
  BlockInfo* newBlockInfo;
  size_t ptrSize;
  void* newPtr;
  if(ptr == NULL){
    return malloc(size);
  }
  else{
    if(size == 0){
        free(ptr);
        return NULL;
    }
  }
  oldBlockInfo = (BlockInfo*) UNSCALED_POINTER_SUB(ptr, WORD_SIZE);
  ptrSize = SIZE(oldBlockInfo->sizeAndTags); //getting the size of the old block
  //checking size
  if (ptrSize >= size){//if old size is greater or equal return old pointer
    return ptr;
  }
  else{
    newPtr = malloc(size);
    newBlockInfo = (BlockInfo*)UNSCALED_POINTER_ADD(newPtr,WORD_SIZE); 
    ptrSize = SIZE(newBlockInfo->sizeAndTags);
    for (int i = WORD_SIZE;i<ptrSize;i+=WORD_SIZE){
      newBlockInfo ->sizeAndTags = oldBlockInfo ->sizeAndTags;
      newBlockInfo = newBlockInfo ->next;
      oldBlockInfo = oldBlockInfo ->next;
    }
  }
  //examine_heap();
  free(ptr);
  return newPtr;
}

this is an implementation of realloc with memcpy, but I can't use memcpy

void *mm_realloc (void *ptr, size_t size) {
    int minsize;
    void *newptr;

    // Allocate new block, returning NULL if not possible.

    newptr = malloc (size);
    if (newptr == NULL) return NULL;

    // Don't copy/free original block if it was NULL.

    if (ptr != NULL) {
        // Get size to copy - mm_getsize must give you the size of the current block.
        // But, if new size is smaller, only copy that much. Many implementations
        // actually reserve the 16 bytes in front of the memory to store this info, e.g.,
        // +--------+--------------------------------+
        // | Header | Your data                      |
        // +--------+--------------------------------+
        //           ^
        //           +--- this is your pointer.
        // <- This is the memory actually allocated ->

        minsize = mm_getsize (ptr);
        if (size < minsize)
           minsize = size;

        // Copy the memory, free the old block and return the new block.

        memcpy (newptr, ptr, minsize);
        free (ptr)
    }

    return newptr;
}
Killerbee
  • 1
  • 2

1 Answers1

0

You should copy just the payload of the old block into the new block. So don't overwrite ptrSize with the information about the new block, you need the old block's size. Subtract WORD_SIZE from it because you're not copying the header, since that was filled in properly by malloc().

    newPtr = malloc(size);
    ptrSize -= WORD_SIZE; // subtract the header size
    char *tempPtr = newPtr;
    for (int i = 0; i<ptrSize; i++){
      tempPtr[i] = ptr[i];
    }
Barmar
  • 741,623
  • 53
  • 500
  • 612