0

I am trying to use memset to set a dynamic array of size rownum x rownmum. However, when I call the showarr function as shown below, instead of getting the output of all zeros (i.e. ASCII 48), I am getting random/garbage values.

void showarr(int **a, int rownum)
{
        for ( int i = 0; i < rownum; i++) {
                for ( int j = 0; j < rownum; j++) {
                        cout<<a[i][j]<<endl;
                }
        }
}

int main(int argc, char *argv[])
{
        int n;
        cin>>n;

        int rownum;
        cin>>rownum;
        int **a = new int* [rownum] ;
        for ( int i = 0; i <rownum; i++) {
             a[i] = new int[rownum];
        }
        for (int i = 0; i < rownum; i++) {
              memset(a[i],48,sizeof(a[i]));
        }
        showarr(a,rownum);

        return 0;
}
4
  • 2
    sizeof(a) == size(int **), meaning either 4 or 8 bytes, depending on your processor. Commented Jan 25, 2012 at 12:51
  • sorry, there was a typo. What I meant was sizeof(a[i]) Commented Jan 25, 2012 at 13:00
  • 4
    What are you trying to do? Generally speaking a smart pointer is better than a raw pointer, a vector is better than an array, a reference to a pointer is better than a pointer to a pointer, and std::fill() is better than memset(). Commented Jan 25, 2012 at 13:02
  • Using sizeof like you try to do only works on explicit arrays (i.e. variables declared like int a[10]). Commented Jan 25, 2012 at 13:02

2 Answers 2

5

sizeof(a[i]) == sizeof(int *); the sizeof operator cannot be used to get the length of a dynamically allocated array. Do

memset(a[i], '0', sizeof(a[i][0]) * rownum);

instead.

(And please refrain from using ASCII values as magic numbers in code.)

Sign up to request clarification or add additional context in comments.

Comments

0

See larsmans answer as to what the issue is. This is easily replaced with vectors (with no chance of memory leaking) (not compiled/tested):

#include <vector>
#include <iostream>

typedef std::vector<int> vector1D;
typedef std::vector<vector1D> vector2D;

void showarr(const vector2D& v)
{
        for (vector2D::iterator it1 = v.begin(); it1 != v.end(); ++it1) {
                for (vector1D::iterator it2 = it1.begin(); it2 != it1.end(); ++it2) {
                        cout<<*it2<<endl;
                }
        }
}

int main()
{
        int rownum;
        cin>>rownum;
        vector2D a();
        for (int i = 0; i <rownum; i++) {
             a.push_back(vector1D(rownum, '0'));
        }
        showarr(a);
}

2 Comments

The behavior of this piece of code is different, though: it will set each int to '0' rather than each byte within each int.
@larsmans: correct, although in this case this is more in line with what is expected. (and part of the not compiled/tested).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.