C中的内存分配问题
我即将读完一本关于 C 编程的介绍性书籍,名为“C 编程的简单步骤”,作者是 Mike McGrath。不过,从表面上看,我想读完这本书后我会学到更多东西。无论如何,我正在处理内存分配,并且编写了这个演示程序,但是当我尝试运行它时,它错误关闭:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, *arr;
arr = calloc(5, sizeof(int));
if(arr!=NULL)
{
printf("Enter 5 integers, seperated by a space:");
for(i=0; i<5; i++) scanf("%d", &arr[i]);
printf("Adding more space...\n");
arr = realloc(8, sizeof(int));
printf("Enter 3 more integers seperated by a space:");
for(i=5; i<8; i++) scanf("%d", &arr[i]);
printf("Thanks.\nYour 8 entries were: ");
for(i=0; i<8; i++) printf("%d, ", arr[i]);
printf("\n");
free(arr);
return 0;
}
else {printf("!!! INSUFFICIENT MEMORY !!!\n"); return 1; }
}
警告消息:
|13|warning: passing argument 1 of 'realloc' makes pointer from integer without a cast|
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\stdlib.h|365|note: expected 'void *' but argument is of type 'int'|
||=== Build finished: 0 errors, 1 warnings ===|
生成的编译要求 5 个整数并打印“添加更多空间...”,此时程序终止,而不是要求额外的三个整数并打印输入。
任何帮助都会很好。 :) 谢谢!
I'm nearing the end of an introductory book on c programming called "C programming in easy steps" by Mike McGrath. I think I'll have a lot more to learn after this book by the looks of things though. Anyways, I'm working with memory allocation and I wrote this demo program, but it errors closed when I try to run it:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, *arr;
arr = calloc(5, sizeof(int));
if(arr!=NULL)
{
printf("Enter 5 integers, seperated by a space:");
for(i=0; i<5; i++) scanf("%d", &arr[i]);
printf("Adding more space...\n");
arr = realloc(8, sizeof(int));
printf("Enter 3 more integers seperated by a space:");
for(i=5; i<8; i++) scanf("%d", &arr[i]);
printf("Thanks.\nYour 8 entries were: ");
for(i=0; i<8; i++) printf("%d, ", arr[i]);
printf("\n");
free(arr);
return 0;
}
else {printf("!!! INSUFFICIENT MEMORY !!!\n"); return 1; }
}
Warning message:
|13|warning: passing argument 1 of 'realloc' makes pointer from integer without a cast|
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\stdlib.h|365|note: expected 'void *' but argument is of type 'int'|
||=== Build finished: 0 errors, 1 warnings ===|
The resulting compilation asks for 5 integers and prints "Adding more space..." at which point the program terminates, instead of asking for the additional three integers and printing the input.
Any help would be nice. :) Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您对
realloc
的使用是错误的。您应该使用 realloc(arr, 8 * sizeof(int));
Your use of
realloc
is wrong.You should use
realloc(arr, 8 * sizeof(int));
首先你必须学习如何使用realloc。
您在屏幕上看到第一个警告的原因
您必须在分配 Calloc 返回的指针之前对其进行类型转换。这就是为什么您在没有强制转换的情况下收到警告的原因。
请注意,
我想您明白了。将其类型转换为您要分配的指针的类型。因为 calloc 返回 void 指针,并且在使用它之前必须将其类型转换为所需的数据类型
,据我所知,
这是使用指针的方法
,并记住将 arr 定义为整数指针,它将其地址增加 2
1 并不意味着 1,它意味着将其增加所指向的指针的大小。
好吧,我从来没有注意到这种指针语法
如果这也是使用指针的方式那么我很高兴接受它我学会了访问指针的其他方法
,你也可以使用
谢谢。
请仔细阅读这些
http://www.cplusplus.com/reference/clibrary/cstdlib/calloc/ 这些网站对于初学者来说确实是非常有前途的网站,我希望您喜欢,如有任何疑问,我们将随时为您提供帮助
At fist You gotta learn the the way to use realloc.
The reason for the 1st warning your seeing on your screen
You have to type cast the pointer returned by The calloc before it is being assigned.Thats why you got the warning without a cast.
Note
I guess you got the point.Typecast it into the type of the pointer your going to assign. because the calloc returns void pointer and you have to type cast into the data type you want before you use it
and As far to my Knowledge
This is the way to use pointers
and remember as your defining arr as integer pointer it increments its address by 2
1 does not mean by 1 it means increment it by the size of the pointer pointing to.
well I never noticed this kind of syntax for pointers
If that is also the way to use pointers then I'm glad to accept it I learned the other way to access pointers
well you can also use
Thanks.
and please go through these
http://www.cplusplus.com/reference/clibrary/cstdlib/calloc/ and these site is really really promsising site for beginners I hope you enjoy and any doubts we are here to help you
您没有按照应有的方式使用
realloc
:尝试:
顺便说一句,您的程序看起来非常拥挤,这使得它难以阅读。也许偶尔留一个空行?
You're not using
realloc
the way you should:Try:
As an aside, your program looks really crammed which makes it hard to read. Maybe leave an empty line once in a while ?
您需要将指针传递给要调整大小的内存:
You need to pass the pointer to the memory you want to resize:
realloc 的第一个参数应该是之前分配的指针
The first argument to
realloc
should be the previously allocated pointer查看 realloc 的工作原理 - 您必须向其传递原始指针:
Look up how
realloc
works - you have to pass it the original pointer: