Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C++ memory allocation

Reply
Thread Tools

C++ memory allocation

 
 
hungta
Guest
Posts: n/a
 
      01-27-2011
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
 
Reply With Quote
 
 
 
 
Joshua Maurice
Guest
Posts: n/a
 
      01-27-2011
On Jan 27, 1:19*am, hungta <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Öö Tiib
Guest
Posts: n/a
 
      01-27-2011
On Jan 27, 11:19*am, hungta <(E-Mail Removed)> 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.
 
Reply With Quote
 
Stuart Redmann
Guest
Posts: n/a
 
      01-27-2011
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
 
Reply With Quote
 
hungta
Guest
Posts: n/a
 
      01-28-2011
On Jan 28, 2:26*am, Paavo Helde <(E-Mail Removed)> wrote:
> hungta <(E-Mail Removed)> wrote in news:58a1f104-1a77-47f8-950d-
> (E-Mail Removed):
>
> > 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
 
Reply With Quote
 
hungta
Guest
Posts: n/a
 
      01-31-2011
On Jan 28, 1:08*pm, Paavo Helde <(E-Mail Removed)> wrote:
> hungta <(E-Mail Removed)> wrote innews:(E-Mail Removed):
>
>
>
> > On Jan 28, 2:26 am, Paavo Helde <(E-Mail Removed)> wrote:
> >> hungta <(E-Mail Removed)> wrote in news:58a1f104-1a77-47f8-950d-
> >> (E-Mail Removed):

>
> >> > 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
An idea for heap allocation at near stack allocation speed Bjarke Hammersholt Roune C++ 14 03-06-2011 08:07 AM
static memory allocation versus dynamic memory allocation Ken C Programming 24 11-30-2006 12:37 AM
What is the difference between dynamic memory allocation,and stack allocation ? chris C++ 6 10-28-2005 05:27 AM
memory allocation and freeing memory Rodrigo Dominguez C Programming 11 06-14-2005 11:54 PM
Dynamic memory allocation and memory leak... s.subbarayan C Programming 10 03-22-2005 02:48 PM



Advertisments