Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C Moderated > Re: Non-null po...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 39 of 49 Topic 1099 of 1183
Post > Topic >>

Re: Non-null pointers in C/C++?

by James Kuyper <jameskuyper@[EMAIL PROTECTED] > Jun 12, 2008 at 07:32 PM

To: comp.lang.c.moderated moderators: I posted a previous version of=20
this post yesterday, which was missing a single character that rendered=20
it incorrect. It's not yet visible on my news server, in either=20
newsgroup. If it's not too late, please reject that message. Thanks!

[oops, a bit late.  here we go with the fixed one, then - mod]

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. As =
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 differe=
nt
>>>> 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 pointed=
 at.
>>>> How could such an implementation implement malloc2() so it would kno=
w
>>>> which representation to use? It would be relatively straightforward =
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 about
>> malloc(), we were talking about malloc2(). As described above, it take=
s
>> an unnullable pointer argument 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 type
>> 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 size)
>> {
>>      T** temp =3D malloc(size);
>>      if(temp)
>>          temp[0] =3D (T*)t;  // Must be type-aware
>>
>>      return (unnullable T**) temp;
>>
>> }
>=20
> Why not:
> unnullable void** malloc2(unnullable void const* initial_value, size_t
> size)
> {
>      void** new_ptr =3D malloc(size);
>      if(new_ptr !=3D NULL)
>      {
>          *new_ptr =3D initial_value;
>      }
>=20
>      return (unnullable void**)new_ptr;
> }
>=20
> 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
other pointer type. Therefore, if malloc2() is your only proposed
mechanism for allowing the initialization of unnullable pointers in
dynamically allocated memory, then no other unnullable pointer types 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.


>>>> Would it be prohibited for a structure to contain a member of unnull=
ablepointertype? 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
a pointer that occupies the initial bytes of the allocated memory.

....
> -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
{
     int &ri;
     ReferenceHolder(int &i):ri(i){}
};

int main()
{
     int j;
     ReferenceHolder *rj;
     ReferenceHolder *rk;
     rj =3D new ReferenceHolder(j);

     rk =3D static_cast<ReferenceHolder*>(std::malloc(sizeof *rk));
     new(rk) ReferenceHolder(j); // inplace construction

     int retval =3D (&(rj->ri) =3D=3D &(rk->ri)) ? EXIT_SUCCESS : EXIT_FA=
ILURE;

     std::free(rk);
     delete(rj);
     return 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: 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.
 




 49 Posts in Topic:
Non-null pointers in C/C++?
angel_tsankov <fn42551  2008-05-13 01:06:32 
Re: Non-null pointers in C/C++?
Francis Glassborow <fr  2008-05-15 11:00:31 
Re: Non-null pointers in C/C++?
"Wojtek Lerch"   2008-05-22 16:51:34 
Re: Non-null pointers in C/C++?
gordonb.ll93b@[EMAIL PROT  2008-05-23 15:57:23 
Re: Non-null pointers in C/C++?
"Wojtek Lerch"   2008-05-24 18:15:37 
Re: Non-null pointers in C/C++?
angel_tsankov <fn42551  2008-05-22 16:51:43 
Re: Non-null pointers in C/C++?
jameskuyper@[EMAIL PROTEC  2008-05-23 15:57:09 
Re: Non-null pointers in C/C++?
angel_tsankov <fn42551  2008-05-28 13:35:14 
Re: Non-null pointers in C/C++?
James Kuyper <jameskuy  2008-05-29 10:58:00 
Re: Non-null pointers in C/C++?
James Kuyper <jameskuy  2008-06-01 12:35:06 
Re: Non-null pointers in C/C++?
angel_tsankov <fn42551  2008-06-01 12:35:16 
Re: Non-null pointers in C/C++?
James Kuyper <jameskuy  2008-06-02 11:54:23 
Re: Non-null pointers in C/C++?
angel_tsankov <fn42551  2008-06-10 00:56:47 
Re: Non-null pointers in C/C++?
James Kuyper <jameskuy  2008-06-12 19:25:13 
Re: Non-null pointers in C/C++?
angel_tsankov <fn42551  2008-06-14 03:02:07 
Re: Non-null pointers in C/C++?
angel_tsankov <fn42551  2008-06-18 08:45:23 
Re: Non-null pointers in C/C++?
angel_tsankov <fn42551  2008-06-18 08:45:31 
Re: Non-null pointers in C/C++?
angel_tsankov <fn42551  2008-06-28 19:04:48 
Re: Non-null pointers in C/C++?
gordon@[EMAIL PROTECTED]   2008-07-04 01:45:57 
Re: Non-null pointers in C/C++?
"Angel Tsankov"  2008-07-08 21:18:29 
Re: Non-null pointers in C/C++?
gordonb.4ax5t@[EMAIL PROT  2008-07-10 13:02:14 
Re: Non-null pointers in C/C++?
"Angel Tsankov"  2008-07-12 18:10:36 
Re: Non-null pointers in C/C++?
gordonb.4dbn6@[EMAIL PROT  2008-07-13 10:55:33 
Re: Non-null pointers in C/C++?
"Angel Tsankov"  2008-07-20 15:07:13 
Re: Non-null pointers in C/C++?
James Kuyper <jameskuy  2008-07-21 18:28:00 
Re: Non-null pointers in C/C++?
"Angel Tsankov"  2008-07-23 22:51:02 
Re: Non-null pointers in C/C++?
James Kuyper <jameskuy  2008-07-25 12:15:29 
Re: Non-null pointers in C/C++?
"Angel Tsankov"  2008-08-02 12:34:34 
Re: Non-null pointers in C/C++?
James Kuyper <jameskuy  2008-08-07 15:01:26 
Re: Non-null pointers in C/C++?
"Angel Tsankov"  2008-08-11 19:05:20 
Re: Non-null pointers in C/C++?
blargg.h4g@[EMAIL PROTECT  2008-08-14 15:29:10 
Re: Non-null pointers in C/C++?
jameskuyper@[EMAIL PROTEC  2008-08-14 15:29:37 
Re: Non-null pointers in C/C++?
"Angel Tsankov"  2008-08-20 21:30:20 
Re: Non-null pointers in C/C++?
John Nagle <nagle@[EMA  2008-08-14 15:30:30 
Re: Non-null pointers in C/C++?
"Mabden" <Ma  2008-08-09 12:08:24 
Re: Non-null pointers in C/C++?
Jasen Betts <jasen@[EM  2008-07-20 15:07:47 
Re: Non-null pointers in C/C++?
"Angel Tsankov"  2008-07-21 18:28:13 
Re: Non-null pointers in C/C++?
Francis Glassborow <fr  2008-07-04 01:46:17 
Re: Non-null pointers in C/C++?
James Kuyper <jameskuy  2008-06-12 19:32:35 
Re: Non-null pointers in C/C++?
Philip Lantz <prl@[EMA  2008-06-14 03:01:41 
Re: Non-null pointers in C/C++?
=?ISO-8859-1?Q?Hans-Bernh  2008-05-15 11:00:41 
Re: Non-null pointers in C/C++?
"Wojtek Lerch"   2008-05-22 16:51:39 
Re: Non-null pointers in C/C++?
angel_tsankov <fn42551  2008-05-22 16:51:46 
Re: Non-null pointers in C/C++?
Jonathan Leffler <jlef  2008-05-24 18:15:49 
Re: Non-null pointers in C/C++?
gordonb.vehsx@[EMAIL PROT  2008-05-15 11:00:54 
Re: Non-null pointers in C/C++?
Eric Sosman <Eric.Sosm  2008-05-22 16:51:21 
Re: Non-null pointers in C/C++?
angel_tsankov <fn42551  2008-05-22 16:51:53 
Re: Non-null pointers in C/C++?
Matthias Buelow <mkb@[  2008-05-23 15:57:49 
Re: Non-null pointers in C/C++?
Roberto Waltman <usene  2008-06-10 00:57:00 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Nov 21 12:48:39 CST 2008.