473,734 Members | 2,789 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

new() and memcpy()


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_bloc k, 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?
May 4 '07 #1
9 10260

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_bloc k, 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, "barcarolle r" <barcarol...@mu sic.netwrote:
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_bloc k, 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?

May 4 '07 #2
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_bloc k, c_block, 100);
Make that memcpy(cpp_bloc k, 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_blo ck, c_block + 100);

of if you need to initialize later:

#include <vector>
#include <algorithm>

std::vector<int cpp_block(100);

....

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

--
Markus

May 4 '07 #3

"Markus Schoder" <a3************ *@yahoo.dewrote in message
news:46******** **************@ newsspool2.arco r-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[].
May 4 '07 #4
barcaroller wrote:
"Markus Schoder" <a3************ *@yahoo.dewrote in message
news:46******** **************@ newsspool2.arco r-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.
May 4 '07 #5
barcaroller wrote:
>
"Markus Schoder" <a3************ *@yahoo.dewrote in message
news:46******** **************@ newsspool2.arco r-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
May 5 '07 #6
"barcarolle r" <ba*********@mu sic.netwrote in message
news:f1******** **@aioe.org...
>
"Markus Schoder" <a3************ *@yahoo.dewrote in message
news:46******** **************@ newsspool2.arco r-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).
May 5 '07 #7
On May 5, 12:31 am, pmouse <pmo...@cogeco. cawrote:
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_bloc k, 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: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 5 '07 #8
Default User wrote:
barcaroller wrote:
>>
"Markus Schoder" <a3************ *@yahoo.dewrote in message
news:46******* *************** @newsspool2.arc or-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?

May 5 '07 #9
On May 5, 1:08 pm, Rolf Magnus <ramag...@t-online.dewrote:
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: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
May 5 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
17509
by: franky.backeljauw | last post by:
Hello, following my question on "std::copy versus pointer copy versus member copy", I had some doubts on the function memcpy, as was used by tom_usenet in his reply. - Is this a c++ standard library function? That is, can I be sure that every c++ standard library has this function? Or is there a c++ alternative to it?
822
29570
by: Turamnvia Suouriviaskimatta | last post by:
I 'm following various posting in "comp.lang.ada, comp.lang.c++ , comp.realtime, comp.software-eng" groups regarding selection of a programming language of C, C++ or Ada for safety critical real-time applications. The majority of expert/people recommend Ada for safety critical real-time applications. I've many years of experience in C/C++ (and Delphi) but no Ada knowledge. May I ask if it is too difficult to move from C/C++ to Ada?...
5
3746
by: manya | last post by:
Ok, it's been a while since I've done the whole memcpy stuff with C++ and I'm having a hard time remembering everything. I hope, however, that you can help me with my problem. I memcpy a struct into a buffer to put it into a database (BerkeleyDB, to be specific) - what do I do to memcpy the thing back? Code Example: //struct I want to work with
35
12050
by: Christopher Benson-Manica | last post by:
(if this is a FAQ or in K&R2, I didn't find it) What parameters (if any) may be 0 or NULL? IOW, which of the following statements are guaranteed to produce well-defined behavior? char src; char dst; memcpy( dst, src, 1 ); memcpy( NULL, src, 1 );
16
15926
by: Amarendra GODBOLE | last post by:
Hi, I am a bit confused over the correct usage of memcpy(). Kindly help me clear the confusion. The linux manpage for memcpy(3) gives me the following prototype of memcpy(3): #include <string.h> void *memcpy(void *dest, const void *src, size_t n);
33
33740
by: Case | last post by:
#define SIZE 100 #define USE_MEMCPY int main(void) { char a; char b; int n; /* code 'filling' a */
6
2946
by: myhotline | last post by:
hi all im very confused about using memcpy and i have three questions....memcpy takes a pointer to src and a pointer to dest and copies src to destination...but im very confuzed about when to use '&' operator while using memcpy....i have code that use '&' and the code that call memcpy without '&' like is the following same Quest1 ---
3
2283
by: Bartholomew Simpson | last post by:
I am writing some C++ wrappers around some legacy C ones - more specifically, I am providing ctors, dtors and assignment operators for the C structs. I have a ton of existing C code that uses these structs. A typical usage case will be as ff (note the code below is Pseudocode and WILL NOT compile) //example structs (I have left out the ctors/dtors etc for brevity sake) struct MyStructA
18
2704
by: sam | last post by:
(newbie)Technically what's the difference between memset() and memcpy() functions?
0
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8776
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9236
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2724
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.