C++ memory allocation

Discussion in 'C++' started by hungta, Jan 27, 2011.

  1. hungta

    hungta Guest

    Hi friends,

    I have question, who knows is very welcome:

    Assume we have a fragment of code:

    typedef char String [46];
    String* pStr = new String[10];

    The questions are:
    1. How is memory allocated for the pointer pStr?
    2. If we use delete pStr; , is the memory allocated for the pointer
    deleted completely?

    Regards,
    Hung
     
    hungta, Jan 27, 2011
    #1
    1. Advertising

  2. On Jan 27, 1:19 am, hungta <> wrote:
    > Hi friends,
    >
    > I have question, who knows is very welcome:
    >
    > Assume we have a fragment of code:
    >
    > typedef char String [46];
    > String* pStr = new String[10];
    >
    > The questions are:
    > 1. How is memory allocated for the pointer pStr?
    > 2. If we use delete pStr; , is the memory allocated for the pointer
    > deleted completely?


    You would need to use "delete[]", not "delete".

    In short, a piece of memory allocated with "new[]" will remain
    allocated until you release it with "delete[]", and "new" with
    "delete". If you take every pointer returned by new and give it to
    delete, a one to one correspondence, then you will have freed all of
    the memory which you allocated.

    In this example, there is exactly one "new[]", so exactly one
    "delete[]" on the new-ed pointer is sufficient (and necessary) to free
    that memory.
     
    Joshua Maurice, Jan 27, 2011
    #2
    1. Advertising

  3. hungta

    Öö Tiib Guest

    On Jan 27, 11:19 am, hungta <> wrote:
    > Hi friends,
    >
    > I have question, who knows is very welcome:
    >
    > Assume we have a fragment of code:
    >
    > typedef char String [46];
    > String* pStr = new String[10];
    >
    > The questions are:
    > 1. How is memory allocated for the pointer pStr?
    > 2. If we use delete pStr; , is the memory allocated for the pointer
    > deleted completely?


    You should use 'delete [] pStr;'. If you use 'delete pStr;' then it is
    a programming error. On most implementations it releases all the
    memory that was allocated but it is still programming error.
     
    Öö Tiib, Jan 27, 2011
    #3
  4. On 27 Jan., hungta wrote:
    > Hi friends,
    >
    > I have question, who knows is very welcome:
    >
    > Assume we have a fragment of code:
    >
    > typedef char String [46];
    > String* pStr = new String[10];
    >
    > The questions are:
    > 1. How is memory allocated for the pointer pStr?
    > 2. If we use delete pStr; , is the memory allocated for the pointer
    > deleted completely?


    Just an amendment: Most people will interpret the memory _for_ the
    pointer as the memory that is used to hold the pointer variable, which
    is allocated by the compiler on the stack. To be a bit more accurate,
    your question is about the memory that the pointer points to.

    Regards,
    Stuart
     
    Stuart Redmann, Jan 27, 2011
    #4
  5. hungta

    hungta Guest

    On Jan 28, 2:26 am, Paavo Helde <> wrote:
    > hungta <> wrote in news:58a1f104-1a77-47f8-950d-
    > :
    >
    > > Hi friends,

    >
    > > I have question, who knows is very welcome:

    >
    > > Assume we have a fragment of code:

    >
    > > typedef char String [46];
    > > String* pStr = new String[10];

    >
    > Your questions have been answered already. However, there is a question for
    > you: why this piece of code was not written simply as:
    >
    > std::vector<std::string> pStr(10);
    >
    > Or was it just a language tour question?
    >
    > Cheers
    > Paavo


    Hi Paayo,

    Thank you for your reply.

    Yes, they're just my questions to specify how is the memory allocated
    for the pointer.
    First, we type define String as char[46], right?
    Then, we new String[10]; but i wonder how is the memory allocated? is
    it like a two-dimension array of char in which there are number of
    rows is 46 and columns is 10?

    Regards,
    Hung
     
    hungta, Jan 28, 2011
    #5
  6. hungta

    hungta Guest

    On Jan 28, 1:08 pm, Paavo Helde <> wrote:
    > hungta <> wrote innews::
    >
    >
    >
    > > On Jan 28, 2:26 am, Paavo Helde <> wrote:
    > >> hungta <> wrote in news:58a1f104-1a77-47f8-950d-
    > >> :

    >
    > >> > Hi friends,

    >
    > >> > I have question, who knows is very welcome:

    >
    > >> > Assume we have a fragment of code:

    >
    > >> > typedef char String [46];
    > >> > String* pStr = new String[10];

    >
    > >> Your questions have been answered already. However, there is a
    > >> question f

    > > or
    > >> you: why this piece of code was not written simply as:

    >
    > >> std::vector<std::string> pStr(10);

    >
    > >> Or was it just a language tour question?

    >
    > >> Cheers
    > >> Paavo

    >
    > > Hi Paayo,

    >
    > > Thank you for your reply.

    >
    > > Yes, they're just my questions to specify how is the memory allocated
    > > for the pointer.
    > > First, we type define String as char[46], right?
    > > Then, we new String[10]; but i wonder how is the memory allocated? is
    > > it like a two-dimension array of char in which there are number of
    > > rows is 46 and columns is 10?

    >
    > Basically yes, except that the meaning of rows and columns depends on how
    > you interpret your data. The computer memory is addressed one-
    > dimensionally so there is no inherent meaning for these terms.
    >
    > The memory is allocated in one piece. The size of the memory block is 10
    > *sizeof(String), plus any potential overhead related to the array new[]
    > constructor, if the compiler decides to use any. The first String lies at
    > the returned pointer address pStr, the next is sizeof(String) bytes
    > further (accessed as pStr[1]) etc. For example, one can access the last
    > byte in the first String as pStr[0][45];
    >
    > The sizeof(String) itself is 46 as sizeof(char) is 1 by definition. I
    > believe compiler is not allowed to insert any padding. This number is not
    > divisible even by 4, meaning that the individual strings are not aligned
    > on bus width or cache line boundaries and accessing them may thus have
    > some performance penalty. Otherwise this is probably the most efficient
    > format for an array of strings, assuming that most of them are always 45
    > characters long or close to it.
    >
    > The memory is not initialized as the String type is a POD. One can cure
    > this by using this syntax to zero-initialize the content:
    >
    > String* pStr = new String[10]();
    >
    > One must take care when filling in the array as it would be easy to
    > overrun the string borders. One cannot store larger strings than 45
    > characters, one cannot change the number of strings in the array
    > afterwards, one has to keep track the number of strings in the array
    > separately, and one has to take care to delete[] the array after use. All
    > these drawbacks mean that using this kind of array is not really
    > indicated in most situations (save maybe for interfacing some old Fortran
    > code?). If the size of String were 48 or 64, then it might be maybe
    > useful in some kind of specific performance-critical processing.
    >
    > hth
    > Paavo


    Thank you very much for your detailed explanation, paavo.

    Now i get the point.

    Cheers,
    Hung
     
    hungta, Jan 31, 2011
    #6
    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. s.subbarayan

    Dynamic memory allocation and memory leak...

    s.subbarayan, Mar 18, 2005, in forum: C Programming
    Replies:
    10
    Views:
    707
    Eric Sosman
    Mar 22, 2005
  2. Rodrigo Dominguez

    memory allocation and freeing memory

    Rodrigo Dominguez, Jun 13, 2005, in forum: C Programming
    Replies:
    11
    Views:
    605
    Jean-Claude Arbaut
    Jun 15, 2005
  3. Ken
    Replies:
    24
    Views:
    3,875
    Ben Bacarisse
    Nov 30, 2006
  4. chris
    Replies:
    6
    Views:
    992
    chris
    Oct 28, 2005
  5. Bjarke Hammersholt Roune
    Replies:
    14
    Views:
    1,192
    Bjarke Hammersholt Roune
    Mar 6, 2011
Loading...

Share This Page