c++ - 初始化在这里重要吗?

标签 c++

我的堆栈上有一个数组

char arr[10];

我有一个宏定义

#define CONVERT(arr, n)  sprintf(arr, "%lld", n)

现在我的问题是,初始化“arr”还是不初始化它有什么关系吗?

您更喜欢哪一个:

1. char arr[10];
2. char arr[10] = {0};

我知道初始化始终是首选,但请指出如果我不这样做会出现什么问题。

编辑:

在 main() 中,我将像这样使用它:

char arr[10] or arr[10] = {0};
CONVERT(arr, 1000);
cout<<arr;

我知道“std::string”将是一个更好的选择,但现在我只想坚持这个。伙计们,我知道我不应该读取未初始化的变量,但在这里我不会在“sprintf”之前读取它。

我只是想要其中一个相对于另一个的优势,并且应该优先选择哪个(有理由)。我并不是在寻找替代方法来做到这一点。

最佳答案

让我们看看数组会发生什么。根据cppreference:

Default initialization is performed in three situations:

1) when a variable with automatic, static, or thread-local storage duration is declared with no initializer;

2) when an object with dynamic storage duration is created by a new-expression with no initializer or when an object is created by a new-expression with the initializer consisting of an empty pair of parentheses (until C++03);

3) when a base class or a non-static data member is not mentioned in a constructor initializer list and that constructor is called.

我们的情况是1)。现在默认初始化程序的作用是什么?

The effects of default initialization are:

  • if T is a non-POD (until C++11) class type, the constructors are considered and subjected to overload resolution against the empty argument list. The constructor selected (which is one of the default constructors) is called to provide the initial value for the new object;

  • if T is an array type, every element of the array is default-initialized;

  • otherwise, nothing is done: the objects with automatic storage duration (and their subobjects) are initialized to indeterminate values.

我们显然有一个数组,但它的元素是 POD 类型。类数组的行为就像多个声明的类、POD 类型数组一样 - 就像多个 POD 类型变量的声明一样。它们的值(value)将是不确定的:

Default initialization of non-class variables with automatic and dynamic storage duration produces objects with indeterminate values (static and thread-local objects get zero initialized)

#include <string>

struct T1 { int mem; };

struct T2
{
    int mem;
    T2() { } // "mem" is not in the initializer list
};

int n; // static non-class, a two-phase initialization is done:
       // 1) zero initialization initializes n to zero
       // 2) default initialization does nothing, leaving n being zero


int main()
{
    int n;            // non-class, the value is indeterminate
    std::string s;    // class, calls default ctor, the value is "" (empty string)
    std::string a[2]; // array, default-initializes the elements, the value is {"", ""}
//  int& r;           // error: a reference
//  const int n;      // error: a const non-class
//  const T1 t1;      // error: const class with implicit default ctor
    T1 t1;            // class, calls implicit default ctor
    const T2 t2;      // const class, calls the user-provided default ctor
                      // t2.mem is default-initialized (to indeterminate value)
}

无论是否初始化,只有在写入之前有读操作才能看到效果。 sprint 仅将其第一个参数点写入内存。如果您之前从该数组读取值,则程序的行为将是未定义的,因为那里的值尚未确定。它可能是 0,它可能是随机值,它可能是标识未初始化内存的特殊标记,取决于编译器。一些编译器为此添加了运行时检查。

关于c++ - 初始化在这里重要吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41340176/

相关文章:

c++ - 如何获取桌面的窗口句柄?

c++ - 可以使用位置信息编写改进的 cmake 日志记录宏吗?

c++ - 自定义匹配函数的 boost async_read_until 问题不会在 GCC 中编译

c++ - OpenCv Visual C++ 2010 Express 问题

c++ - 清除并调整 vector 大小 - C++

c++ - 段错误(核心已转储)

c++ - 将窗口内容显示为位图

c++ - 如何方便地初始化函数指针数组?

c++ - 'registering' 对象放入同一个类中的静态数组时内存泄漏

c++ - 当我用 0 初始化 std::string 时发生了什么?