confused about arrays

P

pruebauno

I have a header for a c++ class I am using (from IBM WebSphere
DataStage):

....
// ---------------------------
// --- assignment operator ---
// ---------------------------

/**
If isFixedLength() is true, copies min(length(),
rhs.length()) characters from rhs into this string.
If less than length() characters are copied, the
balance of the length() is right-filled with pad
characters.
If isBoundedLength() is true, silently clamps the copied
length according to the bound.
Only the string value is copied; attributes such as
fixed-length, pad char, etc. are not copied.
@note For variable-length strings, it is not necessary to call
setLength() before calling this function.
*/
APT_BasicString& operator= (const APT_BasicString& rhs);

/**
* See APT_BasicString& operator= (const APT_BasicString& rhs).
*/
APT_BasicString& operator= (const C* rhs);

/**
* See APT_BasicString& operator= (const APT_BasicString& rhs).
*/
APT_BasicString& operator= (C c);

....etc.

And I have a program that does the following:

APT_String depen[15],frst[15];
int i;
....
//some code that assigns data to depen[0..14]

for(i=0;i<14;i++){
frst=depen;
}


Question: Am I copying the values or pointers of the strings in depen?

I am asking because after that code I change the values of depen[0..14]
and later in the code frst does not have the content I am expecting
(The original unchanged, that is). Of course it also could be that my
logic is messed up somewhere else but I just wanted to make sure I am
understanding this aspect of c++ correctly.
 
J

John Harrison

I have a header for a c++ class I am using (from IBM WebSphere
DataStage):

...
// ---------------------------
// --- assignment operator ---
// ---------------------------

/**
If isFixedLength() is true, copies min(length(),
rhs.length()) characters from rhs into this string.
If less than length() characters are copied, the
balance of the length() is right-filled with pad
characters.
If isBoundedLength() is true, silently clamps the copied
length according to the bound.
Only the string value is copied; attributes such as
fixed-length, pad char, etc. are not copied.
@note For variable-length strings, it is not necessary to call
setLength() before calling this function.
*/
APT_BasicString& operator= (const APT_BasicString& rhs);

/**
* See APT_BasicString& operator= (const APT_BasicString& rhs).
*/
APT_BasicString& operator= (const C* rhs);

/**
* See APT_BasicString& operator= (const APT_BasicString& rhs).
*/
APT_BasicString& operator= (C c);

...etc.

And I have a program that does the following:

APT_String depen[15],frst[15];
int i;
...
//some code that assigns data to depen[0..14]

for(i=0;i<14;i++){

Should be

for(i=0;i<15;i++){

Your arrays are size 15 not 14.
frst=depen;
}


Question: Am I copying the values or pointers of the strings in depen?


Values, I see no pointers anywhere. To be precise you are invoking the
arrsignment operator of APT_String. What that does depends of how it was
written, you will have to consult the documentation. But the obvious
sensible thing would be for APT_String to have been written so that
copying it was like copying a value not a pointer.
I am asking because after that code I change the values of depen[0..14]
and later in the code frst does not have the content I am expecting
(The original unchanged, that is). Of course it also could be that my
logic is messed up somewhere else but I just wanted to make sure I am
understanding this aspect of c++ correctly.

OK, well that is evidence that APT_String has not been sensibly written
(in my view). But really no-one here can answer this question (unless
they happen to know about APT_String), you need to consult your
documentation.

Another way would be to write a simple program

int main()
{
APT_String x;
// code to assign value to x
APT_String y;
y = x;
// code to change value of y
// code to see if value of x has also changed
}

in other words write a really simple program to test the hypothesis. I
still would be surprised if copying an APT_String is like copying a
pointer, I would expect that you've messed up somewhere else (no offense
meant) but I could easily be wrong.

John
 
P

pruebauno

Thanks,

the APT_String class does indeed copy by value confirming your (and my
initial) assumtion. The algorithm was correct also.

After some madness I figured out what the problem was: When I made the
changes to this routine some changes to the routine that calls this one
were made and the order of the input data passed to it was
inadvertently changed. This resulted in inverted rows in the output.
Fixed the order of the input data and voila! everything worked like a
charm. (duh)

Thanks for your tip, at least I discarded that hypothesis and started
looking elsewhere.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top