memset仅用于字符串吗? [英] Is memset used only for strings?
问题描述
大家好,我遇到了问题,请看下面的问题:
int * bit =(int *)malloc(10000 * sizeof(int));
memset(bit,1,10000 * sizeof(int));
printf("%d%d%d \ n",bit [1],bit [2],位[9999]);
输出:16843009 16843009 16843009
显然我将位[0]设置为位[ 9999]到1,但输出不是
1'的。
为什么会这样?我是否误用了memset或者其他?
任何帮助都表示赞赏。谢谢!
Hi, guys,I met a problem, Please look at the problem below:
int* bit = (int*)malloc(10000*sizeof(int));
memset(bit, 1, 10000*sizeof(int));
printf("%d %d %d\n", bit[1],bit[2], bit[9999]);
Output: 16843009 16843009 16843009
Obviously I set the bit[0] to bit[9999] to 1, but it outputs are not
1''s.
Why is this happen? Do I misused memset or sth else?
Any help is appreciated. Thanks!
推荐答案
Frederick Ding说:
Frederick Ding said:
伙计们,我遇到了问题,请看下面的问题:
int * bit =(int *)malloc(10000 * sizeof(int));
int * bit = malloc(10000 * sizeof * bit);
if(bit!= NULL)
{
memset(bit,1,10000 * sizeof(int));
哎呀 - 这不会做你想做的事。
printf("%d%d%d \ n",bit [ 1],位[2],位[9999]);
输出:16843009 16843009 16843009
显然我将位[0]设置为位[9999]为1 ,但它的输出不是
1'。
Hi, guys,I met a problem, Please look at the problem below:
int* bit = (int*)malloc(10000*sizeof(int));
int *bit = malloc(10000 * sizeof *bit);
if(bit != NULL)
{
memset(bit, 1, 10000*sizeof(int));
Oops - that won''t do what you were hoping.
printf("%d %d %d\n", bit[1],bit[2], bit[9999]);
Output: 16843009 16843009 16843009
Obviously I set the bit[0] to bit[9999] to 1, but it outputs are not
1''s.
观察这个魔法:
unsigned char * p =(unsigned char *)位;
while(p<(unsigned char *)(bit + 1))
{
printf("%d \ n",* p ++)
}
打印第一个int中的每个字节。尝试一下,我想你会理解所有关于memset的信息。
-
Richard Heathfield >
Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk
电子邮件:rjh在上面的域名(但显然放弃了www)
Observe this magic:
unsigned char *p = (unsigned char *)bit;
while(p < (unsigned char *)(bit + 1))
{
printf("%d\n", *p++)
}
This prints every byte in your first int. Try it, and I think you''ll
understand all about memset.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Frederick Ding写道:
Frederick Ding wrote:
大家好,我遇到了一个问题,请看下面的问题:
int * bit =(int *)malloc(10000 * sizeof(int));
memset (bit,1,10000 * sizeof(int));
printf("%d%d%d \ n",bit [1],bit [2],bit [9999]);
显然我将位[0]设置为位[9999]为1,但输出不是
1'。
为什么会这样?我是否误用了memset或者其他?
任何帮助都表示赞赏。谢谢!
Hi, guys,I met a problem, Please look at the problem below:
int* bit = (int*)malloc(10000*sizeof(int));
memset(bit, 1, 10000*sizeof(int));
printf("%d %d %d\n", bit[1],bit[2], bit[9999]);
Output: 16843009 16843009 16843009
Obviously I set the bit[0] to bit[9999] to 1, but it outputs are not
1''s.
Why is this happen? Do I misused memset or sth else?
Any help is appreciated. Thanks!
你对memset()的调用将每个字节设置为1.你的int可能占用4
字节,所以你初始化每个int位模式0x01010101,其中
等于16843009.
Bj?rn
Your call to memset() sets each byte to 1. Your ints probably occupies 4
bytes, so you initialize each int with bit patterns 0x01010101, which
equals 16843009.
Bj?rn
" Frederick丁" <二******* @ gmail.com>写道:
"Frederick Ding" <di*******@gmail.com> writes:
大家好,我遇到了一个问题,请看下面的问题:
int * bit =(int *)malloc(10000 * sizeof(int) );
不要施放malloc的结果。它可以掩盖错误,例如失败
以包含< stdlib.h>或者用C ++编译器编译C代码。
推荐的成语是:
int * bit = malloc(10000 * sizeof * bit) ;
你应该检查malloc()调用是否成功。
memset(bit,1,10000 * sizeof(int));
这会将数组的每个字节设置为1.
printf("%d%d%d \ n",bit [1],位[2],位[9999]);
输出:16843009 16843009 16843009
显然我将位[0]设置为位[9999]为1,但它输出不是1'。
Hi, guys,I met a problem, Please look at the problem below:
int* bit = (int*)malloc(10000*sizeof(int));
Don''t cast the result of malloc. It can mask errors such as failing
to include <stdlib.h> or compiling C code with a C++ compiler.
The recommended idiom is:
int *bit = malloc(10000 * sizeof *bit);
You should check whether the malloc() call succeeded.
memset(bit, 1, 10000*sizeof(int));
This sets every byte of the array to 1.
printf("%d %d %d\n", bit[1],bit[2], bit[9999]);
Output: 16843009 16843009 16843009
Obviously I set the bit[0] to bit[9999] to 1, but it outputs are not
1''s.
16843009十六进制是0x01010101。
memset()设置每个字节到指定的值。没有等价的
标准函数将每个int设置为指定值。
-
Keith Thompson( The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst>
圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>
我们必须做点什么。这是事情。因此,我们必须这样做。
16843009 in hexadecimal is 0x01010101.
memset() sets every byte to a specified value. There''s no equivalent
standard function to set every int to a specified value.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
这篇关于memset仅用于字符串吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!