I need help in converting char * to TByteDynArray in Borland C++ Builder.

Below code is suppose to work but it give me EAccess Voilation error.

char *one = "test";
TByteDynArray two;
Move((void *) one[0], (void *) two[0],sizeof(one));

This kind of code work in delphi and it is also suppose to work in c+ builder but not working for me.

Thanks for any help anyone can provide.

Best Regards


11 Years
Discussion Span
Last Post by Salem

> two.set_length(sizeof(one));
sizeof tells you the size of the pointer.
strlen() tells you the length of the string that the pointer points to.

> Move((void *) one[0], (void *) two[0],sizeof(one));
I imagine your random casting has made the compiler shut up about important errors in this line.
1. From the look of things, TByteDynArray is a class with member functions, and it probably doesn't take too kindly to being written at using a brute force memory move. Does the class overload the [ ] operator by chance? If so, use a loop to move each character in turn.

2. If it's anything like the standard memmove() or memcpy() functions, then the parameter order is destination, source, length. In other words, you're copying TO one, not from it. This is an immediate fault as many implementations make string constants read-only.
In fact, it's even worse than that, it's taken the first letter of the string, cast that into a pointer, then tried to scribble all over it.

3. The afore-mentioned sizeof is not strlen problem.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.