百度网盘网页版:realloc的问题

来源:百度文库 编辑:神马品牌网 时间:2024/04/30 08:43:48
data=(char*)realloc(data,datalen+len);
这句在我的程序中有的地方对有的地方错,data是全局的
为什么会出现这样的错误提示啊
Unhandled exception in My.exe:0xC0000005:Access Violation.

函数名: calloc

功 能: 分配主存储器

用 法: void *calloc(size_t nelem, size_t elsize);

程序例:

#include <stdio.h>

#include <alloc.h>

int main(void)

{

char *str = NULL;

/* allocate memory for string */

str = calloc(10, sizeof(char));

/* copy "Hello" into string */

strcpy(str, "Hello");

/* display string */

printf("String is %s\n", str);

/* free memory */

free(str);

return 0;

}
函数名: free

功 能: 释放已分配的块

用 法: void free(void *ptr);

程序例:

#include <string.h>

#include <stdio.h>

#include <alloc.h>

int main(void)

{

char *str;

/* allocate memory for string */

str = malloc(10);

/* copy "Hello" to string */

strcpy(str, "Hello");

/* display string */

printf("String is %s\n", str);

/* free memory */

free(str);

return 0;

}
函数名: malloc

功 能: 内存分配函数

用 法: void *malloc(unsigned size);

程序例:

#include <stdio.h>

#include <string.h>

#include <alloc.h>

#include <process.h>

int main(void)

{

char *str;

/* allocate memory for string */

/* This will generate an error when compiling */

/* with C++, use the new operator instead. */

if ((str = malloc(10)) == NULL)

{

printf("Not enough memory to allocate buffer\n");

exit(1); /* terminate program if out of memory */

}

/* copy "Hello" into string */

strcpy(str, "Hello");

/* display string */

printf("String is %s\n", str);

/* free memory */

free(str);

return 0;

}

函数名: realloc

功 能: 重新分配主存

用 法: void *realloc(void *ptr, unsigned newsize);

程序例:

#include <stdio.h>

#include <alloc.h>

#include <string.h>

int main(void)

{

char *str;

/* allocate memory for string */

str = malloc(10);

/* copy "Hello" into string */

strcpy(str, "Hello");

printf("String is %s\n Address is %p\n", str, str);

str = realloc(str, 20);

printf("String is %s\n New address is %p\n", str, str);

/* free memory */

free(str);

return 0;

}