arrays of objects

J

Joel

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
 
G

Gary Labowitz

Joel said:
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.
 
K

Karl Heinz Buchegger

Joel said:
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.
 
J

John Harrison

Joel said:
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
 
Q

Quandon

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
 
C

Chris Theis

Karl Heinz Buchegger said:
Joel said:
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
 
J

Joel

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
 
J

John Harrison

[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
 
J

John Harrison

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
 
Q

Quandon

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 !
 
J

John Harrison

Quandon said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top