On 14 =D0=AE=D0=BD=D0=B8, 11:02, angel_tsankov <fn42...@[EMAIL PROTECTED]
>
w=
rote:
> On 13 =D0=AE=D0=BD=D0=B8, 03:25, James Kuyper <jameskuy...@[EMAIL PROTECTED]
>
w=
rote:
>
>
>
>
>
> > angel_tsankov wrote:
> > > On 2 =D0=AE=D0=BD=D0=B8, 19:54, James Kuyper
<jameskuy...@[EMAIL PROTECTED]
> wrote:
> > >> angel_tsankov wrote:
> > >>> On 29 =C3=AD=C3=81=C3=8A, 18:58, James Kuyper
<jameskuy...@[EMAIL PROTECTED]
> wrote:
> > >>>> angel_tsankov wrote:
> > >>>>> On 23 =C3=AD=C3=81=C3=8A, 23:57, jameskuy...@[EMAIL PROTECTED]
wrote:
> > >> ...
> > >>>>>> It would also have to be constraint violation for an
> > >>>>>> object with an unnullablepointertype to be left uninitialized.
A=
s a
> > >>>>>> result, I don't think it would be feasible to allow the space
> > >>>>>> containing an unnullablepointerobject to be allocated using the
> > >>>>>> malloc() family.
> > >>>>> One could have malloc2(), taking an unnullablepointerthat would
> > >>>>> serve as initilizer for the malloc'ed unnullablepointer.
> > >>>> The C standard currently allows for the possibility that pointers
=
to
> > >>>> different types can have different representations, and even
diffe=
rent
> > >>>> sizes. There are a number of good reasons why many real-world
> > >>>> implementations actually use two or more differentpointer
> > >>>> representations, depending upon the type of the object being
point=
ed at.
> > >>>> How could such an implementation implement malloc2() so it would
k=
now
> > >>>> which representation to use? It would be relatively
straightforwar=
d to
> > >>>> handle that case by making malloc2() a C++ template function, but
=
I
> > >>>> don't see any easy way to do it in C.
> > >>> I do not really get your point here... As far as I know malloc
does
> > >>> not care about types - it only cares about allocating memory of
the
> > >>> specified amount.
> > >> You are perfectly correct about malloc(); but we weren't talking
abo=
ut
> > >> malloc(), we were talking about malloc2(). As described above, it
ta=
kes
> > >> an unnullablepointerargument that is used to initialize the memory
> > >> allocated by malloc2(). Since unnullable pointers could,
presumably,
> > >> have many different types, with correspondingly different sizes and
> > >> different representations, malloc2() could not work without being
ty=
pe
> > >> aware. Here's code for a hypothetical language with C++ - style
> > >> templates and an 'unnullable' keyword:
>
> > >> template <type T> unnullable T** malloc2(unnullable T *t, size_t
siz=
e)
> > >> {
> > >> =C2=A0 =C2=A0 =C2=A0T** temp =3D malloc(size);
> > >> =C2=A0 =C2=A0 =C2=A0if(temp)
> > >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0temp[0] =3D (T*)t; =C2=A0// Must
b=
e type-aware
>
> > >> =C2=A0 =C2=A0 =C2=A0return (unnullable T**) temp;
>
> > >> }
>
> > > Why not:
> > > unnullable void** malloc2(unnullable void const* initial_value,
size_=
t
> > > size)
> > > {
> > > =C2=A0 =C2=A0 =C2=A0void** new_ptr =3D malloc(size);
> > > =C2=A0 =C2=A0 =C2=A0if(new_ptr !=3DNULL)
> > > =C2=A0 =C2=A0 =C2=A0{
> > > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0*new_ptr =3D initial_value;
> > > =C2=A0 =C2=A0 =C2=A0}
>
> > > =C2=A0 =C2=A0 =C2=A0return (unnullable void**)new_ptr;
> > > }
>
> > > If think the above sample implementation of malloc2 should do,
> > > provided that a conversion from T* to void*, and back to T*, always
> > > yields the initial value for any type T (which I think is guaranteed
> > > by the standard).
>
> > Yes, but that implementation only allows the initialization of
> > unnullable void* pointers; it doesn't allow the initialization of any
> > otherpointertype. Therefore, if malloc2() is your only proposed
> > mechanism for allowing the initialization of unnullable pointers in
> > dynamically allocated memory, then no other unnullablepointertypes can
> > reside in such memory. With references in C++, there's no
corresponding
> > problem, because the constructor for any object type containing a
> > reference is responsible for initializing it, and the 'new' operator
> > causes the constructor to be called on the newly allocated memory.
>
> It seems that I do not have any "correct" answer as to how the C
> language (and the C library) can quarantee that a dynamically
> allocated unnullablepointeris properly initialized.
>
> So, for the time being we could restrict the use of "unnullable" to
> qualify pointers of static storage duration and pointers which are
> pointed to. This means that apointerwhose storage duration is not
> known at the point of their declaration (e.g. member pointers) may not
> be unnullable (unless it specifies apointerwhich is pointed to:
> struct s
> {
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 unnullable**p; // This is OK};
>
> --End example).
>
> However, even with these restrictions one can still have dynamically
> allocated unnullable pointers (of which I cannot see any benefit):
>
> unnullable some_type** malloc_some_type(unnullable some_type*
> existing_ptr)
> {
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 some_type** new_ptr =3D
malloc(sizeof(unnulla=
ble some_type*));
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 // If malloc has succeeded, new_ptr is not
of=
type unnullable
> some_type**, since
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 // thepointerthat new_ptr points to may
beNUL=
L
>
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 if (new_ptr !=3DNULL)
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 {
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 *new_ptr =3D
exis=
ting_ptr;
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 }
>
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 return (unnullable some_type**)new_ptr;
>
> }
> > >>>> Would it be prohibited for a structure to contain a member of
unnu=
llablepointertype? If not, how do you tell malloc2() where to insert
thepoi=
nterthat it takes as an argument, when allocating space for such a
> > >>>> structure?
> > >> Have you figured out an answer to that question?
>
> > I gather that you have not, since you didn't bother to answer it. You
> > know, "I don't know" is a perfectly acceptable answer.
> > Your solution, like mine, still suffers from only be able to
initialize
> > apointerthat occupies the initial bytes of the allocated memory.
>
> Yes, I do not have any answer to this question.
>
>
>
>
>
> > ...
>
> > > -as to malloc2, if we cannot have it, then we won't have it (C++
> > > references cannot be allocated on the heap either);
>
> > Not directly, but it can be done indirectly:
>
> > #include <cstdlib>
> > #include <memory>
>
> > struct ReferenceHolder
> > {
> > =C2=A0 =C2=A0 =C2=A0int &ri;
> > =C2=A0 =C2=A0 =C2=A0ReferenceHolder(int i):ri(i){}
>
> > };
>
> > int main()
> > {
> > =C2=A0 =C2=A0 =C2=A0int j;
> > =C2=A0 =C2=A0 =C2=A0ReferenceHolder *rj;
> > =C2=A0 =C2=A0 =C2=A0ReferenceHolder *rk;
> > =C2=A0 =C2=A0 =C2=A0rj =3D new ReferenceHolder(j);
>
> > =C2=A0 =C2=A0 =C2=A0rk =3D
static_cast<ReferenceHolder*>(std::malloc(si=
zeof *rk));
> > =C2=A0 =C2=A0 =C2=A0new(rk) ReferenceHolder(j); // inplace
construction
>
> > =C2=A0 =C2=A0 =C2=A0int retval =3D (&(rj->ri) =3D=3D &(rk->ri)) ?
EXIT_=
SUCCESS : EXIT_FAILURE;
>
> > =C2=A0 =C2=A0 =C2=A0std::free(rk);
> > =C2=A0 =C2=A0 =C2=A0delete(rj);
> > =C2=A0 =C2=A0 =C2=A0return retval;
>
> > }
>
> > This relies upon the existence of constructors in C++. I've no idea
how
> > you could do something similar in C+unnullable.
> > --
> > comp.lang.c.moderated - moderation address: c...@[EMAIL PROTECTED]
-- you
mu=
st
> > have an appropriate newsgroups line in your header for your mail to be
=
seen,
> > or the newsgroup name in square brackets in the subject line.
=C2=A0Sor=
ry.- =D0=A1=D0=BA=D1=80=D0=B8=D0=B2=D0=B0=D0=BD=D0=B5 =D0=BD=D0=B0 =D1=86=
=D0=B8=D1=82=D0=B8=D1=80=D0=B0=D0=BD=D0=B8=D1=8F =D1=82=D0=B5=D0=BA=D1=81=
=D1=82 -
>
> > - =D0=9F=D0=BE=D0=BA=D0=B0=D0=B7=D0=B2=D0=B0=D0=BD=D0=B5 =D0=BD=D0=B0
=
=D1=86=D0=B8=D1=82=D0=B8=D1=80=D0=B0=D0=BD=D0=B8=D1=8F =D1=82=D0=B5=D0=BA=
=D1=81=D1=82 -
>
> --
> comp.lang.c.moderated - moderation address: c...@[EMAIL PROTECTED]
-- you
must
> have an appropriate newsgroups line in your header for your mail to be
se=
en,
> or the newsgroup name in square brackets in the subject line.
=C2=A0Sorry=
..- =D0=A1=D0=BA=D1=80=D0=B8=D0=B2=D0=B0=D0=BD=D0=B5 =D0=BD=D0=B0
=D1=86=D0=
=B8=D1=82=D0=B8=D1=80=D0=B0=D0=BD=D0=B8=D1=8F =D1=82=D0=B5=D0=BA=D1=81=D1=
=82 -
>
> - =D0=9F=D0=BE=D0=BA=D0=B0=D0=B7=D0=B2=D0=B0=D0=BD=D0=B5 =D0=BD=D0=B0
=D1=
=86=D0=B8=D1=82=D0=B8=D1=80=D0=B0=D0=BD=D0=B8=D1=8F =D1=82=D0=B5=D0=BA=D1=
=81=D1=82 -- =D0=A1=D0=BA=D1=80=D0=B8=D0=B2=D0=B0=D0=BD=D0=B5 =D0=BD=D0=B0
=
=D1=86=D0=B8=D1=82=D0=B8=D1=80=D0=B0=D0=BD=D0=B8=D1=8F =D1=82=D0=B5=D0=BA=
=D1=81=D1=82 -
>
> - =D0=9F=D0=BE=D0=BA=D0=B0=D0=B7=D0=B2=D0=B0=D0=BD=D0=B5 =D0=BD=D0=B0
=D1=
=86=D0=B8=D1=82=D0=B8=D1=80=D0=B0=D0=BD=D0=B8=D1=8F =D1=82=D0=B5=D0=BA=D1=
=81=D1=82 -
So, there are no more questions about "unnullable"? If so, how can I
propose the addition of unnullable to the C standard?
--
comp.lang.c.moderated - moderation address: clcm@[EMAIL PROTECTED]
-- you must
have an appropriate newsgroups line in your header for your mail to be
seen,
or the newsgroup name in square brackets in the subject line. Sorry.


|