arrays of objects - Programmer

This is a discussion on arrays of objects - Programmer ; Hi all, I have a class CMyClass with some member vars like a couple of arrays of char *'s and some integers. In another class header file (call it CClass for simplicity), I declare three arrays of CMyClass objects: CMyClass ...

+ Reply to Thread
Results 1 to 11 of 11

Thread: arrays of objects

  1. arrays of objects

    Hi all,

    I have a class CMyClass with some member vars like a couple of arrays
    of char *'s and some integers. In another class header file (call it
    CClass for simplicity), I declare three arrays of CMyClass objects:
    CMyClass array1[256], CMyClass array2[16], and CMyClass array3[16].
    So far so good. In the CClass constructor, I start filling in the
    parameters of the objects. What I found was that array2[10] and
    array3[0] start overlapping in memory so that I can't keep unique
    information for each object. I am using VC++6 with SP5 on Win2K.
    More than likely this is just something stupid that I have overlooked
    in my non-caffienated state. Any ideas?

    Thanks,

    Joel

  2. Re: arrays of objects

    "Joel" wrote in message
    news:d63d777e.0405030525.48868d22@posting.google.c om...
    > Hi all,
    >
    > I have a class CMyClass with some member vars like a couple of arrays
    > of char *'s and some integers. In another class header file (call it
    > CClass for simplicity), I declare three arrays of CMyClass objects:
    > CMyClass array1[256], CMyClass array2[16], and CMyClass array3[16].
    > So far so good. In the CClass constructor, I start filling in the
    > parameters of the objects. What I found was that array2[10] and
    > array3[0] start overlapping in memory so that I can't keep unique
    > information for each object. I am using VC++6 with SP5 on Win2K.
    > More than likely this is just something stupid that I have overlooked
    > in my non-caffienated state. Any ideas?


    Yes. You have programmed in such a way as to have an error.
    Some code would be nice.
    --
    Gary



  3. Re: arrays of objects

    Joel wrote:
    >
    > Hi all,
    >
    > I have a class CMyClass with some member vars like a couple of arrays
    > of char *'s and some integers. In another class header file (call it
    > CClass for simplicity), I declare three arrays of CMyClass objects:
    > CMyClass array1[256], CMyClass array2[16], and CMyClass array3[16].
    > So far so good. In the CClass constructor, I start filling in the
    > parameters of the objects. What I found was that array2[10] and
    > array3[0] start overlapping in memory so that I can't keep unique
    > information for each object. I am using VC++6 with SP5 on Win2K.
    > More than likely this is just something stupid that I have overlooked
    > in my non-caffienated state. Any ideas?
    >


    You mean while you can see your code and cannot find an error you expect
    us who cannot see your code to spot your problem?

    The error is in line 42, as usual.

    --
    Karl Heinz Buchegger
    kbuchegg@gascad.at

  4. Re: arrays of objects


    "Joel" wrote in message
    news:d63d777e.0405030525.48868d22@posting.google.c om...
    > Hi all,
    >
    > I have a class CMyClass with some member vars like a couple of arrays
    > of char *'s and some integers. In another class header file (call it
    > CClass for simplicity), I declare three arrays of CMyClass objects:
    > CMyClass array1[256], CMyClass array2[16], and CMyClass array3[16].
    > So far so good. In the CClass constructor, I start filling in the
    > parameters of the objects. What I found was that array2[10] and
    > array3[0] start overlapping in memory so that I can't keep unique
    > information for each object. I am using VC++6 with SP5 on Win2K.
    > More than likely this is just something stupid that I have overlooked
    > in my non-caffienated state. Any ideas?
    >
    > Thanks,
    >
    > Joel


    Read back what you've written. Imagine you were an expert who knows
    everything there is to know about C++ but nothing at all about the code the
    'Joel' had written. Would you be able to solve the problem?

    Really what do you expect? You've made some mistake somewhere, but in the
    absence of seeing the code you've written no-one has the slightest clue what
    it might be.

    John



  5. Re: arrays of objects

    On 3 May 2004 06:25:58 -0700, joel_haugen@hotmail.com (Joel) wrote:

    >I have a class CMyClass with some member vars like a couple of arrays
    >of char *'s and some integers. In another class header file (call it
    >CClass for simplicity), I declare three arrays of CMyClass objects:
    >CMyClass array1[256], CMyClass array2[16], and CMyClass array3[16].
    >So far so good. In the CClass constructor, I start filling in the
    >parameters of the objects. What I found was that array2[10] and
    >array3[0] start overlapping in memory so that I can't keep unique
    >information for each object. I am using VC++6 with SP5 on Win2K.
    >More than likely this is just something stupid that I have overlooked
    >in my non-caffienated state. Any ideas?


    This sort of thing can happen when something somewhere in the VS/VC system gets
    itself confused, and somehow manages to use two definitions for the class.

    I have had this happen, and after a rebuild all, it's gone away.

    Of course, there's also the possibility that *you* have accidentally provided
    two class definitions.

    If that doesn't help, I fear you will have to get the code declassified so we
    can have a look

    Quandon


  6. Re: arrays of objects


    "Karl Heinz Buchegger" wrote in message
    news:40964CB2.E5064814@gascad.at...
    > Joel wrote:
    > >
    > > Hi all,
    > >
    > > I have a class CMyClass with some member vars like a couple of arrays
    > > of char *'s and some integers. In another class header file (call it
    > > CClass for simplicity), I declare three arrays of CMyClass objects:
    > > CMyClass array1[256], CMyClass array2[16], and CMyClass array3[16].
    > > So far so good. In the CClass constructor, I start filling in the
    > > parameters of the objects. What I found was that array2[10] and
    > > array3[0] start overlapping in memory so that I can't keep unique
    > > information for each object. I am using VC++6 with SP5 on Win2K.
    > > More than likely this is just something stupid that I have overlooked
    > > in my non-caffienated state. Any ideas?
    > >

    >
    > You mean while you can see your code and cannot find an error you expect
    > us who cannot see your code to spot your problem?
    >
    > The error is in line 42, as usual.
    >


    Isnīt 42 the answer to the "ultimate" question, if I remember correctly? ;-)
    I think that switching on the "/crystall_ball" compiler flag in VC++42.0
    should solve the problem *G*

    Greetings
    Chris



  7. Re: arrays of objects

    Okay, try this:

    class CRegister {
    protected:
    int bits[8];
    CString name;
    int reg;
    CString bitnames[8];

    public:
    void setRegNumber(int);
    void setBitName(int, const CString &);
    void setBitName(int, const unsigned char *);

    CRegister(void);
    CRegister(int);
    CRegister(int, int);
    CRegister(const CRegister &);
    ~CRegister(void);

    void setBit(int, int);
    int getBit(int) const;
    int getRegNumber(void) const;

    CString getBitName(int) const;

    int setBitByName(const CString &, int);
    int setBitByName(const unsigned char *, int);
    int getBitByName(const CString &) const;
    int getBitByName(const unsigned char *) const;
    void writeReg(void) const;
    int readReg(void);

    void setFromVal(int);
    int getVal(void) const;

    int setField(int, int, int);
    int setField(const CString &, int, int * maxbit = NULL);
    int setField(const unsigned char *, int, int * maxbit = NULL);
    int getField(int, int) const; // Gets the value of a field
    int getField(const CString &, int * maxbit = NULL) const;
    int getField(const unsigned char *, int * maxbit = NULL) const;

    CString getName(void) const;
    void setName(const CString &);
    void setName(const unsigned char *);

    int isRegister(const CString &) const;
    int isRegister(const unsigned char *) const;

    CRegister& operator=(int);

    int operator |=(int);
    int operator |=(const CString &);
    int operator |=(const unsigned char *);

    int operator &=(int);
    int operator &=(const CString &);
    int operator &=(const unsigned char *);

    int operator ^=(int);
    int operator ^=(const CString &);
    int operator ^=(const unsigned char *);

    int operator==(int) const;
    int operator!=(int) const;
    };

    For my other class, I have:
    #include "register.h"

    class CChannel
    {
    protected:
    CRegister reg[256];
    CRegister normregs[16];
    CRegister altregs[16];
    ....some other declarations, etc....
    public:
    ....some other declarations, etc....
    };

    Later in the .cpp file I have:

    reg[18].setName("Test");
    reg[18].setRegNumber(18);
    reg[18].setBitName(7, "Test07");
    reg[18].setBitName(6, "Test06");
    reg[18].setBitName(5, "Test05");
    reg[18].setBitName(4, "Test04");
    reg[18].setBitName(3, "Test03");
    reg[18].setBitName(2, "Test02");
    reg[18].setBitName(1, "Test01");
    reg[18].setBitName(0, "Test00");
    normregs[0].setName("Test");
    normregs[0].setRegNumber(18);
    normregs[0].setBitName(7, "Test07");
    normregs[0].setBitName(6, "Test06");
    normregs[0].setBitName(5, "Test05");
    normregs[0].setBitName(4, "Test04");
    normregs[0].setBitName(3, "Test03");
    normregs[0].setBitName(2, "Test02");
    normregs[0].setBitName(1, "Test01");
    normregs[0].setBitName(0, "Test00");
    altregs[0].setName("Alt Test");
    altregs[0].setRegNumber(18);
    altregs[0].setBitName(7, "AltTest07");
    altregs[0].setBitName(6, "AltTest06");
    altregs[0].setBitName(5, "AltTest05");
    altregs[0].setBitName(4, "AltTest04");
    altregs[0].setBitName(3, "AltTest03");
    altregs[0].setBitName(2, "AltTest02");
    altregs[0].setBitName(1, "AltTest01");
    altregs[0].setBitName(0, "AltTest00");

    .....

    where all 16 elements of altregs[] and normregs[] are initialized.
    Later on I do stuff with each array and the reg[] array but that is
    much later than where I see an issue. What happens is that when I
    look at the objects during the initialization process (by stepping
    through the code), I notice that altregs[10].bits[0] has the same
    address as normregs[0].bits[0]. The exact overlap point also depends
    on where I declare the arrays in my CChannel class header file. Here
    is an example: if I change around the declarations in my class to
    look like:

    class CChannel
    {
    protected:
    CRegister reg[256];
    ....some other declarations, etc....
    CRegister normregs[16];
    ....some other declarations, etc....
    CRegister altregs[16];
    public:
    ....some other declarations, etc....
    };

    then I get the following from the watch window in VC++6 (excuse the
    formatting please):

    altregs[0] {...}
    + bits 0x007308b8
    + name {""}
    reg 0x00000000
    + bitnames 0x007308e0 {""}

    altregs[1] {...}
    + bits 0x00730900
    + name {""}
    reg 0x00000000
    + bitnames 0x00730928 {""}

    normregs[15] {...}
    + bits 0x007308f8
    + name {""}
    reg 0x00000000
    + bitnames 0x00730920 {""}

    Notice that altregs[1].bitnames (an 8-element array of CString
    objects) is very close in memory to normregs[15].bitnames so that they
    will probably overlap somehow.

    As this is job related I can't give up too much. Does this help clear
    up some of the confusion? If not, let me know here and I will put
    more out when I can.

    Joel

  8. Re: arrays of objects

    [snip huge amount of irrelevant code]

    >
    > altregs[0] {...}
    > + bits 0x007308b8
    > + name {""}
    > reg 0x00000000
    > + bitnames 0x007308e0 {""}
    >
    > altregs[1] {...}
    > + bits 0x00730900
    > + name {""}
    > reg 0x00000000
    > + bitnames 0x00730928 {""}
    >
    > normregs[15] {...}
    > + bits 0x007308f8
    > + name {""}
    > reg 0x00000000
    > + bitnames 0x00730920 {""}
    >
    > Notice that altregs[1].bitnames (an 8-element array of CString
    > objects) is very close in memory to normregs[15].bitnames so that they
    > will probably overlap somehow.
    >
    > As this is job related I can't give up too much. Does this help clear
    > up some of the confusion? If not, let me know here and I will put
    > more out when I can.
    >


    I think the most likely explanation is that you have made a mistake in
    building your code. For instance you are compiling two source files with
    different compiler options but including the same header in both.

    Except in the case of a union a compiler/linker will not make two variables
    overlap in memory unless its seriously bugged so I think you should look to
    an explanation outside of the code you have written.

    john



  9. Re: arrays of objects

    >
    > I think the most likely explanation is that you have made a mistake in
    > building your code. For instance you are compiling two source files with
    > different compiler options but including the same header in both.
    >
    > Except in the case of a union a compiler/linker will not make two

    variables
    > overlap in memory unless its seriously bugged so I think you should look

    to
    > an explanation outside of the code you have written.
    >


    Another possibility is that your debugger has simply got it wrong, but the
    code is OK. I've seen that happen occasionally. Try deleting all the debug
    information.

    john



  10. Re: arrays of objects

    On 3 May 2004 11:55:23 -0700, joel_haugen@hotmail.com (Joel) wrote:

    >Okay, try this:


    Assuming you have checked to make sure you only define the class once:

    Did you try doing a rebuild all, as I suggested ?

    The next thing to do is to use "sizeof" to determine how big the compiler thinks
    your class is (make sure that this is in the same file where you are stepping
    the debugger and seeing incorrect results).

    Then, determine the alignment setting, and see if this size is correct.

    If it isn't, change one of the array sizes, and see if your sizeof value
    changes.

    Then report back !

  11. Re: arrays of objects


    "Quandon" wrote in message
    news:409743b7.222240187@news.onetel.net.uk...
    > On 3 May 2004 11:55:23 -0700, joel_haugen@hotmail.com (Joel) wrote:
    >
    > >Okay, try this:

    >
    > Assuming you have checked to make sure you only define the class once:
    >
    > Did you try doing a rebuild all, as I suggested ?
    >
    > The next thing to do is to use "sizeof" to determine how big the compiler

    thinks
    > your class is (make sure that this is in the same file where you are

    stepping
    > the debugger and seeing incorrect results).
    >
    > Then, determine the alignment setting, and see if this size is correct.
    >
    > If it isn't, change one of the array sizes, and see if your sizeof value
    > changes.
    >
    > Then report back !


    Yes but please not to comp.lang.c++, this discussion is well off topic here.

    john



+ Reply to Thread