'new'd memeroy is contiguous?

Discussion in 'C++' started by divya_rathore_@gmail.com, Dec 19, 2005.

  1. Guest

    No pun intended in the subject :)

    Is dynamically allocated memory contiguous in C++? In C? Deails would
    be appreciated.


    warm regards,
    Divya Rathore
    (remove underscores for email ID)
     
    , Dec 19, 2005
    #1
    1. Advertising

  2. wrote:
    > No pun intended in the subject :)


    memeroy?

    > Is dynamically allocated memory contiguous in C++? In C? Deails would
    > be appreciated.


    Yes, it is. What details do you expect?
     
    Victor Bazarov, Dec 19, 2005
    #2
    1. Advertising

  3. On 19 Dec 2005 09:04:23 -0800, ""
    <> wrote:

    >Is dynamically allocated memory contiguous in C++? In C? Details would
    >be appreciated.


    C questions are off-topic here unless they are somehow also relevant
    to C++ (comp.lang.c is the place to ask), so I'll limit myself to the
    question concerning "new".

    Do you mean, contiguous across different calls to new? If so, the
    answer is: not necessarily, because it is implementation-defined how
    new obtains and utilizes its raw memory. I would say, no, because
    unused blocks of memory released by calling delete are normally
    recycled within the total memory used by the process taken from its
    free store (i.e. heap memory) provided by the operating system. Often,
    the memory is only returned to the operating system when the process
    ends, but that is also an implementation-specific detail which can be
    quite different on another compiler and operating system.

    A class can also overload new and delete to use a different memory
    allocation scheme.

    Otherwise, I don't really understand your question. Allocations
    performed by new allocate a single object of a given type. Allocations
    performed by new[] allocate arrays of objects of the same type.
    Although the memory occupied by the array is guaranteed to be
    contiguous, and the memory occupied by any given object is contiguous,
    it is implementation-defined how the compiler lays out the individual
    members of the object in that memory. The implementation (i.e. your
    compiler) is free to add padding bytes between the individual members
    for purposes of alignment and can be influenced by different
    optimization and alignment settings at compile-time (e.g., see the
    documentation for "#pragma pack" if your compiler supports it).

    Consider the following:

    struct X {
    short a;
    char b;
    int c;
    };

    What is sizeof X here? Depending on the compiler settings, you will
    most likely get an additional byte of padding thrown in after the char
    member. However, it is also possible that there are two extra bytes
    after the short member as well. Switch around the order of the members
    and see if you get different results.

    Of course, an instance of a class can allocate additional memory and
    store just the pointer to that memory. When querying the size of such
    an object with sizeof(), only the memory occupied by the pointer would
    count towards the total size of the object.

    If you want more details, I suggest you get a copy of the C++ standard
    at: http://www.ansi.org

    --
    Bob Hairgrove
     
    Bob Hairgrove, Dec 19, 2005
    #3
  4. On Mon, 19 Dec 2005 12:26:41 -0500, Victor Bazarov
    <> wrote:

    > wrote:
    >> No pun intended in the subject :)

    >
    >memeroy?


    Just a typo or misspelling.
    I think the pun was with "new'd" and "nude" (groan...)

    --
    Bob Hairgrove
     
    Bob Hairgrove, Dec 19, 2005
    #4
  5. Bob Hairgrove wrote:
    > On Mon, 19 Dec 2005 12:26:41 -0500, Victor Bazarov
    > <> wrote:
    >
    >
    >> wrote:
    >>
    >>>No pun intended in the subject :)

    >>
    >>memeroy?

    >
    >
    > Just a typo or misspelling.
    > I think the pun was with "new'd" and "nude" (groan...)


    Ah... OK. I was thinking of "hemorrhoids".
     
    Victor Bazarov, Dec 19, 2005
    #5
  6. Default User Guest

    wrote:

    > No pun intended in the subject :)
    >
    > Is dynamically allocated memory contiguous in C++?


    Yes.

    > In C?


    Yes.

    > Deails would be appreciated.


    What details do you want? It's contiguous, that's how you have dynamic
    arrays.


    Brian

    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.
     
    Default User, Dec 19, 2005
    #6
  7. Guest

    Thank you all..
    so, in:


    int* x;
    x = new int[MAX];

    memory allocated is contiguous for how-so-ever-large MAX (till system
    supports)?
     
    , Dec 19, 2005
    #7
  8. Guest

    >> Is dynamically allocated memory contiguous in C++? In C? Deails would
    >> be appreciated.



    >Yes, it is. What details do you expect?


    As someone pointed, asking about malloc/calloc is out of scope of this
    newsgroup, so I will restrain myself.
    Thanks anyways.
     
    , Dec 19, 2005
    #8
  9. wrote:
    >>>Is dynamically allocated memory contiguous in C++? In C? Deails would
    >>>be appreciated.

    >
    >
    >
    >>Yes, it is. What details do you expect?

    >
    >
    > As someone pointed, asking about malloc/calloc is out of scope of this
    > newsgroup, so I will restrain myself.


    Actually, since C Standard Library circa 1990 is part of C++ Standard
    Library, it's not out of scope to talk about malloc or calloc. Please
    ask your library questions as you wish, it's absolutely _in_ the scope
    of this newsgroup. C _language_ questions are better asked in
    comp.lang.c, however.

    V
     
    Victor Bazarov, Dec 19, 2005
    #9
  10. On 19 Dec 2005 12:18:29 -0800, ""
    <> wrote:

    >Thank you all..
    >so, in:
    >
    >
    >int* x;
    >x = new int[MAX];
    >
    >memory allocated is contiguous for how-so-ever-large MAX (till system
    >supports)?


    The limit on the array size (MAX) is not specified by the C++
    standard. If the call to new succeeds, x will be a valid pointer to
    that memory. Otherwise, an exception (std::bad_alloc) is thrown.

    If you need to allocate very large sections of RAM, e.g. for image
    processing, you can also do it with operating-system specific means
    and use placement new.

    --
    Bob Hairgrove
     
    Bob Hairgrove, Dec 19, 2005
    #10
  11. wrote:
    > Thank you all..
    > so, in:
    >
    >
    > int* x;
    > x = new int[MAX];


    Why not write them in the same statement?

    > memory allocated is contiguous for how-so-ever-large MAX (till system
    > supports)?


    "how-so-ever-large"? Google only has three links with that text in them
    (and without hyphens, mind you). I have to admit, I'd never seen it
    before reading your post. Some new English every day -- that's my goal.

    The default allocation mechanism (if that's the one you're asking about)
    will attempt to locate a contiguous segment of memory large enough to
    accommodate the array. If it fails, an exception will be thrown. You can
    overload 'new[]' and do whatever you would like...

    V
     
    Victor Bazarov, Dec 19, 2005
    #11
  12. Guest

    wrote:
    > No pun intended in the subject :)
    >
    > Is dynamically allocated memory contiguous in C++? In C? Deails would
    > be appreciated.
    >
    >
    > warm regards,
    > Divya Rathore
    > (remove underscores for email ID)


    Dynamically allocated memory is contiguous. Don't confuse that with a
    dynamically allocated object is contiguous.

    class MyObj {
    public:
    MyObj()
    : blockA( new char [12] )
    , blockB( new int [12] )
    {}
    ~MyObj()
    { delete [] blockA;
    delete [] blockB;
    }
    private:
    char *blockA;
    int*blockB;
    };

    MyObj *p = new MyObj();

    MyObj is contiguous. blockA is contiguous. blockB is contiguous. But
    MyObj, blockA and blockB as a group might not be contiguous.
     
    , Dec 19, 2005
    #12
  13. wrote:
    > wrote:
    >
    >>No pun intended in the subject :)
    >>
    >>Is dynamically allocated memory contiguous in C++? In C? Deails would
    >>be appreciated.
    >>
    >>
    >>warm regards,
    >>Divya Rathore
    >>(remove underscores for email ID)

    >
    >
    > Dynamically allocated memory is contiguous. Don't confuse that with a
    > dynamically allocated object is contiguous.


    Every object occupies a contiguous block of memory, so I am not sure
    where your second sentence is coming from.

    >
    > class MyObj {
    > public:
    > MyObj()
    > : blockA( new char [12] )
    > , blockB( new int [12] )
    > {}
    > ~MyObj()
    > { delete [] blockA;
    > delete [] blockB;
    > }
    > private:
    > char *blockA;
    > int*blockB;
    > };
    >
    > MyObj *p = new MyObj();
    >
    > MyObj is contiguous. blockA is contiguous. blockB is contiguous. But
    > MyObj, blockA and blockB as a group might not be contiguous.


    Why are they suddenly "a group"?

    V
     
    Victor Bazarov, Dec 19, 2005
    #13
  14. Kaz Kylheku Guest

    wrote:
    > No pun intended in the subject :)
    >
    > Is dynamically allocated memory contiguous in C++? In C? Deails would
    > be appreciated.


    Suppose it wasn't contiguous. You get one pointer out of the allocator,
    which points to uninitialized storage. How would your program find the
    other pieces? With what information?
     
    Kaz Kylheku, Dec 19, 2005
    #14
  15. Kaz Kylheku wrote:
    > wrote:
    >
    >>No pun intended in the subject :)
    >>
    >>Is dynamically allocated memory contiguous in C++? In C? Deails would
    >>be appreciated.

    >
    >
    > Suppose it wasn't contiguous. You get one pointer out of the allocator,
    > which points to uninitialized storage. How would your program find the
    > other pieces? With what information?
    >


    Somehow (in an implementation-defined manner) the pointer to a dynamic
    array would be different from a pointer to any other array and any pointer
    arithmetic on it would be performed using some magic (implementation-
    specific magic, mind you). So, indexing, increments, pointer difference,
    would be done for a pointer from 'new[]' somehow differently than for
    a pointer to an element of an automatic or static array. Possible?

    V
     
    Victor Bazarov, Dec 19, 2005
    #15
  16. Victor Bazarov wrote:
    > Bob Hairgrove wrote:
    >
    >> On Mon, 19 Dec 2005 12:26:41 -0500, Victor Bazarov
    >> <> wrote:
    >>
    >>
    >>> wrote:
    >>>
    >>>> No pun intended in the subject :)
    >>>
    >>>
    >>> memeroy?

    >>
    >>
    >>
    >> Just a typo or misspelling.
    >> I think the pun was with "new'd" and "nude" (groan...)

    >
    >
    > Ah... OK. I was thinking of "hemorrhoids".


    Well, what can I say in my C++'d state, I'd never have C'n it coming.
    Oh, the joys of a symbiotic relationship with yeast, alcohol being
    derived from yeast etc, etc.

    JB
     
    n2xssvv g02gfr12930, Dec 19, 2005
    #16
  17. Kaz Kylheku Guest

    Victor Bazarov wrote:
    > Kaz Kylheku wrote:
    > > wrote:
    > >
    > >>No pun intended in the subject :)
    > >>
    > >>Is dynamically allocated memory contiguous in C++? In C? Deails would
    > >>be appreciated.

    > >
    > >
    > > Suppose it wasn't contiguous. You get one pointer out of the allocator,
    > > which points to uninitialized storage. How would your program find the
    > > other pieces? With what information?
    > >

    >
    > Somehow (in an implementation-defined manner) the pointer to a dynamic
    > array would be different from a pointer to any other array and any pointer
    > arithmetic on it would be performed using some magic (implementation-
    > specific magic, mind you). So, indexing, increments, pointer difference,
    > would be done for a pointer from 'new[]' somehow differently than for
    > a pointer to an element of an automatic or static array. Possible?


    The semantics of these pointer operations is what defines contiguity.
    :)
     
    Kaz Kylheku, Dec 20, 2005
    #17
  18. Guest

    Victor Bazarov wrote:

    > "how-so-ever-large"? Google only has three links with that text in them
    > (and without hyphens, mind you). I have to admit, I'd never seen it
    > before reading your post. Some new English every day -- that's my goal.


    hyphens were to put stress. I, well, could have done with italisizing
    the phrase had it been allowed (I am using web interface of google
    groups). Yes.. English, being a flexible language, allows one to be
    experimental.


    > The default allocation mechanism (if that's the one you're asking about)
    > will attempt to locate a contiguous segment of memory large enough to
    > accommodate the array. If it fails, an exception will be thrown. You can
    > overload 'new[]' and do whatever you would like...
    >
    > V



    Thanks! You and others have been very informative.
     
    , Dec 20, 2005
    #18
  19. Guest

    Kaz Kylheku wrote:
    > Suppose it wasn't contiguous. You get one pointer out of the allocator,
    > which points to uninitialized storage. How would your program find the
    > other pieces? With what information?



    Yes, that's what I was worried about.
    I, indeed, am working in a image processing application and was trying
    to figure out if a 2D array is better for dynamic allocation or a big
    1-dimensional one.

    Case 1: 1D case will allocate it in one go. So contguity is there
    indeed.


    Case 2: 2D will involve memory to be new'd (again, grammatical usage is
    casual here. This is *NOT* correct english.) once for the columns and
    then itertively with in a 'for' loop so as to cover the rows.
    int** array2D = new int*[height];
    for (int i=0; i<height; i++)
    {
    array2D = new int[width];
    }

    My querry is that whether this array2D is contiguous or not.

    So far so good.. now which of these 2 cases will be faster to access?
    Obviously the one which is contiguous. Hence this question on the
    newsgroup.
     
    , Dec 20, 2005
    #19
  20. Guest

    wrote:
    > Case 1: 1D case will allocate it in one go. So contguity is there
    > indeed.


    Yes, and you could use this allocated memory as a 2D array, if you
    wanted to.

    int** array2D = new int*[height];
    int* temp = new int[height*width];
    for (int i=0; i<height; ++i)
    {
    array2D = &(temp[i*width]);
    }

    > Case 2: 2D will involve memory to be new'd (again, grammatical usage is
    > casual here. This is *NOT* correct english.) once for the columns and
    > then itertively with in a 'for' loop so as to cover the rows.
    > int** array2D = new int*[height];
    > for (int i=0; i<height; i++)
    > {
    > array2D = new int[width];
    > }
    >
    > My querry is that whether this array2D is contiguous or not.


    Not necessarily.

    > So far so good.. now which of these 2 cases will be faster to access?
    > Obviously the one which is contiguous. Hence this question on the
    > newsgroup.


    The obvious is not necessarily the case. It could be that because of
    cache hits/misses, the non-contiguous one is faster. You should just
    try it and see.

    Another thing that no one mentioned. Do you mean physically contiguous,
    or virtually contiguous. It is a fact that the memory allocated will be
    virtually contiguous. It is not a fact that the memory will be
    physically contiguous.
     
    , Dec 20, 2005
    #20
    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. Newsgroup - Ann
    Replies:
    0
    Views:
    415
    Newsgroup - Ann
    Aug 15, 2003
  2. Ravi
    Replies:
    8
    Views:
    10,399
    Mike Wahler
    Mar 6, 2004
  3. Paul
    Replies:
    1
    Views:
    540
    Jack Klein
    Nov 24, 2004
  4. panbru

    Contiguous memory blocks

    panbru, Aug 5, 2003, in forum: Python
    Replies:
    0
    Views:
    369
    panbru
    Aug 5, 2003
  5. Mark McDougall

    Non-contiguous port vector ranges???

    Mark McDougall, Nov 2, 2006, in forum: VHDL
    Replies:
    2
    Views:
    822
    Jim Lewis
    Nov 2, 2006
Loading...

Share This Page