C语言中的动态内存分配malloc calloc realloc free 等得具体格式是怎样的?

最好每个举一个小例子

malloc(配置内存空间)
相关函数 calloc,free,realloc,brk

表头文件 #include<stdlib.h>

定义函数 void * malloc(size_t size);

函数说明 malloc()用来配置内存空间,其大小由指定的size决定。

返回值 若配置成功则返回一指针,失败则返回NULL。

范例 void p = malloc(1024); /*配置1k的内存*/

calloc(配置内存空间)
相关函数 malloc,free,realloc,brk

表头文件 #include <stdlib.h>

定义函数 void *calloc(size_t nmemb,size_t size);

函数说明 calloc()用来配置nmemb个相邻的内存单位,每一单位的大小为size,并返回指向第一个元素的指针。这和使用下列的方式效果相同:malloc(nmemb*size);不过,在利用calloc()配置内存时会将内存内容初始化为0。

返回值 若配置成功则返回一指针,失败则返回NULL。

范例 /* 动态配置10个struct test 空间*/
#include<stdlib.h>
struct test
{
int a[10];
char b[20];
}
main()
{
struct test *ptr=calloc(sizeof(struct test),10);
}

 

free(释放原先配置的内存)
相关函数 malloc,calloc,realloc,brk

extern void *realloc(void *mem_address, unsigned int newsize);
  语法:指针名=(数据类型*)realloc(要改变内存大小的指针名,新的大小)。//新的大小一定要大于原来的大小不然的话会导致数据丢失!
  头文件:#include <stdlib.h> 有些编译器需要#include <alloc.h>,在TC2.0中可以使用alloc.h头文件
  功能:先按照newsize指定的大小分配空间,将原有数据从头到尾拷贝到新分配的内存区域,而后释放原来mem_address所指内存区域,同时返回新分配的内存区域的首地址。即重新分配存储器块的地址。
  返回值:如果重新分配成功则返回指向被分配内存的指针,否则返回空指针NULL。 
  注意:这里原始内存中的数据还是保持不变的。当内存不再使用时,应使用free()函数将内存块释放。

表头文件 #include<stdlib.h>

定义函数 void free(void *ptr);

函数说明 参数ptr为指向先前由malloc()、calloc()或realloc()所返回的内存指针。调用free()后ptr所指的内存空间便会被收回。假若参数ptr所指的内存空间已被收回或是未知的内存地址,则调用free()可能会有无法预期的情况发生。若参数ptr为NULL,则free()不会有任何作用。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-11-28
1.malloc 的格式:void *malloc( size_t size );返回的是一个空的指针所以应用时:
定义一个:int * a分配n个字节的空间:a = (int*)malloc(n);//一定要进行强制类型转换,你定义的说什么类型就强制转换成什么类型的。
2.calloc 的格式:void *calloc( size_t num, size_t size );参数1 num :Number of elements
参数2 size :Length in bytes of each element;
Example

/* CALLOC.C: This program uses calloc to allocate space for
* 40 long integers. It initializes each element to zero.
*/
#include <stdio.h>
#include <malloc.h>

void main( void )
{
long *buffer;

buffer = (long *)calloc( 40, sizeof( long ) );
if( buffer != NULL )
printf( "Allocated 40 long integers\n" );
else
printf( "Can't allocate memory\n" );
free( buffer );
}

Output
Allocated 40 long integers

3.void *realloc( void *memblock, size_t size );

Example

/* REALLOC.C: This program allocates a block of memory for
* buffer and then uses _msize to display the size of that
* block. Next, it uses realloc to expand the amount of
* memory used by buffer and then calls _msize again to
* display the new amount of memory allocated to buffer.
*/

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

void main( void )
{
long *buffer;
size_t size;

if( (buffer = (long *)malloc( 1000 * sizeof( long ) )) == NULL )
exit( 1 );

size = _msize( buffer );
printf( "Size of block after malloc of 1000 longs: %u\n", size );

/* Reallocate and show new size: */
if( (buffer = (long *)realloc( buffer, size + (1000 * sizeof( long )) ))
== NULL )
exit( 1 );
size = _msize( buffer );
printf( "Size of block after realloc of 1000 more longs: %u\n",
size );

free( buffer );
exit( 0 );
}

Output

Size of block after malloc of 1000 longs: 4000
Size of block after realloc of 1000 more longs: 8000

4.void free( void *memblock );

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 = (char *)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
第2个回答  2011-11-27
一般申请大的内存时使用:
char *string;
string = (char *)malloc(256); //申请256字节的字符串
free(string); //用完后记得释放内存
=======
string = (char *)malloc(128);
str = realloc(string, 256); //128太小,重新申请256大小。

这些用法可以搜索“C库函数大全”,里面有各种库函数的详细用法和举例
相似回答