memset无法按预期工作 [英] memset doesn't work as expected

查看:95
本文介绍了memset无法按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我分配了一块内存并使用memset将其设置为0.

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

int * graph = new int [16];

memset(graph,0,sizeof(graph));

for(int i = 0; i< 4; i ++){

for(int j = 0; j< 4; j ++)

cout<< graph [i * n + j]<<" " ;;

cout<< endl;

}

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

但我发现输出真的很奇怪 - graph [0] [1]总是

a大号。

我预计输出都是0.

I allocated a piece of memory and use memset to set it to 0.
------------------------------------
int *graph = new int[16];
memset(graph, 0, sizeof(graph));
for(int i=0;i<4;i++){
for(int j=0;j<4;j++)
cout<<graph[i*n+j]<<" ";
cout<<endl;
}
--------------------------------
But I found that the output is really strange -- graph[0][1] is always
a large number.
I expected the output will all be 0.

推荐答案

或者我可以新建一块记忆并将其平均设置为零?
or can I new a piece of memory and set it to zero in the mean time?


thomas写道:
thomas wrote:

我分配了一块内存并使用memset将其设置为0.

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

int * graph = new int [16];

memset(graph,0,sizeof(graph));
I allocated a piece of memory and use memset to set it to 0.
------------------------------------
int *graph = new int[16];
memset(graph, 0, sizeof(graph));



sizeof(graph)应该是sizeof(int *) - 绝对不是sizeof

(int)* 16。所以你只是将

数组中的第一个sizeof(int *)字节设置为零。


尝试memset(graph,0,sizeof(int) * 16)相反。

sizeof (graph) should be sizeof (int *) - which is definitely not sizeof
(int)*16. So you are only setting the first sizeof(int *) bytes in the
array to zero.

Try memset (graph, 0, sizeof (int)*16) instead.


2月20日,13:28,thomas< FreshTho ... @ gmail.comwrote:
On 20 Feb., 13:28, thomas <FreshTho...@gmail.comwrote:

我分配了一块内存并使用memset将其设置为0.

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

int * graph = new int [16];

memset(graph,0,sizeof( (图));

for(int i = 0; i< 4; i ++){

* * for(int j = 0; j< 4; j ++)

* * * * cout<< graph [i * n + j]<<" " ;;

* * cout<< endl;}


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

但我发现输出真的很奇怪 - graph [0] [1]总是

a大数。

我预计输出都是0.
I allocated a piece of memory and use memset to set it to 0.
------------------------------------
int *graph = new int[16];
memset(graph, 0, sizeof(graph));
for(int i=0;i<4;i++){
* * for(int j=0;j<4;j++)
* * * * cout<<graph[i*n+j]<<" ";
* * cout<<endl;}

--------------------------------
But I found that the output is really strange -- graph[0][1] is always
a large number.
I expected the output will all be 0.



这是因为你没有使用std :: vector。总是使用高级别的
结构,除非你有充分的理由不这样做:低级编程

要求你处理许多与

你的问题,可能很难做对。你的一个问题

这里是sizeof没有回复你的想法,但有潜伏的其他问题




/ Peter

This is because you do not use std::vector. Always use high-level
constructs unless you have a good reason not to: low level programming
requires you to take care of lots of details that are irrelevant to
your problem and might be difficult to get right. One of your problems
here is that sizeof did not return what you thought, but there are
other problems lurking!

/Peter


这篇关于memset无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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