malloc和realloc的关系,它是怎样处理时所需的空间不可用内存 [英] Malloc and Realloc relation, How does it handle when the required space is not available in memory

查看:133
本文介绍了malloc和realloc的关系,它是怎样处理时所需的空间不可用内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
   realloc的()和malloc函数


 #包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
无效的主要()
{
  INT * P;
  p值= malloc的(6);
  p值= realloc的(P,10);
  如果(P == NULL)
  {
    的printf(错误); //什么时候p指向为null考虑我在PRGRM足够的空间
                     //内存区而不是在内存中的realloc试图搜索
                     //为记忆,我不知道如何解释,试图undrstnd
   出口(1);
   }
}

拿这个例子为code,假定总内存为10字节,两个字节用于指针的声明由malloc函数剩下的2个字节被其他程序占用为int类型和ohter 6个字节,现在当我运行realloc函数来扩展该指针所指向的内存,它会搜索10个字节的内存,当它不存在,它分配从堆区10个字节的内存和malloc的副本内容并粘贴到新的分配在堆区内存区域,然后删除记忆存储在malloc的吧?

是否realloc()的返回NULL指针,因为内存不可用?无权利!?它不会去堆区域的内存分配吧?它不返回NULL指针吧?

听我说:
| 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 |

考虑这个存储器模块:
假定01至06已被用于由malloc()FUNC,07和08是自由和最后2块I,E 09和10正在使用的其他程序的存储器。现在,当我打电话的realloc(P,10)我需要10个字节,但只有2个可用字节,那么什么realloc的呢?返回一个空指针或分配内存形成堆区和内存01到06块的内容复制到堆区内存,请让我知道。


解决方案

的malloc


  1. 这将分配的内存块(如果可用),否则将返回NULL。

的realloc


  1. 如果传递的尺寸比现有的块greated那么这将试图扩大现有存储器,如果它在扩大其将返回相同指针成功。

  2. 否则如果没有exapand然后它会分配新的内存块,它会从旧的内存块新的内存块复制旧的数据。然后,它会释放旧块,它将返回新块地址。

  3. 如果新分配的内存块失败则只会返回NULL,而不释放旧的内存块。

  4. 如果通过大小为零的的realloc 功能,那么它只会释放旧的内存块,并返回NULL。 的realloc(PTR,0)等同于免费(PTR)

  5. 如果传递给的realloc 函数大小比旧的内存大小block`s较小,那么它会缩小存储器。

回答您的方案


听我说:| 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 |认为这是存储器块:假定01至06已被用于由malloc()
FUNC,07和08是I,E 09和10都在使用免费的和最后2个街区
其他程序的内存。现在,当我打电话的realloc(P,10)我需要10个字节,但
只有2个可用字节,那么什么realloc的呢?返回NULL指针
或分配内存形成堆区和01的内容复制到06
内存在堆区的内存块,请让我知道。

是的,这将复制的内容 01 06 从旧的内存块新的内存块,它会释放旧的内存块,然后将返回新内存块的地址。

Possible Duplicate:
realloc and malloc functions

#include<stdio.h>
#include<stdlib.h>
void main()
{
  int *p;
  p = malloc(6);
  p = realloc(p, 10);
  if (p == NULL)
  {
    printf("error"); // when does p point to null consider i have enough space in prgrm
                     //memory area but not in memory where realloc is trying to search 
                     //for the memory, I dont know how to explain that try to undrstnd
   exit(1);
   }
}

Take this example for the code, suppose the total memory is 10 bytes and 2 bytes is used by the declaration of pointer to type int and ohter 6 bytes by malloc function the remaining 2 bytes is occupied by other programs, now when i run realloc function to extend the memory that pointer is pointing to, it will search for 10 bytes in memory and when it is not available it allocates 10 bytes of memory from heap area and copies contents of malloc and paste it in new allocated memory area in heap area and then delete the memory stored in malloc right?

Does realloc() return a NULL pointer because the memory is not available? No right!? It does go to heap area for the memory allocation right? It does not return a NULL pointer right?

Listen to me: | 01 | 02 | 03 | 04 | 05 | 06 | 07 |08 |09 | 10 |

consider this as memory blocks: assume that 01 to 06 has been used by malloc() func, 07 and 08 are free and last 2 blocks i,e 09 and 10 are being used by memory of other programs. Now when i call realloc(p,10) i need 10 bytes but there are only 2 free bytes, so what does realloc do? return a NULL pointer or allocate memory form the heap area and copy the contents of 01 to 06 blocks of memory to that memory in the heap area, please let me know.

解决方案

malloc

  1. This will allocate the memory block if it is available, or else it will return NULL.

realloc

  1. If the size passed is greated than the existing block then this will tries to expand the existing memory, if it succeeds in expanding it will return the same pointer.
  2. Or else if its failed to exapand then it will allocate the new memory block and it will copy the old data from the old memory block to new memory block. Then it will free the old block and it will return the new blocks address.
  3. If allocating new memory block failed then it will simply return NULL, without freeing old memory block.
  4. If size passed is zero to the realloc function, then it will just free the old memory block and return NULL. realloc(ptr, 0) is equivalent to free(ptr).
  5. If the size passed to the realloc function is lesser than the old memory block`s size, then it will shrink the memory.

Answer to your scenario

Listen to me: | 01 | 02 | 03 | 04 | 05 | 06 | 07 |08 |09 | 10 |

consider this as memory blocks: assume that 01 to 06 has been used by malloc() 
func, 07 and 08 are free and last 2 blocks i,e 09 and 10 are being used by 
memory of other programs. Now when i call realloc(p,10) i need 10 bytes but 
there are only 2 free bytes, so what does realloc do? return a NULL pointer 
or allocate memory form the heap area and copy the contents of 01 to 06 
blocks of memory to that memory in the heap area, please let me know.

Yes it will copy content of 01 to 06 from old memory block to new memory block and it will free the old memory block and then it will return the address of new memory block.

这篇关于malloc和realloc的关系,它是怎样处理时所需的空间不可用内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆