将一个数组内的数组归零c ++ [duplicate]

时间:2022-09-06 11:35:29

This question already has an answer here:

这个问题在这里已有答案:

I have a struct defined in my program.

我的程序中定义了一个结构。

struct A{
    int arr[10];
}

Lets say I have a pointer to it. A * a = new A;

可以说我有一个指向它的指针。 A * a =新A;

I can zero it in two ways:

我可以用两种方式将其归零:

memset(&a->arr,0,sizeof(A));
memset(a->arr,0,sizeof(A));

both work and look the same!

既工作又看起来一样!

which one is more correct?

哪一个更正确?

5 个解决方案

#1


0  

This is the correct way for just the array

这是只有阵列的正确方法

memset(a->arr,0,sizeof(a->arr))

Picked out the arr member just in case there are other structure members that do not need to be touched. Makes no difference in your example the following will do likewise

挑选出arr成员以防万一其他结构成员不需要触摸。在您的示例中没有区别,以下也会这样做

memset(a->arr,0,sizeof(A));

#2


2  

which one is more correct?

哪一个更正确?

I'd argue neither. The easiest way would be to value initialize the allocated object:

我也不争辩。最简单的方法是初始化分配的对象的值:

A * a = new A();

Of course, this assumes that you actually have a good reason to new this object.

当然,这假设您实际上有充分的理由来创建此对象。

#3


2  

Since you are using C++ I would take advantage of C++11 features and use:

由于您使用的是C ++,我将利用C ++ 11的功能并使用:

#include <iostream>
#include <cmath>
using namespace std;

struct A{
    int arr[10]{};  // initializes array with all 0's
};

int main() {
    A * a = new A;
    for (auto e : a->arr) // ranged based for loop to show all of the 0's
        cout << e << "\n";
    return 0;
}

You can see it running with this Live Example

您可以看到它与此实例一起运行

#4


1  

While the type of each expression is different, the actual result, the pointer you pass to memset, will be equal in both cases.

虽然每个表达式的类型不同,但实际结果(传递给memset的指针)在两种情况下都是相同的。

Personally I would probably use std::fill instead of memset in a C++ program:

就个人而言,我可能会在C ++程序中使用std :: fill而不是memset:

std::fill(std::begin(a->arr), std::end(a->arr), 0);

Also note that if you have more members in the structure, sizeof(A) will be different from sizeof(a->arr) (or sizeof(A::arr)).

另请注意,如果结构中有更多成员,则sizeof(A)将与sizeof(a-> arr)(或sizeof(A :: arr))不同。

#5


1  

you can define a default construct function

您可以定义默认构造函数

struct A{
    int arr[10];
    A():arr(){}
};

#1


0  

This is the correct way for just the array

这是只有阵列的正确方法

memset(a->arr,0,sizeof(a->arr))

Picked out the arr member just in case there are other structure members that do not need to be touched. Makes no difference in your example the following will do likewise

挑选出arr成员以防万一其他结构成员不需要触摸。在您的示例中没有区别,以下也会这样做

memset(a->arr,0,sizeof(A));

#2


2  

which one is more correct?

哪一个更正确?

I'd argue neither. The easiest way would be to value initialize the allocated object:

我也不争辩。最简单的方法是初始化分配的对象的值:

A * a = new A();

Of course, this assumes that you actually have a good reason to new this object.

当然,这假设您实际上有充分的理由来创建此对象。

#3


2  

Since you are using C++ I would take advantage of C++11 features and use:

由于您使用的是C ++,我将利用C ++ 11的功能并使用:

#include <iostream>
#include <cmath>
using namespace std;

struct A{
    int arr[10]{};  // initializes array with all 0's
};

int main() {
    A * a = new A;
    for (auto e : a->arr) // ranged based for loop to show all of the 0's
        cout << e << "\n";
    return 0;
}

You can see it running with this Live Example

您可以看到它与此实例一起运行

#4


1  

While the type of each expression is different, the actual result, the pointer you pass to memset, will be equal in both cases.

虽然每个表达式的类型不同,但实际结果(传递给memset的指针)在两种情况下都是相同的。

Personally I would probably use std::fill instead of memset in a C++ program:

就个人而言,我可能会在C ++程序中使用std :: fill而不是memset:

std::fill(std::begin(a->arr), std::end(a->arr), 0);

Also note that if you have more members in the structure, sizeof(A) will be different from sizeof(a->arr) (or sizeof(A::arr)).

另请注意,如果结构中有更多成员,则sizeof(A)将与sizeof(a-> arr)(或sizeof(A :: arr))不同。

#5


1  

you can define a default construct function

您可以定义默认构造函数

struct A{
    int arr[10];
    A():arr(){}
};