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? 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?
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
"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[].
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.
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
"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).
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
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?
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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?
|
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?...
|
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
|
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 );
|
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);
| |
by: Case |
last post by:
#define SIZE 100
#define USE_MEMCPY
int main(void)
{
char a;
char b;
int n;
/* code 'filling' a */
|
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
---
|
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
|
by: sam |
last post by:
(newbie)Technically what's the difference between memset() and
memcpy() functions?
|
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...
|
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,...
| |
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...
|
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,...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |