简体   繁体   中英

How to initialize a 2D array with all values same?

I want to initialize a 2D array with -1 as all the value. I used memset() for this.

#include <bits/stdc++.h>
using namespace std;

int dp[100][100];
memset(dp, -1, sizeof(dp));

int dynamicProgramming(some parameters)
{
    //I want to use dp[][] array here
}

int main() {
    cout<<dp[99][99];
    return 0;
}

But I am getting an error as

prog.cpp:5:7: error: expected constructor, destructor, or type conversion before ‘(’ token
 memset(dp, -1, sizeof(dp));

Can you tell me the correct way to do it?

There is no way (at least I do not know any) to initialize all the elements of an array with a specific value other than 0 .

You can somehow work around in that you call memset in the course of initializing another variable at file scope:

int dp[100][100];
auto x = memset(dp, -1, sizeof(dp));

int main() {
    cout<<dp[99][99];
    return 0;
}

But note: the order of global variable initialization is not guaranteed; the only thing you can rely on is that the memset will have been executed before function main is entered. But then you could do it as first line in main as well.

Note further that you need good luck when you want to initialize an array elements of type int , as a single int comprises more than one byte; memset fills the elements at byte level, not at element type level. For example, memset(dp, -2, sizeof(dp)) will not lead to an array filled with -2 then; and even -1 requires the architecture to be two's complement to work.

As others have said, you can't call memset in the global scope. The most correct and clear vay is probably going to be something like

#include <iostream>
using namespace std;

const int D1 = 100;
const int D2 = 100;
int dp[D1][D2];

void initializeDp()
{
    for (int i = 0; i < D1; i++) {
        for (int j = 0; j < D2; j++) {
            dp[i][j] = -1;
        }
    }
}

int main() {
    initializeDp();
    cout << dp[D1-1][D2-1];
    return 0;
}

If you count global variables as a good practice, obviously. I personally would use a 2d std::array , so it would contain its size itself

for (auto& v : dp) {
    std::fill(std::begin(v), std::end(v), -1);
}

Inside function, not global scope.

You can't use memset in the global scope. It must be written in main scope.

#include <bits/stdc++.h>
using namespace std;

int dp[100][100];

int main() {
    memset(dp, -1, sizeof(dp));
    cout<<dp[99][99];
    return 0;
}

In practice, in this case, it is possible to write an helping templated function, itself based on simple for-range loops.

One advantage is that is will work for other types of 2D arrays.

#include    <iostream>

template <typename T2d, typename T>
void init2d (T2d &arr, T val) {
    for (auto& row: arr) {
        for (auto& i: row) {
            i = val;
        }
    }       
}

int main() {
    const int  n = 5, m = 6;
    int a[n][m];

    init2d (a, -1);
    std::cout << a[n-1][m-1] << "\n";
}

If you want to initialize before main , the simplest way is to rely on STL, eg on std::vector or std::array , like this, for a 5x6 matrix:

std::vector<std::vector<int>> b(5, std::vector<int> (6, -1));

It seems like you are looking for a way to init a global array before main like described in Call a function before main ? Usually I would recommend to avoid compiler specific extension but your code already is compiler specific and it seems ok for you. This is a gcc extension. It can cause strange errors. Other global objects could be uninitialized. Parts of this code probably causes undefined behavior. Just do it if you want to shoot you in your foot.

#include <bits/stdc++.h>
using namespace std;

void beforeMain (void) __attribute__((constructor));

int dp[100][100];
void beforeMain (void) {
    memset(dp, -1, sizeof(dp));
}

int main() {
    std::cout << dp[0][0] << std::endl;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM