关于malloc()和calloc()的问题 [英] A question on malloc() and calloc()

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

问题描述

大家好,

我试过2个程序:


#include< stdio.h>

#include< ; string.h>


1,

int main(void){

char * str = NULL;


str =(char *)malloc(sizeof(char *)* 5);

strcpy(str," abcd");

str [0] ='s'';


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

free(str );

返回0;

}

-------------------- ----------

2,

#include< stdio.h>

#include< string。 h>


int main(void){

char * str = NULL;


str =(char *)calloc(sizeof(char *)* 5);

strcpy(str," abcd");

str [0] =''s'';


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

free(str);

返回0;

}

------------------------------

1,可以正常工作,但是当我执行2时,会发生Segmentation故障d。

正好在strcpy(str,abcd);


7 str =(char *)calloc(sizeof(char *)* 5 );

(gdb)s

8 strcpy(str," abcd");

(gdb)s

程序收到信号SIGSEGV,分段错误。

0x42079df6 strcpy()来自/lib/tls/libc.so.6


我以为calloc和malloc之间的区别在于calloc()

将初始指针指向NULL或0.但这确实与上面的

答案有关。??


谢谢。

Hi all,
I tried 2 programs :

#include <stdio.h>
#include <string.h>

1,
int main(void){
char *str = NULL;

str = (char *)malloc(sizeof(char *)*5);
strcpy(str,"abcd");
str[0] = ''s'' ;

printf("%s\n",str);
free(str);
return 0;
}
------------------------------
2,
#include <stdio.h>
#include <string.h>

int main(void){
char *str = NULL;

str = (char *)calloc(sizeof(char *)*5);
strcpy(str,"abcd");
str[0] = ''s'' ;

printf("%s\n",str);
free(str);
return 0;
}
------------------------------
1, can work well , but when I execute 2, Segmentation fault happened.
Exactly at strcpy(str,"abcd");

7 str = (char *)calloc(sizeof(char *)*5);
(gdb) s
8 strcpy(str,"abcd");
(gdb) s
Program received signal SIGSEGV, Segmentation fault.
0x42079df6 in strcpy () from /lib/tls/libc.so.6

I thought the difference between calloc and malloc is that calloc()
will initial pointer with NULL or 0. But that does related with the
answer above.??

Thanks.

推荐答案

Ro ***** @ gmail.com 写道:
大家好,
我尝试了2个程序:

#include< ; stdio.h>
#include< string.h>
忘了包含stdlib.h - 这就是声明malloc / calloc

函数的地方。

1,
int main(void ){
char * str = NULL;

str =(char *)malloc(sizeof(char *)* 5);
避免不需要的强制转换,比如返回malloc。

还要记得检查返回值,它可能

出错时返回NULL。


你为5个字符指针分配空间。

可能你只需要为

5个字符分配空间。由于sizeof(char)是1,它只是


str = malloc(5);

strcpy(str," abcd") ;
str [0] ='s'';

printf("%s \ n",str);
free(str);
返回0;
}
------------------------------
2,
#include< stdio.h>
#include< string.h>

int main(void){
char * str = NULL;

str =(char *)calloc(sizeof(char *)* 5);


calloc需要2个参数。


阅读malloc和calloc文档。

既然它出现了你''在linux机器上运行 - 运行

man calloc

str = calloc(5,1);

strcpy(str," abcd");
str [0] ='s'';

printf("%s \ n",str);
free(str);
返回0;
}
------------------------------
1,可以正常工作,但是当我执行2时,发生了分段错误。
正好在strcpy(str,abcd);

7 str =(char *)calloc(sizeof (char *)* 5);
(gdb)s
8 strcpy(str," abcd");
(gdb)s
程序接收信号SIGSEGV,分段错误。
来自/lib/tls/libc.so.6的strcpy()0x42079df6

我认为calloc和malloc之间的区别在于calloc()
将初始指针与NULL或0.但这确实与上面的
答案有关。??
Hi all,
I tried 2 programs :

#include <stdio.h>
#include <string.h> Forgot to include stdlib.h - that''s where the malloc/calloc
functions are declared.
1,
int main(void){
char *str = NULL;

str = (char *)malloc(sizeof(char *)*5); Avoid uneeded casts, like for the return of malloc.
Also remember to check the return value, it might
return NULL on error.

You allocate space for 5 char pointers.
Likely you only need to allocate space for
5 chars. Since sizeof(char) is 1, it''ll just be

str = malloc(5);
strcpy(str,"abcd");
str[0] = ''s'' ;

printf("%s\n",str);
free(str);
return 0;
}
------------------------------
2,
#include <stdio.h>
#include <string.h>

int main(void){
char *str = NULL;

str = (char *)calloc(sizeof(char *)*5);
calloc takes 2 arguments.

Read up on the malloc and calloc documentation.
Since it appears you''re on a linux machine - run
man calloc
str = calloc(5,1);
strcpy(str,"abcd");
str[0] = ''s'' ;

printf("%s\n",str);
free(str);
return 0;
}
------------------------------
1, can work well , but when I execute 2, Segmentation fault happened.
Exactly at strcpy(str,"abcd");

7 str = (char *)calloc(sizeof(char *)*5);
(gdb) s
8 strcpy(str,"abcd");
(gdb) s
Program received signal SIGSEGV, Segmentation fault.
0x42079df6 in strcpy () from /lib/tls/libc.so.6

I thought the difference between calloc and malloc is that calloc()
will initial pointer with NULL or 0. But that does related with the
answer above.??




也许错误使用calloc - 它需要2个参数和/或

忘记了#include< stdlib.h> ?


我假设您正在使用gcc,在这种情况下,您应该/ attaast /编译

并使用-Wall标记到gcc。



Maybe the wrong use of calloc - it takes 2 arguments and/or the
forgotten #include <stdlib.h> ?

I''m assuming you''re using gcc in which case you should /atleast/ compile
with the -Wall flag to gcc.


您有很多错误,但并非所有错误都可能导致问题。



代码line


str =(char *)calloc(sizeof(char *)* 5);


错了。你在调用calloc时参数太少,它的原型是


void * calloc(size_t nelem,size_t elsize);

见这里


此外,在这两种情况下,您都试图分配 sizeof(char *)* 5 ,然后将字符串复制为abcd。至。字符串abcd是5个字节长,每个字母加1个终结符。


然而 sizeof(char *)* 5 取决于 sizeof(char * ),这可能是4个字节(但可能更低或更高,具体取决于目标计算机系统),因此您要分配20个字节的数据。我认为您可能意味着 sizeof(char)* 5


malloc获取您希望分配的字节数,calloc获取元素数量和你希望分配的元素的大小(然后在内部将它们相乘以获得字节数)。


我认为你的malloc和colloc行应分别为

You have a number of errors although not all of them may be contributing to the problem.


The code line

str = (char *)calloc(sizeof(char *)*5);

is wrong. You have too few parameters in the call to calloc, it''s prototype is

void *calloc(size_t nelem, size_t elsize);

See here

Additionally in both cases you are attempting to allocate sizeof(char *)*5 which you then copy the string "abcd" to. The string "abcd" is 5 bytes long, 1 for each letter plus the terminator.

However sizeof(char *)*5 is dependent on sizeof(char *), this is likely to be 4 bytes (but may be lower or higher depending on the target computer system) so you are allocating 20 bytes of data. I think that you probably mean sizeof(char) * 5.

malloc takes the number of bytes you wish to allocate and calloc takes the number of elements and the size of the elements that you wish to allocate (which it then multiples together internally to get the number of bytes).

I think that your malloc and colloc lines should respectively be

展开 | 选择 | Wrap | 行号


Ro * **** @gmail.com 写道:
大家好,<我尝试了2个程序:

#include< stdio.h>
#include< string.h>

1,
int main(void){
char * str = NULL;

str =(char *)malloc(sizeof(char *)* 5);
strcpy(str," ; abcd");
str [0] ='s'';

printf("%s \ n",str);
free(str) ;
返回0;
}
------------------------------
2,
#include< stdio.h>
#include< string.h>

int main(void){
char * str = NULL ;

str =(char *)calloc(sizeof(char *)* 5);
strcpy(str," abcd");
str [0] =' 's'';

printf("%s \ n",str);
免费(str);
返回0;
}
------------------------------
1,可以正常工作,但是当我执行2时,分段错误发生了。
正好在strcpy(str,abcd);

7 str =(char *)calloc(sizeof(char *)* 5);
(gdb )s
8 strcpy(str," abcd");
(gdb)s
程序收到信号SIGSEGV,分段错误。
来自/ lib / tls / libc的strcpy()中的0x42079df6。所以我认为calloc和malloc之间的区别在于calloc()
将初始指针用NULL或0.但这确实与上面的
答案有关。 ?

谢谢。
Hi all,
I tried 2 programs :

#include <stdio.h>
#include <string.h>

1,
int main(void){
char *str = NULL;

str = (char *)malloc(sizeof(char *)*5);
strcpy(str,"abcd");
str[0] = ''s'' ;

printf("%s\n",str);
free(str);
return 0;
}
------------------------------
2,
#include <stdio.h>
#include <string.h>

int main(void){
char *str = NULL;

str = (char *)calloc(sizeof(char *)*5);
strcpy(str,"abcd");
str[0] = ''s'' ;

printf("%s\n",str);
free(str);
return 0;
}
------------------------------
1, can work well , but when I execute 2, Segmentation fault happened.
Exactly at strcpy(str,"abcd");

7 str = (char *)calloc(sizeof(char *)*5);
(gdb) s
8 strcpy(str,"abcd");
(gdb) s
Program received signal SIGSEGV, Segmentation fault.
0x42079df6 in strcpy () from /lib/tls/libc.so.6

I thought the difference between calloc and malloc is that calloc()
will initial pointer with NULL or 0. But that does related with the
answer above.??

Thanks.




你犯了两个非常非常严重的错误,我会告诉你你到底是什么意思>
做错了,所以要密切注意。首先,忽略calloc()和malloc()之间的

差异,当你想为5个字符分配

空间时,你应该运行malloc(sizeof(char) * 5)。你在
做了什么,malloc(sizeof(char *)* 5),分配20或40字节的内存

(取决于CPU),而不是5。 char *是一个指向char的指针,一个指针

到char是4bytes big(在32位CPU上)或8bytes big(在64bit
CPU上)。但是,sizeof(char)是1byte big,这就是你想要的。

结论:


坏:

malloc(sizeof(char *)* 5)//将分配20或40个字节。


好​​:

malloc(sizeof(char)* 5) //将分配5个字节。


其次,关于calloc()。 calloc()的手册页说是

calloc(size_t count,size_t size),这意味着

" calloc(how_many,how_big) )" ;.让我们说你有一个struct

crackerjacks这是20字节大,你想为100个crackerjack分配足够的内存,你可以调用calloc(100,sizeof(struct

crackerjacks));或者如果你想为10个字符留下足够的内存,你可以

做calloc(10,sizeof(char))。


是的,如你所说, calloc()实际上用0填充内存,

不像malloc。


如果你想要任何有关C的实时建议,请关注AIM ,我的屏幕

名称是rutski89。



You''ve made two very very grave errors, I''ll tell you exactly what you
did wrong, so pay close attention. First of all, disregarding the
differences between calloc() and malloc(), when you want to allocate
space for 5 characters you should run malloc(sizeof(char) * 5). What you
did, malloc(sizeof(char *) * 5), allocates 20 or 40 bytes of memory
(dependent on the CPU), not 5. "char *" is a pointer to char, a pointer
to char is either 4bytes big (on a 32bit CPU) or 8bytes big (on a 64bit
CPU). However, sizeof(char) is 1byte big, which is what you want there.
In conclusion:

Bad:
malloc(sizeof(char *) * 5) // Will allocate 20 or 40 bytes.

Good:
malloc(sizeof(char) * 5) // Will allocate 5 bytes.

Secondly, about calloc(). The man page for calloc() says that is
"calloc(size_t count, size_t size)", this translates to mean
"calloc(how_many, how_big)". Let''s say that you have a "struct
crackerjacks" that is 20bytes big, and you want to allocate enough
memory for 100 crackerjacks, you could call "calloc(100, sizeof(struct
crackerjacks))"; or if you wanted enough memory for 10 chars, you could
do calloc(10, sizeof(char)).

And yes, as you mentioned, calloc() does in fact fill the memory with 0,
unlike malloc.

If you ever want any live advice about C, look me up on AIM, my screen
name is rutski89.


这篇关于关于malloc()和calloc()的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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