Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > new() and memcpy()

Reply
Thread Tools

new() and memcpy()

 
 
barcaroller
Guest
Posts: n/a
 
      05-04-2007

Is it safe to use the result of 'new int[100]' in a memcpy().

Example:

int* cpp_block = new int[100];
int* c_block = some_c_function();

memcpy(cpp_block, c_block, 100);


I suspect cpp_block may be pointing to more than just a simple array of
integers.

If it's not safe, I could use a vector instead of the 'new int[100]' but how
do I initialize the vector using the C-style array without having to iterate
over the vector one integer at a time?


 
Reply With Quote
 
 
 
 
pmouse
Guest
Posts: n/a
 
      05-04-2007

barcaroller wrote:
> Is it safe to use the result of 'new int[100]' in a memcpy().
>
> Example:
>
> int* cpp_block = new int[100];
> int* c_block = some_c_function();
>
> memcpy(cpp_block, c_block, 100);
>
>
> I suspect cpp_block may be pointing to more than just a simple array of
> integers.
>
> If it's not safe, I could use a vector instead of the 'new int[100]' but how
> do I initialize the vector using the C-style array without having to iterate
> over the vector one integer at a time?


If you haven't overloaded the new operator, this is what it does
(pseudo code):
int* cpp_block = (int*)malloc( sizeof(int) * 100 );
for ( int i = 0; i < 100; ++i )
cpp_block[i]::int();

so yes, there is nothing more magical about the default new operator.
It is safe to copy it around (obviously the size of not 100, but
sizeof(int)*100, but i assume that you know what your doing)

run this code to see:

int * new_arr = new int[100];
int * old_arr = (int*)malloc( sizeof(int)*100 );
memset( old_arr, 0, sizeof(int)*100);

unsigned char* new_begin = (unsigned char*)new_arr;
unsigned char* old_begin = (unsigned char*)old_arr;

for ( int i = 0; i < sizeof(int)*100; ++i )
{
if ( *new_begin++ != *old_begin++ )
cout << "problem!" << endl;
}

delete[] new_arr;
free(old_arr);


On May 4, 6:21 pm, "barcaroller" <(E-Mail Removed)> wrote:
> Is it safe to use the result of 'new int[100]' in a memcpy().
>
> Example:
>
> int* cpp_block = new int[100];
> int* c_block = some_c_function();
>
> memcpy(cpp_block, c_block, 100);
>
> I suspect cpp_block may be pointing to more than just a simple array of
> integers.
>
> If it's not safe, I could use a vector instead of the 'new int[100]' but how
> do I initialize the vector using the C-style array without having to iterate
> over the vector one integer at a time?



 
Reply With Quote
 
 
 
 
Markus Schoder
Guest
Posts: n/a
 
      05-04-2007
barcaroller wrote:
> Is it safe to use the result of 'new int[100]' in a memcpy().
>
> Example:
>
> int* cpp_block = new int[100];
> int* c_block = some_c_function();
>
> memcpy(cpp_block, c_block, 100);


Make that memcpy(cpp_block, c_block, 100 * sizeof(int)) and it should be
ok.

> I suspect cpp_block may be pointing to more than just a simple array
> of integers.


Nope. Just an array.

> If it's not safe, I could use a vector instead of the 'new int[100]'
> but how do I initialize the vector using the C-style array without
> having to iterate over the vector one integer at a time?


Using a vector is recommended because it will also do the deallocation
for you. Initialization is easy:

#include <vector>

std::vector<int> cpp_block(c_block, c_block + 100);

of if you need to initialize later:

#include <vector>
#include <algorithm>

std::vector<int> cpp_block(100);

....

std::copy(c_block, c_block + 100, cpp_block.begin());

--
Markus

 
Reply With Quote
 
barcaroller
Guest
Posts: n/a
 
      05-04-2007

"Markus Schoder" <(E-Mail Removed)> wrote in message
news:463bb619$0$6399$(E-Mail Removed)-online.net...

>> I suspect cpp_block may be pointing to more than just a simple array
>> of integers.

>
> Nope. Just an array.


But doesn't C++ (unlike C) need to keep track of the size of the array?
Hence the difference between delete and delete[].


 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      05-04-2007
barcaroller wrote:
> "Markus Schoder" <(E-Mail Removed)> wrote in message
> news:463bb619$0$6399$(E-Mail Removed)-online.net...
>
>>> I suspect cpp_block may be pointing to more than just a simple array
>>> of integers.

>> Nope. Just an array.

>
> But doesn't C++ (unlike C) need to keep track of the size of the array?
> Hence the difference between delete and delete[].
>


In this case, it doesn't matter, you are just copying block of memory.

The C++ runtime may be doing housekeeping under the hood, but that's not
your problem, you just see a contiguous block of memory 100*sizeof(int)
long.

--
Ian Collins.
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      05-05-2007
barcaroller wrote:

>
> "Markus Schoder" <(E-Mail Removed)> wrote in message
> news:463bb619$0$6399$(E-Mail Removed)-online.net...
>
> > > I suspect cpp_block may be pointing to more than just a simple
> > > array of integers.

> >
> > Nope. Just an array.

>
> But doesn't C++ (unlike C) need to keep track of the size of the
> array? Hence the difference between delete and delete[].


More likely the OS keeps track of it. From the standpoint of the user
it's plain block of memory, in this case aligned for use as an array of
ints.




Brian
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      05-05-2007
"barcaroller" <(E-Mail Removed)> wrote in message
news:f1gfh9$5bh$(E-Mail Removed)...
>
> "Markus Schoder" <(E-Mail Removed)> wrote in message
> news:463bb619$0$6399$(E-Mail Removed)-online.net...
>
>>> I suspect cpp_block may be pointing to more than just a simple array
>>> of integers.

>>
>> Nope. Just an array.

>
> But doesn't C++ (unlike C) need to keep track of the size of the array?
> Hence the difference between delete and delete[].


It may, it may not. But if it does it's transparent to you. Maybe the
number it allocated is kept in an extra bit of memory before the pointer you
are returned, or after the block. Either way, the pointer you have points
to a block of 100 ints. It's not safe to use free to release a block of
memory obtained with new for possible housekeeping reasons though because
the pointer you are given by new[] is not neccessarily the pointer to the
memory allocated (it may point to sizeof size_t after the start for
instance).


 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      05-05-2007
On May 5, 12:31 am, pmouse <(E-Mail Removed)> wrote:
> barcaroller wrote:
> > Is it safe to use the result of 'new int[100]' in a memcpy().


> > Example:


> > int* cpp_block = new int[100];
> > int* c_block = some_c_function();


> > memcpy(cpp_block, c_block, 100);


> > I suspect cpp_block may be pointing to more than just a simple array of
> > integers.


There's obviously more somewhere, but cpp_block points to the
first of 100 consecutive int's.

> > If it's not safe, I could use a vector instead of the 'new int[100]' but how
> > do I initialize the vector using the C-style array without having to iterate
> > over the vector one integer at a time?


> If you haven't overloaded the new operator, this is what it does
> (pseudo code):
> int* cpp_block = (int*)malloc( sizeof(int) * 100 );
> for ( int i = 0; i < 100; ++i )
> cpp_block[i]::int();


Where do you get this from? There's no guarantee that operator
new uses malloc (although it is a frequent implementation). And
operator new certainly isn't required to initialize the ints
with 0; most of the ones I've seen don't.

--
James Kanze (Gabi Software) email: http://www.velocityreviews.com/forums/(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      05-05-2007
Default User wrote:

> barcaroller wrote:
>
>>
>> "Markus Schoder" <(E-Mail Removed)> wrote in message
>> news:463bb619$0$6399$(E-Mail Removed)-online.net...
>>
>> > > I suspect cpp_block may be pointing to more than just a simple
>> > > array of integers.
>> >
>> > Nope. Just an array.

>>
>> But doesn't C++ (unlike C) need to keep track of the size of the
>> array? Hence the difference between delete and delete[].

>
> More likely the OS keeps track of it.


I'd say that's rather unlikely.

> From the standpoint of the user it's plain block of memory, in this case
> aligned for use as an array of ints.


Isn't it even aligned for any use?

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      05-05-2007
On May 5, 1:08 pm, Rolf Magnus <(E-Mail Removed)> wrote:
> Default User wrote:


[...]
> > From the standpoint of the user it's plain block of memory, in this case
> > aligned for use as an array of ints.


> Isn't it even aligned for any use?


It's not guaranteed. The return value of the operator new()
function must be sufficiently aligned for any use, as must new
of a character type, but for the others, all that's guaranteed
is sufficient alignment for the allocated type.

--
James Kanze (Gabi Software) email: (E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


 
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
if and and vs if and,and titi VHDL 4 03-11-2007 05:23 AM



Advertisments