user-0 提问: malloc与free问题

回复:
user: user-0
content: 引用 7楼TWG8 的回复:1.首先，你使用指针p申请内存，但是你并没有使用这块内存，那我不知道你申请内存的意义在哪，直接定义一个指针就行。 2.p=&a[i] 改变指针的指向，指向了数组元素，所以最后free(p),p指向a[0] 等于释放了a[0] 。 但是a[0]是栈变量，函数执行完，程序自动释放，然而你又free，等于释放了2次。所以编译器 报错 double free 3.至于你第二种为什么可以，我也不清楚，但是理论上是和第一种是同一种情况。感谢。我是初学，这部分不清楚，您说的 我现在明白了

user: user-1
content: p运行到最后已经不指向开始时申请的内存了，如果调用free释放就会出错，第二种方式也是不对的，只是还没有报错而已吧

user: user-2
content: http://fanyi.baidu.com/#en/zh/Attempting%20to%20free%20an%20invalid%20pointer%20(a%20pointer%20to%20a%20memory%20block%20that%20was%20not%20allocated%20by%20calloc%2C%20malloc%2C%20or%20realloc)%20may%20affect%20subsequent%20allocation%20requests%20and%20cause%20errors.

user: user-2
content: free Deallocates or frees a memory block.  void free( void *memblock );  Function Required Header Compatibility  free <stdlib.h> and <malloc.h> ANSI, Win 95, Win NT    For additional compatibility information, see Compatibility in the Introduction.  Libraries  LIBC.LIB Single thread static library, retail version  LIBCMT.LIB Multithread static library, retail version  MSVCRT.LIB Import library for MSVCRT.DLL, retail version    Return Value  None  Parameter  memblock  Previously allocated memory block to be freed  Remarks  The free function deallocates a memory block (memblock) that was previously allocated by a call to calloc, malloc, or realloc. The number of freed bytes is equivalent to the number of bytes requested when the block was allocated (or reallocated, in the case of realloc). If memblock is NULL, the pointer is ignored and free immediately returns. Attempting to free an invalid pointer (a pointer to a memory block that was not allocated by calloc, malloc, or realloc) may affect subsequent allocation requests and cause errors.  After a memory block has been freed, _heapmin minimizes the amount of free memory on the heap by coalescing the unused regions and releasing them back to the operating system. Freed memory that is not released to the operating system is restored to the free pool and is available for allocation again.  When the application is linked with a debug version of the C run-time libraries, free resolves to _free_dbg. For more information about how the heap is managed during the debugging process, see Using C Run-Time Library Debugging Support.  Example  /* MALLOC.C: This program allocates memory with  * malloc, then frees the memory with free.  */  #include <stdlib.h>         /* For _MAX_PATH definition */ #include <stdio.h> #include <malloc.h>  void main( void ) {    char *string;     /* Allocate space for a path name */    string = malloc( _MAX_PATH );    if( string == NULL )       printf( "Insufficient memory available\n" );    else    {       printf( "Memory space allocated for path name\n" );       free( string );       printf( "Memory freed\n" );    } }   Output  Memory space allocated for path name Memory freed   Memory Allocation Routines  See Also   _alloca, calloc, malloc, realloc, _free_dbg, _heapmin

user: user-3
content: 第二个也有问题，只不过潜伏着呢

user: user-4
content: 感觉没区别啊，你指向你分配内存的那个指针指到别的地方了，然后你去释放一块根本不是malloc得来的空间，是有问题的啊 。

user: user-0

user: user-5
content: 正如7楼所说的，你根本就没用申请的内存，何来释放之说

user: user-6
content: 那个都不行。两个都会导致内存泄露。free释放的并不是malloc的空间。不能说运行没问题，就是程序没问题，这个是随机性的。  若用malloc的空间，就不能再将p指向a数组的某个元素或者a数组。 本身这个程序就可以不用malloc/free

user: user-7
content: 1.首先，你使用指针p申请内存，但是你并没有使用这块内存，那我不知道你申请内存的意义在哪，直接定义一个指针就行。 2.p=&a[i] 改变指针的指向，指向了数组元素，所以最后free(p),p指向a[0] 等于释放了a[0] 。 但是a[0]是栈变量，函数执行完，程序自动释放，然而你又free，等于释放了2次。所以编译器 报错 double free 3.至于你第二种为什么可以，我也不清楚，但是理论上是和第一种是同一种情况。

user: user-8
content: free释放的是malloc分配的地址，不是数组的地址