0

I have declared a 2D array of type bool and I want to initiate it with false. I have this requirement for a purpose of this code (given below) which is a question on "Wild Card String Matching"

I have tried 2 ways to initialize the boolean matrix with false. The first method is bool lookup[n + 1][m + 1]={false}; and the second method is by memset(lookup, false, sizeof(lookup));

When I am testing my code the first method is giving me a wrong answer. Why is this happening? I have attached my code herewith.

// C++ program to implement wildcard
// pattern matching algorithm
#include <bits/stdc++.h>
using namespace std;

// Function that matches input str with
// given wildcard pattern
bool strmatch(char str[], char pattern[], int n, int m)
{
    

    // lookup table for storing results of
    // subproblems
    bool lookup[n + 1][m + 1];

    // initailze lookup table to false
    memset(lookup, false, sizeof(lookup));

    // empty pattern can match with empty string
    lookup[0][0] = true;

    // Only '*' can match with empty string
    for (int j = 1; j <= m; j++)
        if (pattern[j - 1] == '*')
            lookup[0][j] = lookup[0][j - 1];

    // fill the table in bottom-up fashion
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            
            if (pattern[j - 1] == '*')
                lookup[i][j]
                    = lookup[i][j - 1] || lookup[i - 1][j];

            else if (pattern[j - 1] == '?'
                    || str[i - 1] == pattern[j - 1])
                lookup[i][j] = lookup[i - 1][j - 1];

            // If characters don't match
            else
                lookup[i][j] = false;
        }
    }

    return lookup[n][m];
}

int main()
{
    char pattern[] = "ajjajodojei*";
    char str[] = "ahdisahdisha";
    
    

    if (strmatch(str, pattern, strlen(str),
                strlen(pattern)))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;

    return 0;
}
Turing101
  • 347
  • 3
  • 15
  • No, they are not equivalent. The fist one does what you want, but not for the reasons you think. The second one is incorrect. And `bool lookup[n + 1][m + 1];` is invalid C++ as VLAs are not part of the standard. – bolov May 27 '21 at 16:50
  • The code that I have provided i.e. with `memset` works fine, but when I am trying to initialize using `bool loockup[n+1][m+1]` I am getting wrong ans. So I dont think the second one is incorrect as it gives me the correct ans. – Turing101 May 27 '21 at 16:54
  • 6
    Getting lucky with undefined behavior isn't the same thing as writing correct code. – Nathan Pierson May 27 '21 at 16:56
  • 2
    @NathanPierson can you clarify what is wrong with the call to `memset()`? Is it the pointer, or the value that's being used to fill the memory? It's not immediately obvious what OP is doing wrong. – Tim Randall May 27 '21 at 17:00
  • @bolov while VLAs are not part of the standard, they're a commonly available extension. I don't think it's fair to yell at someone for using them. – Mark Ransom May 27 '21 at 18:46
  • 1
    @MarkRansom I also think it would be totally unfair and out of place to yell at somebody for using VLAs. I guess it's a good thing I didn't, not in the slightest. Getting back to the matter at hand they are not commonly available: 1 out of the 3 major C++ compilers doesn't support them. So I do think it's necessary to raise attention to them when someone is using them, especially when they seem to be beginners and might not know this. – bolov May 27 '21 at 19:07

1 Answers1

3

I'm not sure where this broken syntax originated, but that is not how you zero-initialize an object in C++. This is how you do it:

bool lookup = {}; // note there's no false randomly thrown in there

Also note that the above is not an array, because your VLA is invalid in C++.

Your memset call is also broken, because the second argument is not a bool, it's an int that's interpreted as an unsigned char. Both problems are easily solved by just reading the documentation.

Some ways to properly define your array in C++:

// nested vectors. This has the advantage of packing your values as bits instead of bytes.
std::vector<std::vector<bool>> arr;

// nested arrays stored as unique pointers. This has the advantage of automatic cleanup.
std::unique_ptr<std::unique_ptr<bool[]>[]> arr; 

// nested arrays stored as plain pointers. Just bad.
bool **arr;

// flat array of some kind. Use math to map your 2D index to a flat index and back.
std::vector<bool> arr;
std::unique_ptr<bool[]> arr;
bool *arr;
Blindy
  • 65,249
  • 10
  • 91
  • 131
  • Unfortunately `std::vector` is broken. – bolov May 27 '21 at 17:41
  • @bolov it's not so broken as to be totally useless. I don't see a problem with the way it's presented here, although you certainly won't get away with using `memset` on it. – Mark Ransom May 27 '21 at 18:42
  • The common consensus, even among C++ experts is to avoid `std::vector`. But yeah, they are not completely useless, you can use them if you want. – bolov May 27 '21 at 19:12
  • That's a lot of words for having no proof to back them up. I would ask what you think is broken with it, despite being used everywhere, but I don't think I want to hear you try to logic something together. And you don't `memset` a `vector`, that's what `clear()` is for. – Blindy May 27 '21 at 20:23
  • Don't be afraid to ask :) https://isocpp.org/blog/2012/11/on-vectorbool#:~:text=First%2C%20what's%20wrong%20with%20vector,quite%20innocent%20looking%20generic%20code. , https://stackoverflow.com/questions/17794569/why-isnt-vectorbool-a-stl-container , https://stackoverflow.com/questions/6781985/is-the-use-of-stdvectorbool-objects-in-c-acceptable-or-should-i-use-an-al , http://www.gotw.ca/gotw/050.htm – bolov May 27 '21 at 23:11
  • There's nothing broken in the examples shown above, the container works as documented. And any attempts to misuse it (due to not reading the aforementioned documentation) end in compile-time errors, rather than broken code being executed. I don't know of any C++ "expert" who's not intelligent enough to use it as intended. Kindly keep your opinions and difficulties with C++ off of answers, and feel free to post your own questions if you need help with standard C++ features instead. – Blindy May 28 '21 at 16:08