努力使用calloc和realloc [英] struggling to use calloc and realloc

查看:65
本文介绍了努力使用calloc和realloc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。我在FreeBSD下使用C和gcc编译器,使用calloc和realloc调用时遇到了麻烦。


例如代码片段:


#include< stdio.h>


int main(){


char * ptr;


ptr =(char *)calloc(1,sizeof(char));


printf("初始大小(1个字符)=%d \ n",sizeof(ptr));


ptr =(char *)realloc(ptr,sizeof(char)* 10);


printf(" new size(10 chars)=%d \ n",sizeof(ptr));


返回0 ;

}

然而当我运行它时,每个printf的大小为4,即使

最初我只分配了大小1个字符,在重新分配之后

应该是10个字节的空间......?或者我在

推理中哪里出错?


感谢您的帮助:)


解决方案

在文章< 3f ******** @ news1.mweb.co.za> ;, dagger< fe **** @ mweb。 co.za>写道:

你好。我在FreeBSD下使用C和gcc编译器,并且在使用calloc和realloc调用时遇到了一些麻烦。

例如代码片段:


[trimmed mercilessly] char * ptr;
printf(" initial size(1 char)=%d \ n",sizeof(ptr));
printf(" ;新尺寸(10个字符)=%d \ n",sizeof(ptr));
然而当我运行它时,每个printf的大小为4,即使最初我只分配了1个字符的大小,并且在重新分配之后
应该有10个字节的空间。 ..?或者我的
推理在哪里犯了错误?




sizeof(ptr)给出了指针的大小(并为此获得4) />
表示你​​在一台指针为4字节的机器上运行程序

宽,在这个时代,这表明台式机运行的是

32位操作系统)。

没有办法找回指针所指向的内存大小;

" sizeof * ptr"将给出ptr指向的任何类型的大小,

但是(特别是对于从malloc及其朋友那里获得的内存)你的

代码需要跟踪多少那些它有自己的空间。

dave


-

Dave Vandervies dj ****** @ csclub.uwaterloo.ca

是的,我想如果它真的想要它可以。我纠正了。当然,

我会质疑这样一个编译器的道德和饮酒习惯......

- 在comp.lang.c中的理查德希思菲尔德


"匕首" < FE **** @ mweb.co.za>在留言中写道

新闻:3f ******** @ news1.mweb.co.za ...

你好。我在FreeBSD下使用C和gcc编译器,并且在使用calloc和realloc调用时遇到了一些麻烦。

例如代码片段:

#include< stdio.h>


你没有收到警告吗?对于realloc和calloc,你也需要stdlib.h。

int main(){

char * ptr;

ptr =(char *) calloc(1,sizeof(char));


啊,由于演员阵容你没有得到警告,也没有投出分配函数的返回值

。 />
printf(" initial size(1 char)=%d \ n",sizeof(ptr));


sizeof(ptr)与sizeof(char *)相同,在你的系统sizeof(char *)是

4字节。

然而当我运行它时,每个printf的大小为4,即使最初我只分配了1个字符的大小,并且在重新分配
之后应该有10个字节的空间。 ..?或者我的推理在哪里出错?





你也不会指望这个。


int x = 100;

if(sizeof x == 100) )false ot true?


dagger< fe **** @ mweb.co.za>这样说:

#include< stdio.h>
int main(){
char * ptr;
ptr =(char *) calloc(1,sizeof(char));
^^^^^^^^

演员不需要,并且会隐藏有用的编译器警告

你不幸忘记了包括stdio.h。您应该在继续之前检查分配是否成功。
为什么不只是

使用malloc()?你可能想做的事情


malloc(sizeof(* ptr));

printf(" initial size(1 char)=%d \ n,sizeof(ptr));


ptr是一个指向角色的指针。 sizeof(ptr)是这样一个实体所需的空间

;在你的实现上,恰好是四个b
字节。这个数字与分配的内存(如果有的话)ptr指向的内存数量无关。

ptr =(char *)realloc(ptr,sizeof(char) * 10);


再次,失去演职员表。如果原始分配成功,但是这个

一个失败,你最初分配的内存现在已经永远消失了。

确保你可以释放()你在这个事件中分配的内容:


char * ptr,* tmp;


ptr = malloc(sizeof(* ptr));

tmp = ptr;

ptr = realloc(ptr,10 * sizeof(* ptr));

if(!ptr){

免费(tmp); / * tmp指向内存最初malloc()''ed,

并且该内存现已释放* /

/ *继续生活* /

}

else {

/ *用ptr做你想做的事* /

}

printf(new size(10 chars)=%d \ n,sizeof(ptr));




与上述相同的错误。


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。


Hi there. I''m using C under FreeBSD with the gcc compiler and am having a
bit of trouble using the calloc and realloc calls.

As an example the code snippet:

#include <stdio.h>

int main() {

char *ptr;

ptr = (char *) calloc(1, sizeof(char));

printf( "initial size (1 char) = %d\n", sizeof(ptr) );

ptr = (char *) realloc(ptr, sizeof(char)*10);

printf( "new size (10 chars) = %d\n", sizeof(ptr) );

return 0;
}
and yet when I run it I get a size of 4 for each printf even though
initially I allocated only the size of 1 char, and after reallocation there
should be room for 10 bytes...? Or where am I making a mistake in my
reasoning?

Thanks for any help :)


解决方案

In article <3f********@news1.mweb.co.za>, dagger <fe****@mweb.co.za> wrote:

Hi there. I''m using C under FreeBSD with the gcc compiler and am having a
bit of trouble using the calloc and realloc calls.

As an example the code snippet:
[trimmed mercilessly] char *ptr;
printf( "initial size (1 char) = %d\n", sizeof(ptr) );
printf( "new size (10 chars) = %d\n", sizeof(ptr) ); and yet when I run it I get a size of 4 for each printf even though
initially I allocated only the size of 1 char, and after reallocation there
should be room for 10 bytes...? Or where am I making a mistake in my
reasoning?



sizeof(ptr) gives you the size of the pointer (and getting 4 for that
means you''re running the program on a machine where pointers are 4 bytes
wide, which in this day and age suggests a desktop machine running a
32-bit operating system).
There''s no way to get back the size of the memory a pointer points to;
"sizeof *ptr" will give the size of one of whatever type ptr points at,
but (especially for memory obtained from malloc and its friends) your
code needs to keep track of how many of those it has space for itself.
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
Yes, I suppose it could if it really wanted. I stand corrected. Of course,
I''d question such a compiler''s morals and drinking habits...
--Richard Heathfield in comp.lang.c


"dagger" <fe****@mweb.co.za> wrote in message
news:3f********@news1.mweb.co.za...

Hi there. I''m using C under FreeBSD with the gcc compiler and am having a
bit of trouble using the calloc and realloc calls.

As an example the code snippet:

#include <stdio.h>
Didn''t you get a warning? For realloc and calloc you need stdlib.h too.
int main() {

char *ptr;

ptr = (char *) calloc(1, sizeof(char));
Ah, you didn''t get a warning because of the cast, don''t cast the return
value of the alloc functions.
printf( "initial size (1 char) = %d\n", sizeof(ptr) );
sizeof(ptr) is the same as sizeof(char *), on your system sizeof(char *) is
4 bytes.
and yet when I run it I get a size of 4 for each printf even though
initially I allocated only the size of 1 char, and after reallocation there should be room for 10 bytes...? Or where am I making a mistake in my
reasoning?



sizeof gives the size of the variable itself, not the size of its value.

You wouldn''t expect this either.

int x = 100;
if (sizeof x == 100) false ot true?


dagger <fe****@mweb.co.za> spoke thus:

#include <stdio.h>
int main() {
char *ptr;
ptr = (char *) calloc(1, sizeof(char)); ^^^^^^^^
The cast isn''t needed, and will hide helpful compiler warnings should
you have the misfortune to forget to include stdio.h. You should
check that the allocation succeeded before continuing. Why not just
use malloc()? And you might want to do

malloc( sizeof(*ptr) );
printf( "initial size (1 char) = %d\n", sizeof(ptr) );
ptr is a pointer to a character. sizeof(ptr) is the space required
by such an entity; on your implementation, it happens to be four
bytes. This number has no relation to how many bytes of allocated
memory, if any, ptr points to.
ptr = (char *) realloc(ptr, sizeof(char)*10);
Again, lose the cast. If the original allocation succeeds, but this
one fails, the memory you originally allocated is now gone forever.
Make sure you can free() what you allocated in this event:

char *ptr, *tmp;

ptr=malloc( sizeof(*ptr) );
tmp=ptr;
ptr=realloc( ptr, 10*sizeof(*ptr) );
if( !ptr ) {
free( tmp ); /* tmp pointed at the memory originally malloc()''ed,
and that memory is now freed */
/* go on with life */
}
else {
/* do what you want with ptr */
}
printf( "new size (10 chars) = %d\n", sizeof(ptr) );



Same error as above.

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


这篇关于努力使用calloc和realloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆