Array as a C++ class member variable

11,754

Solution 1

Actually, you already have a pointer. But it's uninitialized.

Setting j[0] will work in some cases, sure, but you're writing to unallocated memory, or worse, memory used by another object. Basically you're creating a massive hole in your application.

Consider using a std::vector, which simply does all the allocating/deallocating for you. If that's not an option, at least initialize the array and don't go further than you allocated.

j[] is simply *j which is only a pointer to a random memory address. Very, very, very bad.

Solution 2

int j[];

Is not what you think it is. This actually defines a pointer: int* j;, which is left uninitialized, and from which you're reading in the uninitialized state, which is undefined behaviour, aka wrong.

Just use a std::vector.

Share:
11,754
FourOfAKind
Author by

FourOfAKind

Updated on July 07, 2022

Comments

  • FourOfAKind
    FourOfAKind almost 2 years

    Can I use array as a member variable of a class in c++ in the following way? or Should I declare it as a pointer? The following code works fine. Is it correct way of doing it? (I made it public just for simplicity). I'm also not sure whether I'm using the map correctly or not.

    #include <iostream>
    #include <map>
    #include <string.h>
    
    using namespace std;
    
    class SimpleMap{
    public:
        map<string,int> m;
        int i;
        int j[];
        SimpleMap(int ii);
    };
    
    SimpleMap::SimpleMap(int ii){
        i = ii;
    }
    
    
    
    int main(){
        SimpleMap mm(5);
        mm.m["one"] = 1;
    
        cout<<"hi hru";
    
        cout<<mm.m["one"];
    
        mm.j[0] = 11;
        cout << mm.j[0];
    }
    

    EDIT: I've add map member variable.

  • Xeo
    Xeo over 12 years
    Too bad you can't do that inside of a class definition.
  • FourOfAKind
    FourOfAKind over 12 years
    I'm actually confused how to use these compound data structures as data members of a class. I posted another snippet which uses map as data member. Could plz see if I am doing it wrong and that piece is also working fine.
  • jrok
    jrok over 12 years
    Why does such a declaration as a class member compile fine, but it's an error if you put it in function body (of both free and member function)?
  • FourOfAKind
    FourOfAKind over 12 years
    @Jrok: Could you please elaborate?
  • FourOfAKind
    FourOfAKind over 12 years
    Could you please tell me whether I'm using the map properly?
  • Xeo
    Xeo over 12 years
    @Tom: Not if you have a int j[] member, and especially not with that syntax.
  • Tom van der Woerdt
    Tom van der Woerdt over 12 years
    You are. It's very hard to create memory leaks with a std object. :-)
  • Xeo
    Xeo over 12 years
    @Lamia: The map is fine as you use it, though you won't get away with only using operator[] later on. :) Get friendly with map.find.
  • Xeo
    Xeo over 12 years
    @Tom: ; new std::vector<double>(500);. That was easy! >_> ... <_< ... >_>
  • Tom van der Woerdt
    Tom van der Woerdt over 12 years
    @Xeo Well, who would use a vector like that anyway? Besides, a good compiler will give a warning about that.
  • Xeo
    Xeo over 12 years
    @Tom: It was just meant as a joke answer to your "very hard". :)
  • Tom van der Woerdt
    Tom van der Woerdt over 12 years
    @Xeo: Oh :-) Humor is very hard in these comment boxes (see what I did there?).
  • interestedparty333
    interestedparty333 about 6 years
    @Xeo What if the author had written int j[3];, would j still be a pointer?
  • Flamefire
    Flamefire over 4 years
    This is not correct. This is (if supported) a variable array member, not a pointer.