关于realloc的用法问题

大胜警长 2011-10-22 09:30:58
代码如下:
char *q = (char*)malloc(10);
char *p = "123456";
p = (char*)realloc(p,20);
if(p==NULL)
cout<<"alloc bad"<<endl;
q = (char*)realloc(q,20);
if(p==NULL)
cout<<"alloc bad"<<endl;
printf("\n");

为什么指针p的realloc会失败,输出"alloc bad",而q不会。求解答???
...全文
121 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
h_j9527 2011-10-24
  • 打赏
  • 举报
回复
it must have been returned by an earlier call to malloc(), calloc() or realloc().
必须是用malloc或者calloc或者realloc申请的返回值
AnYidan 2011-10-23
  • 打赏
  • 举报
回复
realloc() 函数要求第一个参数必须是 malloc()或 calloc()的返回值
qinjuning 2011-10-23
  • 打赏
  • 举报
回复
学习 。。
goldbeef 2011-10-22
  • 打赏
  • 举报
回复
ptr
Pointer to a memory block previously allocated with malloc, calloc or realloc to be reallocated.
If this is NULL, a new block is allocated and a pointer to it is returned by the function.
B612 2011-10-22
  • 打赏
  • 举报
回复
"********" 为字面常量。不可改变
shenyan008 2011-10-22
  • 打赏
  • 举报
回复
realloc现在原地获取指定大小的内存,如果不能,那么需要另外分配,再free原来的内存;
char *p = "123456","123456"是字符串常量在常量区,在常量区分配和释放内存,都会错误。

void *realloc(void *ptr, size_t size);

If ptr is not a null pointer, it must be the address of an existing object that you first allocate by calling calloc, malloc, or realloc.

Unless ptr is NULL, it must have been returned by an earlier call to malloc(), calloc() or realloc().
ProgrammingRing 2011-10-22
  • 打赏
  • 举报
回复
realloc是用来改变动态内存大小的。。
柯本 2011-10-22
  • 打赏
  • 举报
回复
char *p = "123456";
p = (char*)realloc(p,20);
看下realloc的HELP吧。
void * realloc ( void * ptr, size_t size );
Reallocate memory block

The size of the memory block pointed to by the ptr parameter is changed to the size bytes, expanding or reducing the amount of memory available in the block.

The function may move the memory block to a new location, in which case the new location is returned. The content of the memory block is preserved up to the lesser of the new and old sizes, even if the block is moved. If the new size is larger, the value of the newly allocated portion is indeterminate.

In case that ptr is NULL, the function behaves exactly as malloc, assigning a new block of size bytes and returning a pointer to the beginning of it.

In case that the size is 0, the memory previously allocated in ptr is deallocated as if a call to free was made, and a NULL pointer is returned.


Parameters
ptr
Pointer to a memory block previously allocated with malloc, calloc or realloc to be reallocated.
If this is NULL, a new block is allocated and a pointer to it is returned by the function.
size
New size for the memory block, in bytes.
If it is 0 and ptr points to an existing block of memory, the memory block pointed by ptr is deallocated and a NULL pointer is returned.


它必须是用malloc, calloc or realloc 申请的指针才可用

大胜警长 2011-10-22
  • 打赏
  • 举报
回复
错了 是如下代码:
char *q = (char*)malloc(10);
char *p = "123456";
p = (char*)realloc(p,20);
if(p==NULL)
cout<<"alloc bad"<<endl;
q = (char*)realloc(q,20);
if(q==NULL)
cout<<"alloc bad"<<endl;
printf("\n");

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧