Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

TByteDynArray

110 views
Skip to first unread message

Gerhard Wolf

unread,
Sep 2, 2015, 3:47:03 AM9/2/15
to
Hi,

if have a unexpected result with the C++Builders TByteDynArray class.
Pruefung[STOP] = AB;
seems to pass ABs value by reference because
[1]-value of Pruefung[STOP] switches to 1 after
AB[1] = 1;
The std::string example works as expected.

Why that? How can i avoid this?
Is this a C++ feature defined in the TByteDynArray class?

----the console output----------------------------------------------
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00

0000000000
----the code--------------------------------------------------------
#include <vcl.h>
#include <windows.h>

#pragma hdrstop
#pragma argsused
#include <map>
#include <sstream>
#include <tchar.h>

#include <iostream>
#include <iomanip>
const int STOP = 9998;

std::string printTByteDynArry(TByteDynArray &td) {
std::stringstream ss;
for (int i(0); i < td.get_length(); i++) {
ss << std::setfill('0') << std::setw(2) << i << " ";
}
ss << std::endl;
for (int i(0); i < td.get_length(); i++) {
ss << std::setfill('0') << std::setw(2) << std::hex << (unsigned int)
td[i] << " ";
}
ss << std::endl;
return ss.str();
}

int _tmain(int argc, _TCHAR* argv[])
{
typedef std::map<int, TByteDynArray>TBDyArrMap;
TBDyArrMap Pruefung;
TByteDynArray AB;
AB.set_length(16);

for (int i(0); i < 15; i++)
AB[i] = 0;
Pruefung[STOP] = AB;
std::cout << printTByteDynArry(Pruefung[STOP]) << std::endl;
AB[1] = 1;
std::cout << printTByteDynArry(Pruefung[STOP]) << std::endl;

typedef std::map<int, std::string> StrMAP;
StrMAP test;
std::string tmp = "0000000000";
test[1] = tmp;
tmp = "1111111111";
std::cout << test[1] << std::endl;

return 0;
}

Barry Schwarz

unread,
Sep 2, 2015, 5:40:05 AM9/2/15
to
On Wed, 2 Sep 2015 09:46:44 +0200, Gerhard Wolf <lea...@gmx.de>
wrote:

>Hi,
>
>if have a unexpected result with the C++Builders TByteDynArray class.
>Pruefung[STOP] = AB;
>seems to pass ABs value by reference because
>[1]-value of Pruefung[STOP] switches to 1 after
>AB[1] = 1;

What is the actual type that TBytDynArray is an alias for? If
https://gist.github.com/daeltar/90951 is correct, it is a class with
one int member and one int* member. Since the = operator is not
overloaded, assigning one such object to another should result in the
corresponding members of both objects having the same value.

If two pointers point to the same memory, changes made by
dereferencing one pointer will show up when dereferencing the other.
--
Remove del for email

Kalle Olavi Niemitalo

unread,
Sep 2, 2015, 5:56:09 AM9/2/15
to
Gerhard Wolf <lea...@gmx.de> writes:

> if have a unexpected result with the C++Builders TByteDynArray class.
> Pruefung[STOP] = AB;
> seems to pass ABs value by reference because
> [1]-value of Pruefung[STOP] switches to 1 after
> AB[1] = 1;

I am not familiar with C++Builder but I looked at its documentation:

http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/Types_TByteDynArray.html
says it's defined in Delphi as "TByteDynArray = array of Byte;".
That's a Delphi dynamic array type, I think.

http://docwiki.embarcadero.com/Libraries/XE8/en/System.DynamicArray
says the DynamicArray template in C++Builder corresponds to the
Delphi dynamic array type, and it is reference counted.
I suppose C++Builder then defines TByteDynArray as a typedef for
DynamicArray<Byte>; I didn't find that explicitly in the
documentation, but perhaps the typedef is in some header file
distributed with C++Builder.

> How can i avoid this?

There is apparently a Copy method.
http://docwiki.embarcadero.com/Libraries/XE8/en/System.DynamicArray#Assigning.2C_comparing_and_copying_dynamic_arrays

Kalle Olavi Niemitalo

unread,
Sep 2, 2015, 6:09:03 AM9/2/15
to
Barry Schwarz <schw...@dqel.com> writes:

> What is the actual type that TBytDynArray is an alias for?

System::DynamicArray in C++Builder is the same size as a pointer.
http://docwiki.embarcadero.com/RADStudio/XE8/en/64-bit_Windows_Data_Types_Compared_to_32-bit_Windows_Data_Types

> If https://gist.github.com/daeltar/90951 is correct, it is a
> class with one int member and one int* member.

It is not correct: it does not define a copy assignment operator,
DynamicArray::resize reads past the end of the old array when
resizing to a larger size, and the comparison operators can
return without specifying a value.

Gerhard Wolf

unread,
Sep 2, 2015, 6:46:47 AM9/2/15
to
0 new messages