0

I have the following source code:

class Solution {
public:
    void setZeroes(vector<vector<int>>& matrix) {
        int m=matrix.size(), n=matrix[1].size(), index=0;
        int rowOfZeroEle[m+1], colOfZeroEle[n+1];
        
        for(int i=0; i<m; i++){ rowOfZeroEle[i]=-1;}
        for(int i=0; i<n; i++){ colOfZeroEle[i]=-1;}
        
        for(int i=0; i<m; i++){
            for(int j=0; j<n; j++){
                if(matrix[i][j]==0){
                    rowOfZeroEle[index] = i;
                    colOfZeroEle[index++] = j;
                }
            }
        }
        
        for(int i=0;i<m;i++){
            if(rowOfZeroEle[i]!=-1){
                for(int j=0;j<n;j++)
                    matrix[rowOfZeroEle[i]][j]=0;
            }
        }
        for(int j=0;j<n;j++){
            if(colOfZeroEle[j]!=-1){
                for(int i=0;i<m;i++)
                    matrix[i][colOfZeroEle[j]]=0;
            }
        }
    }
};

This is the error message I am getting:

29==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6030000001b0 at pc 0x0000003461f9 bp 0x7ffe9eede170 sp 0x7ffe9eede168
READ of size 8 at 0x6030000001b0 thread T0
    #2 0x7fb9ab2140b2  (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
0x6030000001b0 is located 8 bytes to the right of 24-byte region [0x603000000190,0x6030000001a8)
allocated by thread T0 here:
    #6 0x7fb9ab2140b2  (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
Shadow bytes around the buggy address:
  0x0c067fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c067fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c067fff8000: fa fa fd fd fd fd fa fa fd fd fd fa fa fa fd fd
  0x0c067fff8010: fd fa fa fa fd fd fd fd fa fa fd fd fd fa fa fa
  0x0c067fff8020: fd fd fd fa fa fa fd fd fd fd fa fa fd fd fd fa
=>0x0c067fff8030: fa fa 00 00 00 fa[fa]fa fa fa fa fa fa fa fa fa
  0x0c067fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c067fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c067fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c067fff8070: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c067fff8080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
  Shadow gap:              cc
==29==ABORTING

Could anyone help me this error message and guide me to resolve it.

Problem Link

Sercan
  • 4,739
  • 3
  • 17
  • 36
  • 1
    As usual with such sites, they tend to teach invalid C++ code. [C++ doesn't have variable-length arrays](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). Use vectors instead. Incidentally that might actually solve your problem (which seems to be an `alloca` issue which allocates space on the stack, probably for your variable-length arrays). – Some programmer dude Jan 18 '22 at 08:55
  • So-called "competition" or "online judge" sites are not teaching or learning resources. Using them as such can actually be harmful to your learning of programming and programming languages. If you want to learn C++ and how to program in C++, invest in [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) and take classes. – Some programmer dude Jan 18 '22 at 08:56
  • I agree with some programmer dude to learn c++ don't use these sites. they teach you a lot of bad habits (like using namespace std). In this case rowOfZeroEle and colOfZeroEle should also have been std::vectors. Only use leetcode to train your problem solving skills, if you can't get a book try this : https://www.learncpp.com/ – Pepijn Kramer Jan 18 '22 at 08:57
  • `int rowOfZeroEle[m+1]` -- Note that your code already uses `std::vector`, so it seems you are not aware what a vector is. If you were taught what vectors are, you would never had declared an array this way. You would have automatically done this: `std::vector rowOfZeroEle(m + 1);`. Also, no good C++ book shows declaring arrays with a runtime variable denoting the size. The reason why is because declaring arrays like that is not C++. – PaulMcKenzie Jan 18 '22 at 09:07
  • `index` can grow larger than `m` and `n`, and then you have undefined behaviour on your hands. – molbdnilo Jan 18 '22 at 09:09
  • The other benefit of using vector is that you can use the `at()` function to check if you are going out-of-bounds. If you used vector, and if you replace where you are using `[]` to access the elements with `at()`, I can almost guarantee you will get a much better error telling you what the issue is. – PaulMcKenzie Jan 18 '22 at 09:09
  • @molbdnilo how could I solve these problem – Manik Badshah Jan 18 '22 at 09:22
  • Think a bit more. If there are `k` zeros in a row (or column), do you really need to store that row (or column) number `k` times, or would once be enough? – molbdnilo Jan 18 '22 at 09:28
  • `matrix[1].size()` will go out of bounds if it's a 1xN matrix. – infinitezero Jan 18 '22 at 10:05

0 Answers0