如何使用memset初始化动态2D数组?

时间:2021-08-05 21:36:22

What's wrong with this little program? I cannot get the correct answer. I just use m[1][1] to test but it's always 0!

这个小程序出了什么问题?我无法得到正确的答案。我只是用m [1] [1]来测试,但它总是0!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    int **m;
    m = new int*[5];
    for( int i = 0 ; i < 5; i++ )
    {
        m[i] = new int[5];
        memset(m[i],1,sizeof(int)*5);   
    }

    printf("%f",*(*(m+1)+1));
    return 0;

}

1 个解决方案

#1


4  

This is "not C++". Yes, it's a C++ code, but it isn't using reasonable C++ idioms -- you're mixing C-style memset and pointer chasing with operator new (which is the only C++ feature you hapen to use). I will therefore assume that you have a reason to use code like that. If you don't have that, seriously consider using some STL class like a vector and avoid pointer manipulation.

这是“不是C ++”。是的,它是一个C ++代码,但它没有使用合理的C ++惯用语 - 你将C风格的memset和指针追逐与operator new混合(这是你需要使用的唯一C ++特性)。因此,我假设你有理由使用这样的代码。如果你没有那个,那么认真考虑使用像向量一样的STL类并避免指针操作。

If you used a reasonable compiler and added reasonable warnings, you would get the error immediately:

如果您使用了合理的编译器并添加了合理的警告,则会立即得到错误:

$ g++ -Wall pwn.cpp
pwn.cpp: In function ‘int main()’:
pwn.cpp:14:28: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat]

The correct argument to printf for printing ints is %d. How does it look after changing that?

用于打印整数的printf的正确参数是%d。改变它后如何看待?

$ ./a.out 
16843009

How come it doesn't print 1? The memset function sets memory, it does not initialize integers. And indeed, 0x01010101 == 16843009. This is not portable and very likely not what you want to do. Just don't do this, seriously.

怎么不打印1? memset函数设置内存,它不初始化整数。事实上,0x01010101 == 16843009.这不是便携式的,很可能不是你想要做的。只是不要认真对待。

#1


4  

This is "not C++". Yes, it's a C++ code, but it isn't using reasonable C++ idioms -- you're mixing C-style memset and pointer chasing with operator new (which is the only C++ feature you hapen to use). I will therefore assume that you have a reason to use code like that. If you don't have that, seriously consider using some STL class like a vector and avoid pointer manipulation.

这是“不是C ++”。是的,它是一个C ++代码,但它没有使用合理的C ++惯用语 - 你将C风格的memset和指针追逐与operator new混合(这是你需要使用的唯一C ++特性)。因此,我假设你有理由使用这样的代码。如果你没有那个,那么认真考虑使用像向量一样的STL类并避免指针操作。

If you used a reasonable compiler and added reasonable warnings, you would get the error immediately:

如果您使用了合理的编译器并添加了合理的警告,则会立即得到错误:

$ g++ -Wall pwn.cpp
pwn.cpp: In function ‘int main()’:
pwn.cpp:14:28: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat]

The correct argument to printf for printing ints is %d. How does it look after changing that?

用于打印整数的printf的正确参数是%d。改变它后如何看待?

$ ./a.out 
16843009

How come it doesn't print 1? The memset function sets memory, it does not initialize integers. And indeed, 0x01010101 == 16843009. This is not portable and very likely not what you want to do. Just don't do this, seriously.

怎么不打印1? memset函数设置内存,它不初始化整数。事实上,0x01010101 == 16843009.这不是便携式的,很可能不是你想要做的。只是不要认真对待。