C ++中数组的默认值是多少?

时间:2016-10-07 04:30:20

标签: c++ arrays

如果我要创建一个带有int* array = new int[10];的数组并使用值填充数组的一部分,我该如何检查填充了多少数组?我想循环并检查每个值是否是默认值但我不确定每个数组元素的默认值是什么。是null 0还是垃圾值?

5 个答案:

答案 0 :(得分:3)

没有默认值,所以它是垃圾。

答案 1 :(得分:3)

这是在制作数组时如何在C ++中设置默认值。

#include <random>
#include <chrono>
#include <iostream>
#include <xmmintrin.h>
#include <vector>

using namespace std;

const int N = 100000;
__declspec(align(16)) vector<float> X1;
__declspec(align(16)) vector<float> X2;
__declspec(align(16)) vector<float> Y(N);

__declspec(align(16)) vector<__m128> X1v;
__declspec(align(16)) vector<__m128> X2v;
__declspec(align(16)) vector<__m128> Yv(N / 4);

int main() {

    default_random_engine randomGenerator(time(0));
    uniform_real_distribution<float> diceroll(0.0f, 100.0f);

    for (int i = 0; i < N; i++) X1.push_back(diceroll(randomGenerator));
    for (int i = 0; i < N; i++) X2.push_back(diceroll(randomGenerator));
    for (int i = 0; i < N; i += 4)
    {
        __m128 _x1v = _mm_loadu_ps(&X1[i]);
        __m128 _x2v = _mm_loadu_ps(&X2[i]);
        X1v.push_back(_x1v);
        X2v.push_back(_x2v);
    }

    chrono::high_resolution_clock::time_point c1start, c1end, c2start, c2end;

    c1start = chrono::high_resolution_clock::now();
    for (int i = 0; i<N; i += 4)
    {
        __m128 _x1 = _mm_load_ps(&X1[i]);
        __m128 _x2 = _mm_load_ps(&X2[i]);
        __m128 _sum = _mm_add_ps(_x1, _x2);
        __m128 _sqrt = _mm_sqrt_ps(_sum);
        _mm_store_ps(&Y[i], _sqrt);
    }
    c1end = chrono::high_resolution_clock::now();

    c2start = chrono::high_resolution_clock::now();
    for (int j = 0; j<(N / 4); j++)
    {
        __m128 _x1 = X1v[j];
        __m128 _x2 = X2v[j];
        __m128 _sum = _mm_add_ps(_x1, _x2);
        __m128 _sqrt = _mm_sqrt_ps(_sum);
        Yv[j] = _sqrt;
    }
    c2end = chrono::high_resolution_clock::now();


    auto c1 = chrono::duration_cast<chrono::microseconds>(c1end - c1start).count();
    auto c2 = chrono::duration_cast<chrono::microseconds>(c2end - c2start).count();

    cout << "With arrays of floats and loads/stores: " << (float)c1 << endl;
    cout << "With arrays of __m128 and no loads or stores: " << (float)c2 << endl;
    cout << "Ratio arrays-of-m128: / arrays-of-floats " << (float)c2 / (float)c1 << endl;

    cout << endl;
    system("pause");
    return 0;
}

现在每个元素都设置为int array[100] = {0}; 。没有这样做,每个元素都是垃圾,如果使用的话将是未定义的行为。

并非所有语言都是这样的。声明数据结构时,Java有默认值,但C ++没有。

答案 2 :(得分:1)

你不能做你希望的,而不是类型是int

数组的未初始化元素将具有不可预测的值。此外,访问这些元素是造成未定义行为的原因。

您可以使用以下命令将数组元素初始化为分配时的标记值:

int* ptr = new int[10]{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};

如果-1没有,则使用任何哨兵值。

答案 3 :(得分:0)

您可以通过更多C ++方式解决您的问题。您可以创建 struct ,其中包含您的值和bool标志。 Bool标志必须在默认构造函数中设置为 false ,并在operator =中设置为 true 。已准备好实施此类 - boost.optional。 std :: optional将在C ++ 17中。

#include <boost/optional.hpp>
#include <iostream>

int main()
{
    const size_t nArr = 100;
    auto pArr = new boost::optional<int>[nArr];
    const size_t nInit = 30;
    for (size_t i = 0; i < nInit; ++i)
    {
       pArr[i] = i;  //initialize nInit first values of pArr
    }
    size_t n = 0;
    for (; n < nArr; ++n)
    {
         if (!pArr[n].is_initialized()) break;
         // or more compact form:
         //if(!pArr[n]) break;
         assert(*pArr[n] == n);
    }
    std::cout << "nInit = " << nInit << ", n = " << n << std::endl;
    assert(nInit == n);
    delete[] pArr;
}

答案 4 :(得分:0)

数组的默认值为 indeterminate 表示垃圾

  

如何查看填充的阵列数量?

您无法检查,C/C++没有数组边界检查。您必须自己完成。您需要跟踪用户插入的数据。当计数器达到数组大小时,数组已满