Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > strcpy from int[]?

Reply
Thread Tools

strcpy from int[]?

 
 
Ural Mutlu
Guest
Posts: n/a
 
      07-26-2006
Hi,

I have an array of int's and a string. I am trying to copy the int array
into the string but I haven't been successful. The only solution I can think
of is strcpy or memcpy.

I wrote the following test program but in the example below str is empty.

I thought of using itoa() in the stdlib.h but itoa() must be obsolete, gcc
doesn't find the function.

Any suggestions?

OS: linux
compiler: gcc v:4.1.1


#include <iostream>
#include <string>

using namespace std;

int main() {

string str("");
int i[2]={0,0};

strncpy((char*)str.c_str(),(char*)i, sizeof(i));
//memcpy((void*)str.c_str(),(void*)i, sizeof(i));
cout<<str<<endl;
return 0;
}


Regards
 
Reply With Quote
 
 
 
 
Ural Mutlu
Guest
Posts: n/a
 
      07-26-2006
On Wed, 26 Jul 2006 04:39:38 +0100, Ural Mutlu wrote:

> Hi,
>
> I have an array of int's and a string. I am trying to copy the int array
> into the string but I haven't been successful. The only solution I can think
> of is strcpy or memcpy.
>
> I wrote the following test program but in the example below str is empty.
>
> I thought of using itoa() in the stdlib.h but itoa() must be obsolete, gcc
> doesn't find the function.
>
> Any suggestions?
>
> OS: linux
> compiler: gcc v:4.1.1
>
>
> #include <iostream>
> #include <string>
>
> using namespace std;
>
> int main() {
>
> string str("");
> int i[2]={0,0};
>
> strncpy((char*)str.c_str(),(char*)i, sizeof(i));
> //memcpy((void*)str.c_str(),(void*)i, sizeof(i));
> cout<<str<<endl;
> return 0;
> }
>
>
> Regards


I should probably add that I am not looking to convert the int's to char's.

I mean if the size of int is 4 bytes, I have to represent it as 4 char's.
Basically, some data is stored in a string rather than int.

That's why I thought memcpy is the solution, but somehow the above
example doesn't give me a result.

regards
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      07-26-2006
Ural Mutlu wrote:
> Hi,
>
> I have an array of int's and a string. I am trying to copy the int array
> into the string but I haven't been successful. The only solution I can think
> of is strcpy or memcpy.
>

Why?

> I wrote the following test program but in the example below str is empty.
>
> I thought of using itoa() in the stdlib.h but itoa() must be obsolete, gcc
> doesn't find the function.
>
> Any suggestions?
>

What are you actualy trying to achieve? Do you want the bitwise
representation of the ints in your string or their character equivalent?

> #include <iostream>
> #include <string>
>
> using namespace std;
>

Why?

> int main() {
>
> string str("");
> int i[2]={0,0};
>
> strncpy((char*)str.c_str(),(char*)i, sizeof(i));


You can't use the value returned by c_str() as an lvalue, as your
compiler will tell you if you remove the vile cast.

--
Ian Collins.
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      07-26-2006
Ural Mutlu wrote:

> On Wed, 26 Jul 2006 04:39:38 +0100, Ural Mutlu wrote:
>
>> Hi,
>>
>> I have an array of int's and a string. I am trying to copy the int array
>> into the string but I haven't been successful. The only solution I can
>> think of is strcpy or memcpy.
>>
>> I wrote the following test program but in the example below str is empty.
>>
>> I thought of using itoa() in the stdlib.h but itoa() must be obsolete,
>> gcc doesn't find the function.
>>
>> Any suggestions?
>>
>> OS: linux
>> compiler: gcc v:4.1.1
>>
>>
>> #include <iostream>
>> #include <string>
>>
>> using namespace std;
>>
>> int main() {
>>
>> string str("");
>> int i[2]={0,0};
>>
>> strncpy((char*)str.c_str(),(char*)i, sizeof(i));
>> //memcpy((void*)str.c_str(),(void*)i, sizeof(i));
>> cout<<str<<endl;
>> return 0;
>> }
>>
>>
>> Regards

>
> I should probably add that I am not looking to convert the int's to
> char's.


Yes.

> I mean if the size of int is 4 bytes, I have to represent it as 4 char's.
> Basically, some data is stored in a string rather than int.
>
> That's why I thought memcpy is the solution, but somehow the above
> example doesn't give me a result.


Try:

#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>

using namespace std;

int main() {
string str("");
int i[2]={48908902,4090799};
char const * c_begin
= static_cast< char* >( static_cast< void* >( &i[0] ) );
char const * c_end = c_begin + sizeof(i);
copy( c_begin, c_end, inserter( str, str.end() ) );
cout<<str<<endl;
return 0;
}

Beware that the code is bound to invoke undefined behavior as the standard
does not impose rigid restrictions on the internal representation of
integers. In particular, you may find that endian-ness enters the picture.


Best

Kai-Uwe Bux
 
Reply With Quote
 
Ural Mutlu
Guest
Posts: n/a
 
      07-26-2006
On Wed, 26 Jul 2006 16:41:22 +1200, Ian Collins wrote:

> Ural Mutlu wrote:
>> Hi,
>>
>> I have an array of int's and a string. I am trying to copy the int array
>> into the string but I haven't been successful. The only solution I can think
>> of is strcpy or memcpy.
>>

> Why?
>
>> I wrote the following test program but in the example below str is empty.
>>
>> I thought of using itoa() in the stdlib.h but itoa() must be obsolete, gcc
>> doesn't find the function.
>>
>> Any suggestions?
>>

> What are you actualy trying to achieve? Do you want the bitwise
> representation of the ints in your string or their character equivalent?


bitwise

>
>> #include <iostream>
>> #include <string>
>>
>> using namespace std;
>>

> Why?
>
>> int main() {
>>
>> string str("");
>> int i[2]={0,0};
>>
>> strncpy((char*)str.c_str(),(char*)i, sizeof(i));

>
> You can't use the value returned by c_str() as an lvalue, as your
> compiler will tell you if you remove the vile cast.



no the compiler doesn't complain and it work, indeed I saw this somewhere
else and that's why I use it. c_str() returns a const char*, therefore the
need to cast it to char*. I know it's ugly but works.


 
Reply With Quote
 
Ural Mutlu
Guest
Posts: n/a
 
      07-26-2006

>>> string str("");
>>> int i[2]={0,0};
>>>
>>> strncpy((char*)str.c_str(),(char*)i, sizeof(i));

>>
>> You can't use the value returned by c_str() as an lvalue, as your
>> compiler will tell you if you remove the vile cast.

>
>
> no the compiler doesn't complain and it work, indeed I saw this somewhere
> else and that's why I use it. c_str() returns a const char*, therefore the
> need to cast it to char*. I know it's ugly but works.



sorry I misunderstood your message, too early in the morning..

you are right...
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      07-26-2006
Ural Mutlu wrote:
> On Wed, 26 Jul 2006 16:41:22 +1200, Ian Collins wrote:
>>
>>What are you actualy trying to achieve? Do you want the bitwise
>>representation of the ints in your string or their character equivalent?

>
>
> bitwise
>

Why on Earth would you want to do that? Consider what happens with 0.
>
>>>#include <iostream>
>>>#include <string>
>>>
>>>using namespace std;
>>>

>>
>>Why?
>>
>>
>>>int main() {
>>>
>>> string str("");
>>> int i[2]={0,0};
>>>
>>> strncpy((char*)str.c_str(),(char*)i, sizeof(i));

>>
>>You can't use the value returned by c_str() as an lvalue, as your
>>compiler will tell you if you remove the vile cast.

>
> no the compiler doesn't complain and it work, indeed I saw this somewhere
> else and that's why I use it. c_str() returns a const char*, therefore the
> need to cast it to char*. I know it's ugly but works.
>

Nonsense. It may appear to work, or even work with you current
implementation, but it's wrong. Consider what happens if the
implementation of std::string doesn't allocate a buffer until it is
required.

Either way, you will end up with an invalid string object, all of its
internal state with be for zero length, but you will have bladdered its
buffer. Any data you have written in will be lost because there won't
be a way to get it back.

--
Ian Collins.
 
Reply With Quote
 
Ural Mutlu
Guest
Posts: n/a
 
      07-26-2006
>
> #include <iostream>
> #include <string>
> #include <algorithm>
> #include <iterator>
>
> using namespace std;
>
> int main() {
> string str("");
> int i[2]={48908902,4090799};
> char const * c_begin
> = static_cast< char* >( static_cast< void* >( &i[0] ) );
> char const * c_end = c_begin + sizeof(i);
> copy( c_begin, c_end, inserter( str, str.end() ) );
> cout<<str<<endl;
> return 0;
> }
>
> Beware that the code is bound to invoke undefined behavior as the standard
> does not impose rigid restrictions on the internal representation of
> integers. In particular, you may find that endian-ness enters the picture.
>
>
> Best
>
> Kai-Uwe Bux



thanks,

got it to work in a similar way to what you said.

I still don't understand why memcpy or strcpy don't work. The code
above copies one char at a time until the last element is copied. The
difference is that memcpy copies a chunk of memory given with the
pointer and size. Basically they both copy memory and they should do the
same job?

regards,
ural
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      07-26-2006
Ural Mutlu wrote:
>>#include <iostream>
>>#include <string>
>>#include <algorithm>
>>#include <iterator>
>>
>>using namespace std;
>>
>>int main() {
>> string str("");
>> int i[2]={48908902,4090799};
>> char const * c_begin
>> = static_cast< char* >( static_cast< void* >( &i[0] ) );
>> char const * c_end = c_begin + sizeof(i);
>> copy( c_begin, c_end, inserter( str, str.end() ) );
>> cout<<str<<endl;
>> return 0;
>>}
>>
>>Beware that the code is bound to invoke undefined behavior as the standard
>>does not impose rigid restrictions on the internal representation of
>>integers. In particular, you may find that endian-ness enters the picture.
>>
>>
>>Best
>>
>>Kai-Uwe Bux

>
>
>
> thanks,
>
> got it to work in a similar way to what you said.
>
> I still don't understand why memcpy or strcpy don't work. The code
> above copies one char at a time until the last element is copied. The
> difference is that memcpy copies a chunk of memory given with the
> pointer and size. Basically they both copy memory and they should do the
> same job?
>

Because you can't write to the value returned by c_str().

--
Ian Collins.
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      07-26-2006
Ural Mutlu wrote:

>>
>> #include <iostream>
>> #include <string>
>> #include <algorithm>
>> #include <iterator>
>>
>> using namespace std;
>>
>> int main() {
>> string str("");
>> int i[2]={48908902,4090799};
>> char const * c_begin
>> = static_cast< char* >( static_cast< void* >( &i[0] ) );
>> char const * c_end = c_begin + sizeof(i);
>> copy( c_begin, c_end, inserter( str, str.end() ) );
>> cout<<str<<endl;
>> return 0;
>> }
>>
>> Beware that the code is bound to invoke undefined behavior as the
>> standard does not impose rigid restrictions on the internal
>> representation of integers. In particular, you may find that endian-ness
>> enters the picture.
>>
>>
>> Best
>>
>> Kai-Uwe Bux

>
>
> thanks,
>
> got it to work in a similar way to what you said.
>
> I still don't understand why memcpy or strcpy don't work. The code
> above copies one char at a time until the last element is copied. The
> difference is that memcpy copies a chunk of memory given with the
> pointer and size. Basically they both copy memory and they should do the
> same job?


Nope. For one thing, note that the string may need to increase in length
while you are appending characters. So, reallocations might occur issued
behind your back and out of your controll by the string class. Thus, if you
try to force memcpy to write into the buffer owned by the string, you are
likely to write beyond its end. Also, how is memcopy supposed to notify the
string object that it became longer? Strings do not use the 0 char as a
terminator, instead they keep track of their length.

In short, all that you do using memcpy is to trash the internal data of the
string. The standard was written with some possible implementations in
mind. Therefore, it declares what you try undefined behavior. And you got
the worst incarnation of it.


Best

Kai-Uwe Bux
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
strcpy of a class object leonkatz@gmail.com C++ 11 01-20-2005 04:44 AM
strcpy and the copy constructor RonHiler C++ 8 10-19-2004 06:30 AM
C++ Compiler with a -Wwarn-use-of-strcpy or similar option?? Paul Sheer C++ 4 09-14-2004 08:38 PM
C++ Compiler with a -Wwarn-use-of-strcpy or similar option?? Paul Sheer C++ 7 09-10-2004 05:07 PM
strcpy Mike Mimic C++ 9 05-17-2004 08:12 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57