Topic: Storage duration of objects created with malloc()
Author: andy.champ@tescoREMOVETHIS.net (Andy Champ)
Date: Tue, 20 May 2003 16:08:35 +0000 (UTC) Raw View
KIM Seungbeom wrote:
> musiphil@bawi.org (KIM Seungbeom) wrote in message news:<bd47bb0e.0303212203.5b045bb2@posting.google.com>...
>
>>francis.glassborow@ntlworld.com (Francis Glassborow) wrote in message news:<lzhIaaDWLvd+Ew+G@robinton.demon.co.uk>...
>>
>>>In article <bd47bb0e.0303171811.656dbf71@posting.google.com>, KIM
>>>Seungbeom <musiphil@bawi.org> writes
>>>
>>>>How about POD objects?
>>>>
>>>> int* pi = static_cast<int*>(malloc(sizeof(int)));
>>>>
>>>>In this case, what is the storage duration of the int object(*pi)?
>>>
>>>You have to look in several places, including the C89 Standard and the
>>>relationship between new style casts and the C-style cast to deduce that
>>>it has dynamic storage duration.
>>
>>Sorry I don't understand.
>>The Technical Corrigendum 2 of the C90 Standard states that
>>malloc'ed objects have "allocated" storage duration.
>>And what do the casts have to do with the storage duration of objects?
>
>
> I got no answer from Francis Glassborow.
> (Is it that you answered but it didn't reach me?)
>
> Reading through this thread again, I don't find anything definitive
> and I'm still confused. People don't seem to agree even whether an
> object is created by malloc() or not; so I would be attempting the
> impossible to ask them to clarify its storage duration, wouldn't I?
>
> Probably there's a defect in the standard.
> (I'm not sure if I would be able to proceed to submitting a DR, though.)
>
> Any further opinions are welcome.
>
Not being a standards person, I'll put it in English.
The variable pi will exist until the end of the scope
(close-curly-bracket). The data which it points to, which was allocated
by the malloc, will last until free is called. This can be forever.
Andy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: musiphil@bawi.org (KIM Seungbeom)
Date: Mon, 19 May 2003 17:38:44 +0000 (UTC) Raw View
musiphil@bawi.org (KIM Seungbeom) wrote in message news:<bd47bb0e.0303212203.5b045bb2@posting.google.com>...
> francis.glassborow@ntlworld.com (Francis Glassborow) wrote in message news:<lzhIaaDWLvd+Ew+G@robinton.demon.co.uk>...
> > In article <bd47bb0e.0303171811.656dbf71@posting.google.com>, KIM
> > Seungbeom <musiphil@bawi.org> writes
> > >
> > >How about POD objects?
> > >
> > > int* pi = static_cast<int*>(malloc(sizeof(int)));
> > >
> > >In this case, what is the storage duration of the int object(*pi)?
> >
> > You have to look in several places, including the C89 Standard and the
> > relationship between new style casts and the C-style cast to deduce that
> > it has dynamic storage duration.
>
> Sorry I don't understand.
> The Technical Corrigendum 2 of the C90 Standard states that
> malloc'ed objects have "allocated" storage duration.
> And what do the casts have to do with the storage duration of objects?
I got no answer from Francis Glassborow.
(Is it that you answered but it didn't reach me?)
Reading through this thread again, I don't find anything definitive
and I'm still confused. People don't seem to agree even whether an
object is created by malloc() or not; so I would be attempting the
impossible to ask them to clarify its storage duration, wouldn't I?
Probably there's a defect in the standard.
(I'm not sure if I would be able to proceed to submitting a DR, though.)
Any further opinions are welcome.
--
KIM Seungbeom <musiphil@bawi.org>
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: a9804814@unet.univie.ac.at (Thomas Mang)
Date: Fri, 21 Mar 2003 20:47:14 +0000 (UTC) Raw View
Francis Glassborow schrieb:
> In article <7f2735a5.0303191645.69a02ffd@posting.google.com>, Allan W
> <allan_w@my-dejanews.com> writes
> >Basically, an object
> >becomes an object when we say, "don't use this memory for anything
> >else." That's the same thing as allocating memory.
>
> Exactly, and until we actually say what type of object we are going to
> use it for it is just unformed memory that our program now controls.
>
> And memory can be part of more than one object:-)
But that's not what the standard says (at least the way I interpret it).
As already said, if your interpretation was the intent of the standard, fix
it. Add a third requirement to 3.8/1. Make it simply clear - no matter what
was really the intent of the standard.
Otherwise it remains, at least IMO, dubious, confusing and arguable.
best regards,
Thomas
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: llewelly.at@xmission.dot.com (LLeweLLyn)
Date: Mon, 24 Mar 2003 23:24:32 +0000 (UTC) Raw View
allan_w@my-dejanews.com (Allan W) writes:
[snip]
> 1. Allocate memory with proper alignment, and
> 2. The Object Creator waves it's magic wand and completes (physically
> or virtually) the magic chant: "O greate Bjarne, bless this Object
> that it may serve us all, without exception" -- which magically
> transforms the raw memory into an object (although the object has
> not yet been initialized), and
You are joking, and so was Larry Wall, but in perl, 'blessing' an
object by use of the 'bless' operator is a necessary part of
creating any OO type.
> 3. Constructor (if any) runs to completion
>
>
> Suppose there really was an "Object Creator" that did something.
> Removing the silly chant(*), what would it actually do?
In a language with introspection, explicit acknowledgement of shared
libs, or explicit acknowledgement of symbolic debugging, I could
imagine many uses. But C++ was designed to required only a minimal
runtime - an "Object Creator" to me impiles a feature-rich
runtime.
> The object
> already has some properly-aligned memory, and we know that it isn't
> yet initialized... Should objects be registered somehow? No,
> because such a registry would slow things down to no net benefit
> at all (what would we do with the registry?). Basically, an object
> becomes an object when we say, "don't use this memory for anything
> else." That's the same thing as allocating memory.
>
> (*) The reference to Bjarne Stroustrup was meant to be silly, in order
> to demonstrate my point that there is no need for any sort of
> Object Creator (except for the memory allocation). No offense was
> meant towards Bjarne Stroustrup, or Thomas Mang, or anyone else.
[snip]
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: musiphil@bawi.org (KIM Seungbeom)
Date: Mon, 24 Mar 2003 23:29:35 +0000 (UTC) Raw View
francis.glassborow@ntlworld.com (Francis Glassborow) wrote in message news:<lzhIaaDWLvd+Ew+G@robinton.demon.co.uk>...
> In article <bd47bb0e.0303171811.656dbf71@posting.google.com>, KIM
> Seungbeom <musiphil@bawi.org> writes
> >
> >How about POD objects?
> >
> > int* pi = static_cast<int*>(malloc(sizeof(int)));
> >
> >In this case, what is the storage duration of the int object(*pi)?
>
> You have to look in several places, including the C89 Standard and the
> relationship between new style casts and the C-style cast to deduce that
> it has dynamic storage duration.
Sorry I don't understand.
The Technical Corrigendum 2 of the C90 Standard states that
malloc'ed objects have "allocated" storage duration.
And what do the casts have to do with the storage duration of objects?
--
KIM Seungbeom <musiphil@bawi.org>
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: a9804814@unet.univie.ac.at (Thomas Mang)
Date: Thu, 20 Mar 2003 18:15:20 +0000 (UTC) Raw View
Stefan Ram schrieb:
> a9804814@unet.univie.ac.at (Thomas Mang) writes:
> >Hmm, my interpretation of 3.8/1 is quite clear that this expression
> >"malloc(sizeof(int))" has just created an / many objects. 3.8/1 does not mention
>
> The /evaluation/ of an expression may have an effect, not
> the expression by itself.
>
> >anything to what type the pointer to the raw memory block is casted afterwards; all
> >requirements for lifetime-begin are met - so I'd say the object lives now.
>
> The value malloc(4) does not specify an object, it might specify
> a begin of a region of storage but not an end, i.e., its size.
> The size would be given by the type, but there is no type, one
> can not dereference void *. 1.8, p1 says "An object has a type",
> so if it doesn't have a type, it's not an object.
All true, but in my eyes, still not a contradiction to 3.8/1. As I said in my first
post, allocating memory creates one / many objects with different types.
Think about a union, where many objects share the same memory. Members of a union may
not have a constructor; so basically the objects are created after the memory is
aquired. It's up to the user to keep clear what object is currently active.
And I think the same applies here too: Memory allocation creates one / many objects, all
with indeterminate initial value (yes, they even are initialized !), they die when the
memory is deleted later. These objects share the same memory, and have different types
(all types for which the size and alignment is correct). The cast later only "extracts"
a given type.
If it was the intent of the standard to delay creation of objects until a certain type
is explicitly provided by the user, then they shall add this as third requirement for
object lifetime begin.
regards,
Thomas
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: francis.glassborow@ntlworld.com (Francis Glassborow)
Date: Thu, 20 Mar 2003 21:21:40 +0000 (UTC) Raw View
In article <7f2735a5.0303191645.69a02ffd@posting.google.com>, Allan W
<allan_w@my-dejanews.com> writes
>Basically, an object
>becomes an object when we say, "don't use this memory for anything
>else." That's the same thing as allocating memory.
Exactly, and until we actually say what type of object we are going to
use it for it is just unformed memory that our program now controls.
And memory can be part of more than one object:-)
--
ACCU Spring Conference 2003 April 2-5
The Conference you cannot afford to miss
Check the details: http://www.accuconference.co.uk/
Francis Glassborow ACCU
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: allan_w@my-dejanews.com (Allan W)
Date: Wed, 19 Mar 2003 19:52:31 +0000 (UTC) Raw View
musiphil@bawi.org (KIM Seungbeom) wrote
> The C++ Standard states in 3.7 that there are three kinds of storage
> durations - static, automatic, and dynamic. It also associates the
> dynamic storage duration with objects created with operator new.
>
> Then, what is the storage duration of objects created with malloc()?
>
> (A) It has dynamic storage duration.
> - contradicts with 3.7/2 saying that the dynamic storage duration
> is associated with objects created with operator new.
>
> (B) It has allocated storage duration, as specified in the C Standard.
> - contradicts with 3.7/1 saying that the storage duration is one
> of static, automatic, and dynamic.
>
> (C) It has another (unknown) storage duration.
> - contradicts with 3.7/1 saying that the storage duration is one
> of static, automatic, and dynamic.
>
> What is the definitive answer?
bool isDynamic (int*);
bool isStatic (int*);
bool isAutomatic(int*);
bool isOther (int*);
int main() {
int *z = malloc(sizeof(int));
if (isDynamic (z)) std::cout << "Dynamic";
else if (isStatic (z)) std::cout << "Static";
else if (isAutomatic(z)) std::cout << "Automatic";
else if (isOther (z)) std::cout << "Other";
else std::cout << "I see your point -- "
"how can a program possibly operate "
"without knowing the storage duration of *z?";
std::cout << std::endl;
}
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: francis.glassborow@ntlworld.com (Francis Glassborow)
Date: Wed, 19 Mar 2003 19:53:47 +0000 (UTC) Raw View
In article <bd47bb0e.0303171811.656dbf71@posting.google.com>, KIM
Seungbeom <musiphil@bawi.org> writes
>francis.glassborow@ntlworld.com (Francis Glassborow) wrote in message news:<$VEuDPBD0Zd+Ew+Z@robinton.demon.co.uk>...
>>
>> Please give an example of creating an object (rather than acquiring a
>> block of memory) dynamically without using a new expression. I think it
>> is important to look at the wording carefully. The relevant text seems
>> to be worded in such a way that dynamic storage can be acquired through
>> other mechanisms but the only way that I know to use that memory for a
>> C++ object is via a new expression. For backward compatibility C
>> mechanisms will work but they are provided by the inclusion of C89 as a
>> base document.
>
>How about POD objects?
>
> int* pi = static_cast<int*>(malloc(sizeof(int)));
>
>In this case, what is the storage duration of the int object(*pi)?
You have to look in several places, including the C89 Standard and the
relationship between new style casts and the C-style cast to deduce that
it has dynamic storage duration.
--
ACCU Spring Conference 2003 April 2-5
The Conference you cannot afford to miss
Check the details: http://www.accuconference.co.uk/
Francis Glassborow ACCU
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: a9804814@unet.univie.ac.at (Thomas Mang)
Date: Wed, 19 Mar 2003 19:55:52 +0000 (UTC) Raw View
Anthony Williams schrieb:
> musiphil@bawi.org (KIM Seungbeom) writes:
>
> > The C++ Standard states in 3.7 that there are three kinds of storage
> > durations - static, automatic, and dynamic. It also associates the
> > dynamic storage duration with objects created with operator new.
> >
> > Then, what is the storage duration of objects created with malloc()?
> >
> > (A) It has dynamic storage duration.
> > - contradicts with 3.7/2 saying that the dynamic storage duration
> > is associated with objects created with operator new.
> >
> > (B) It has allocated storage duration, as specified in the C Standard.
> > - contradicts with 3.7/1 saying that the storage duration is one
> > of static, automatic, and dynamic.
> >
> > (C) It has another (unknown) storage duration.
> > - contradicts with 3.7/1 saying that the storage duration is one
> > of static, automatic, and dynamic.
> >
> > What is the definitive answer?
>
> malloc doesn't create objects, it just allocates memory. To create an object
> in the allocated memory you have to use placement new. The use of placement
> new is a new-expression, and entities created with a new-expression has
> dynamic storage duration.
Well, actually malloc "creates" objects:
3.8/1:
The lifetime of an object of type T begins when:
-- storage with the proper alignment and size for type T is obtained, and
-- if T is a class type with a non-trivial constructor, the constructor call has
completed.
This paragraph really fascinates me because it completely hacks the strong type
system. If you allocate memory, you have basically just "created" one / many
objects of any type meeting alignment and size and having a trivial constructor
(e.g. on a typical 32 bit -machine without alignment-requirements, 8 newly
allocated bytes may be 8 signed chars, 8 unsigned chars, 2 short and 2 unsigned
chars and 2 signed chars, one double, 2 float, one int and one signed char and
one short and one bool, or..........not infinite, but many possibilities); they
have an "indeterminate initial value" (8.5 / 9). Only a later cast determines
what it really is / how it is treated.
regards,
Thomas
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: francis.glassborow@ntlworld.com (Francis Glassborow)
Date: Wed, 19 Mar 2003 22:21:01 +0000 (UTC) Raw View
In article <3E77A41A.1DC33972@unet.univie.ac.at>, Thomas Mang
<a9804814@unet.univie.ac.at> writes
>Well, actually malloc "creates" objects:
>
>3.8/1:
>
>The lifetime of an object of type T begins when:
>
>-- storage with the proper alignment and size for type T is obtained, and
>-- if T is a class type with a non-trivial constructor, the constructor call has
>completed.
I am not convinced. Until you decide what object you are creating you
simply have a block of memory whose address is held in a void*.
I think we have to distinguish between
malloc(sizeof(int));
and
(int*) malloc(sizeof(int));
And even then we better be very careful about using the 'object'
--
ACCU Spring Conference 2003 April 2-5
The Conference you cannot afford to miss
Check the details: http://www.accuconference.co.uk/
Francis Glassborow ACCU
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: a9804814@unet.univie.ac.at (Thomas Mang)
Date: Thu, 20 Mar 2003 02:20:13 +0000 (UTC) Raw View
Francis Glassborow schrieb:
> In article <3E77A41A.1DC33972@unet.univie.ac.at>, Thomas Mang
> <a9804814@unet.univie.ac.at> writes
> >Well, actually malloc "creates" objects:
> >
> >3.8/1:
> >
> >The lifetime of an object of type T begins when:
> >
> >-- storage with the proper alignment and size for type T is obtained, and
> >-- if T is a class type with a non-trivial constructor, the constructor call has
> >completed.
>
> I am not convinced. Until you decide what object you are creating you
> simply have a block of memory whose address is held in a void*.
>
> I think we have to distinguish between
>
> malloc(sizeof(int));
>
> and
>
> (int*) malloc(sizeof(int));
Hmm, my interpretation of 3.8/1 is quite clear that this expression
"malloc(sizeof(int))" has just created an / many objects. 3.8/1 does not mention
anything to what type the pointer to the raw memory block is casted afterwards; all
requirements for lifetime-begin are met - so I'd say the object lives now.
A Defect ?
regards,
Thomas
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: allan_w@my-dejanews.com (Allan W)
Date: Thu, 20 Mar 2003 02:21:03 +0000 (UTC) Raw View
a9804814@unet.univie.ac.at (Thomas Mang) wrote
> 3.8/1:
>
> The lifetime of an object of type T begins when:
>
> -- storage with the proper alignment and size for type T is obtained, and
> -- if T is a class type with a non-trivial constructor, the constructor
> call has completed.
>
>
>
> This paragraph really fascinates me because it completely hacks
> the strong type system. If you allocate memory, you have basically
> just "created" one / many objects of any type meeting alignment
> and size and having a trivial constructor (e.g. on a typical
> 32 bit -machine without alignment-requirements, 8 newly
> allocated bytes may be 8 signed chars, 8 unsigned chars, 2 short
> and 2 unsigned chars and 2 signed chars, one double, 2 float, one
> int and one signed char and one short and one bool, or..........
> not infinite, but many possibilities); they have an
> "indeterminate initial value" (8.5 / 9). Only a later cast
> determines what it really is / how it is treated.
C++ is a multi-paradigm language supporting (among other things)
object-oriented programming. But even the highest-level, most
"pure" design must eventually run on a real computer with real RAM
(limited, even on "virtual memory" systems).
In short, the paragraph that offends you says that creating an object
involves acquiring memory and running any automatic initialization.
But is there any other possible definition?
Alternative (hypothetical) new definition for 3.8/1:
The lifetime of an object of type T begins when all of the following
have occurred in order:
1. Allocate memory with proper alignment, and
2. The Object Creator waves it's magic wand and completes (physically
or virtually) the magic chant: "O greate Bjarne, bless this Object
that it may serve us all, without exception" -- which magically
transforms the raw memory into an object (although the object has
not yet been initialized), and
3. Constructor (if any) runs to completion
Suppose there really was an "Object Creator" that did something.
Removing the silly chant(*), what would it actually do? The object
already has some properly-aligned memory, and we know that it isn't
yet initialized... Should objects be registered somehow? No,
because such a registry would slow things down to no net benefit
at all (what would we do with the registry?). Basically, an object
becomes an object when we say, "don't use this memory for anything
else." That's the same thing as allocating memory.
(*) The reference to Bjarne Stroustrup was meant to be silly, in order
to demonstrate my point that there is no need for any sort of
Object Creator (except for the memory allocation). No offense was
meant towards Bjarne Stroustrup, or Thomas Mang, or anyone else.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: musiphil@bawi.org (KIM Seungbeom)
Date: Mon, 17 Mar 2003 04:47:54 +0000 (UTC) Raw View
The C++ Standard states in 3.7 that there are three kinds of storage
durations - static, automatic, and dynamic. It also associates the
dynamic storage duration with objects created with operator new.
Then, what is the storage duration of objects created with malloc()?
(A) It has dynamic storage duration.
- contradicts with 3.7/2 saying that the dynamic storage duration
is associated with objects created with operator new.
(B) It has allocated storage duration, as specified in the C Standard.
- contradicts with 3.7/1 saying that the storage duration is one
of static, automatic, and dynamic.
(C) It has another (unknown) storage duration.
- contradicts with 3.7/1 saying that the storage duration is one
of static, automatic, and dynamic.
What is the definitive answer?
--
KIM Seungbeom <musiphil@bawi.org>
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: anthony.williamsNOSPAM@anthonyw.cjb.net (Anthony Williams)
Date: Mon, 17 Mar 2003 16:57:22 +0000 (UTC) Raw View
musiphil@bawi.org (KIM Seungbeom) writes:
> The C++ Standard states in 3.7 that there are three kinds of storage
> durations - static, automatic, and dynamic. It also associates the
> dynamic storage duration with objects created with operator new.
>
> Then, what is the storage duration of objects created with malloc()?
>
> (A) It has dynamic storage duration.
> - contradicts with 3.7/2 saying that the dynamic storage duration
> is associated with objects created with operator new.
>
> (B) It has allocated storage duration, as specified in the C Standard.
> - contradicts with 3.7/1 saying that the storage duration is one
> of static, automatic, and dynamic.
>
> (C) It has another (unknown) storage duration.
> - contradicts with 3.7/1 saying that the storage duration is one
> of static, automatic, and dynamic.
>
> What is the definitive answer?
malloc doesn't create objects, it just allocates memory. To create an object
in the allocated memory you have to use placement new. The use of placement
new is a new-expression, and entities created with a new-expression has
dynamic storage duration.
HTH
Anthony
--
Anthony Williams
Senior Software Engineer, Beran Instruments Ltd.
Remove NOSPAM when replying, for timely response.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: bob.news@gmx.net ("Robert Klemme")
Date: Mon, 17 Mar 2003 16:57:39 +0000 (UTC) Raw View
"KIM Seungbeom" <musiphil@bawi.org> schrieb im Newsbeitrag
news:bd47bb0e.0303162017.f677b4a@posting.google.com...
> The C++ Standard states in 3.7 that there are three kinds of storage
> durations - static, automatic, and dynamic. It also associates the
> dynamic storage duration with objects created with operator new.
>
> Then, what is the storage duration of objects created with malloc()?
First of all, malloc() does not create objects. It just returns a pointer
to a chunk of memory that you are allowed to use as you see fit and that
has at least the size you requested. In case this fails other things
might happen.
> (A) It has dynamic storage duration.
> - contradicts with 3.7/2 saying that the dynamic storage duration
> is associated with objects created with operator new.
>
> (B) It has allocated storage duration, as specified in the C Standard.
> - contradicts with 3.7/1 saying that the storage duration is one
> of static, automatic, and dynamic.
>
> (C) It has another (unknown) storage duration.
> - contradicts with 3.7/1 saying that the storage duration is one
> of static, automatic, and dynamic.
>
> What is the definitive answer?
I guess from the standard's point of view malloc() is just a function that
returns a pointer. If that pointer points to freshly allocated memory,
the implementation of malloc has to use one of the mechanisms you
enumerate. So it depends on the implementation of malloc.
Regards
robert
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: stephen.clamage@sun.com (Steve Clamage)
Date: Mon, 17 Mar 2003 16:57:57 +0000 (UTC) Raw View
Section 1.2 says the C standard applies where not contradicted
by the C++ standard. The C90 standard does not use the term
"dynamic storage duration", but says in its section 7.10.3.3
that space allocated by malloc (or calloc or realloc) lasts
until it is explicitly freed.
--
Steve Clamage, stephen.clamage@sun.com
> Date: Mon, 17 Mar 2003 04:47:54 +0000 (UTC)
> From: KIM Seungbeom <musiphil@bawi.org>
> Newsgroups: comp.std.c++
> Subject: Storage duration of objects created with malloc()
>
> The C++ Standard states in 3.7 that there are three kinds of storage
> durations - static, automatic, and dynamic. It also associates the
> dynamic storage duration with objects created with operator new.
>
> Then, what is the storage duration of objects created with malloc()?
>
> (A) It has dynamic storage duration.
> - contradicts with 3.7/2 saying that the dynamic storage duration
> is associated with objects created with operator new.
>
> (B) It has allocated storage duration, as specified in the C Standard.
> - contradicts with 3.7/1 saying that the storage duration is one
> of static, automatic, and dynamic.
>
> (C) It has another (unknown) storage duration.
> - contradicts with 3.7/1 saying that the storage duration is one
> of static, automatic, and dynamic.
>
> What is the definitive answer?
>
>
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: francis.glassborow@ntlworld.com (Francis Glassborow)
Date: Mon, 17 Mar 2003 21:31:40 +0000 (UTC) Raw View
In article <bd47bb0e.0303162017.f677b4a@posting.google.com>, KIM
Seungbeom <musiphil@bawi.org> writes
>The C++ Standard states in 3.7 that there are three kinds of storage
>durations - static, automatic, and dynamic. It also associates the
>dynamic storage duration with objects created with operator new.
>
>Then, what is the storage duration of objects created with malloc()?
>
>(A) It has dynamic storage duration.
> - contradicts with 3.7/2 saying that the dynamic storage duration
> is associated with objects created with operator new.
>
>(B) It has allocated storage duration, as specified in the C Standard.
> - contradicts with 3.7/1 saying that the storage duration is one
> of static, automatic, and dynamic.
>
>(C) It has another (unknown) storage duration.
> - contradicts with 3.7/1 saying that the storage duration is one
> of static, automatic, and dynamic.
>
>What is the definitive answer?
Please give an example of creating an object (rather than acquiring a
block of memory) dynamically without using a new expression. I think it
is important to look at the wording carefully. The relevant text seems
to be worded in such a way that dynamic storage can be acquired through
other mechanisms but the only way that I know to use that memory for a
C++ object is via a new expression. For backward compatibility C
mechanisms will work but they are provided by the inclusion of C89 as a
base document.
--
ACCU Spring Conference 2003 April 2-5
The Conference you cannot afford to miss
Check the details: http://www.accuconference.co.uk/
Francis Glassborow ACCU
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: musiphil@bawi.org (KIM Seungbeom)
Date: Tue, 18 Mar 2003 05:21:18 +0000 (UTC) Raw View
francis.glassborow@ntlworld.com (Francis Glassborow) wrote in message news:<$VEuDPBD0Zd+Ew+Z@robinton.demon.co.uk>...
>
> Please give an example of creating an object (rather than acquiring a
> block of memory) dynamically without using a new expression. I think it
> is important to look at the wording carefully. The relevant text seems
> to be worded in such a way that dynamic storage can be acquired through
> other mechanisms but the only way that I know to use that memory for a
> C++ object is via a new expression. For backward compatibility C
> mechanisms will work but they are provided by the inclusion of C89 as a
> base document.
How about POD objects?
int* pi = static_cast<int*>(malloc(sizeof(int)));
In this case, what is the storage duration of the int object(*pi)?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]