new() and memcpy()

Discussion in 'C++' started by barcaroller, May 4, 2007.

  1. barcaroller

    barcaroller Guest

    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?
     
    barcaroller, May 4, 2007
    #1
    1. Advertising

  2. barcaroller

    pmouse Guest

    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::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" <> 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?
     
    pmouse, May 4, 2007
    #2
    1. Advertising

  3. 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
     
    Markus Schoder, May 4, 2007
    #3
  4. barcaroller

    barcaroller Guest

    "Markus Schoder" <> wrote in message
    news:463bb619$0$6399$-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, May 5, 2007
    #4
  5. barcaroller

    Ian Collins Guest

    barcaroller wrote:
    > "Markus Schoder" <> wrote in message
    > news:463bb619$0$6399$-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.
     
    Ian Collins, May 5, 2007
    #5
  6. barcaroller

    Default User Guest

    barcaroller wrote:

    >
    > "Markus Schoder" <> wrote in message
    > news:463bb619$0$6399$-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
     
    Default User, May 5, 2007
    #6
  7. barcaroller

    Jim Langston Guest

    "barcaroller" <> wrote in message
    news:f1gfh9$5bh$...
    >
    > "Markus Schoder" <> wrote in message
    > news:463bb619$0$6399$-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).
     
    Jim Langston, May 5, 2007
    #7
  8. barcaroller

    James Kanze Guest

    On May 5, 12:31 am, pmouse <> 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::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:
    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
     
    James Kanze, May 5, 2007
    #8
  9. barcaroller

    Rolf Magnus Guest

    Default User wrote:

    > barcaroller wrote:
    >
    >>
    >> "Markus Schoder" <> wrote in message
    >> news:463bb619$0$6399$-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?
     
    Rolf Magnus, May 5, 2007
    #9
  10. barcaroller

    James Kanze Guest

    On May 5, 1:08 pm, Rolf Magnus <> 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:
    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
     
    James Kanze, May 5, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bren
    Replies:
    8
    Views:
    2,057
    Stephen Howe
    Sep 4, 2003
  2. Amon Tse

    wcscpy and memcpy

    Amon Tse, Apr 26, 2005, in forum: C++
    Replies:
    1
    Views:
    2,653
    Ron Natalie
    Apr 26, 2005
  3. Mike Wahler

    resolved: difference between memcpy and memmove

    Mike Wahler, Sep 27, 2003, in forum: C Programming
    Replies:
    21
    Views:
    1,448
    Jirka Klaue
    Sep 30, 2003
  4. Case

    memcpy() and endianness

    Case, May 10, 2004, in forum: C Programming
    Replies:
    26
    Views:
    1,888
    Dan Pop
    May 12, 2004
  5. Replies:
    2
    Views:
    501
    Thomas 'PointedEars' Lahn
    Mar 11, 2008
Loading...

Share This Page