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 > Re: type-punnin...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 16 of 16 Topic 26091 of 26977
Post > Topic >>

Re: type-punning?

by Ben Bacarisse <ben.usenet@[EMAIL PROTECTED] > May 6, 2008 at 10:17 PM

j.j.fishbat@[EMAIL PROTECTED]
 writes:

> Is there really no way at all to modify a generic
> pointer by passing a reference to it to a function?

Two people have said "no" here and that is because they have, quote
reasonably, taken this phrase in the context of your previous example.
However, I think you can do what you ask, provided you stick to the
narrowest interpretation of the quote above.  (If not, I will get shot
down by the experts and we will both be wiser.)

What you can't do is leave the safety guarantees provided by the void
* type.  So, you /can/ do this (example only, I don't recommend it!):

  int my_realloc(void **ptr, size_t sz)
  {
      void *newp = realloc(*ptr, sz);
      if (newp == NULL) {
          free(*ptr);
          return 0;
      }
      *ptr = newp;
      return 1;
  }


later...

  char *sp = malloc(20);
  void *tmp;
  ...
  tmp = sp;
  if (!my_realloc(&tmp, 30))
      printf("Failed!\n");
  else sp = tmp;
  ...

and you can do the same even for the types that do not explicitly use
the same representation: int *, or struct pointers, etc.  The
assignments to a void * object make it work in a ****table way.

This may not be close enough to what you want to be of any use, but I
hope it clarifies some things.

-- 
Ben.
 




 16 Posts in Topic:
type-punning?
j.j.fishbat@[EMAIL PROTEC  2008-05-06 08:46:00 
Re: type-punning?
vippstar@[EMAIL PROTECTED  2008-05-06 09:43:53 
Re: type-punning?
Harald van =?UTF-8?b?RMSz  2008-05-06 19:35:00 
Re: type-punning?
Andrey Tarasevich <and  2008-05-06 11:05:25 
Re: type-punning?
lawrence.jones@[EMAIL PRO  2008-05-06 14:39:47 
Re: type-punning?
j.j.fishbat@[EMAIL PROTEC  2008-05-06 11:17:38 
Re: type-punning?
Andrey Tarasevich <and  2008-05-06 11:24:55 
Re: type-punning?
Harald van =?UTF-8?b?RMSz  2008-05-06 20:32:01 
Re: type-punning?
Kenneth Brody <kenbrod  2008-05-06 14:34:57 
Re: type-punning?
Harald van =?UTF-8?b?RMSz  2008-05-06 20:50:08 
Re: type-punning?
j.j.fishbat@[EMAIL PROTEC  2008-05-06 11:56:36 
Re: type-punning?
Kenneth Brody <kenbrod  2008-05-06 16:26:19 
Re: type-punning?
Kaz Kylheku <kkylheku@  2008-05-06 14:02:24 
Re: type-punning?
Harald van =?UTF-8?b?RMSz  2008-05-06 23:11:51 
Re: type-punning?
"Default User"   2008-05-06 22:49:07 
Re: type-punning?
Ben Bacarisse <ben.use  2008-05-06 22:17:42 

Post A Reply:
  Go here to Signup

AddThis Feed Button


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

Contact
tan12V112 Sat Jul 26 3:55:26 CDT 2008.