Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Please help me find out the relative topics in C99 for Arithmetic on void and function pointers
Hi, all.

As far as I know, the speical arithmetic operator on void pointer is an
externsion in GNU CC. However, I could not find the relative topics in C99.
Would someone like to help me find them out?

in linux\mm\Slab.c,
typedef struct slab_s {
struct list_head list;
unsigned long  colouroff;
void   *s_mem;  /* including colour offset */
unsigned int  inuse;  /* num of objs active in slab */
kmem_bufctl_t  free;
} slab_t;

static inline void kmem_cache_init_objs (kmem_cache_t * cachep,
slab_t * slabp, unsigned long ctor_flags)
{
int i;

for (i = 0; i < cachep->num; i++) {
void* objp = slabp->s_mem+cachep->objsize*i; // it's one of the
externsions to the C Language Family in GNU CC
...
}

Arithmetic on void- and function-pointers
----------------------------------------------------------------------------
----
In GNU C, addition and subtraction operations are supported on pointers to
void and on pointers to functions. This is done by treating the size of a
void or of a function as 1.
A consequence of this is that sizeof is also allowed on void and on function
types, and returns 1. The option, ¡®-Wpointer-arith¡¯, requests a warning if
these extensions are used.

Thanks,

Best Regards,

Xiangliang Meng



Report this thread to moderator Post Follow-up to this message
Old Post
Xiangliang Meng
07-22-04 01:55 PM


Re: Please help me find out the relative topics in C99 for Arithmetic on void and function pointers
"Xiangliang Meng" <xiangliang_meng@hotmail.com> wrote:

> As far as I know, the speical arithmetic operator on void pointer is an
> externsion in GNU CC.

Yes.

> However, I could not find the relative topics in C99.

6.2.5#18: void is an incomplete type; and 6.5.6#2 (for additive
operators) ... or one operand shall be a pointer to an object type.

Since a void * is a pointer to an incomplete type, not to an object
type, and since addition requires a pointer to an object type, addition
to void *s is a constraint violation.
Ditto for subtraction, which can work on either one or two pointers, but
they, too, must all be pointers to an object type, so void * does not
qualify.

Also note 6.5.3.4#1: the sizeof operator shall not be applied to ... an
incomplete type. If you have a void *vp, then sizeof vp is allowed
(that's the size of the pointer _itself_, probably 4 or 8, but could be
anything); but sizeof *vp is not - that would be the size of a void, and
this is a violation of the constraints in 6.5.3.4.

Conclusion: in ISO C, no arithmetic is allowed on pointers to void.

Richard

Report this thread to moderator Post Follow-up to this message
Old Post
Richard Bos
07-22-04 01:55 PM


Re: Please help me find out the relative topics in C99 for Arithmetic on void and function pointers
"Xiangliang Meng" <xiangliang_meng@hotmail.com> writes:
> Hi, all.
>
> As far as I know, the speical arithmetic operator on void pointer is an
> externsion in GNU CC. However, I could not find the relative topics in C99
.
> Would someone like to help me find them out?
>
> in linux\mm\Slab.c,

<OT>Backslashes?  Are you sure it's not linux/mm/Slab.c?</OT>

> typedef struct slab_s {
>  struct list_head list;
>  unsigned long  colouroff;
>  void   *s_mem;  /* including colour offset */
>  unsigned int  inuse;  /* num of objs active in slab */
>  kmem_bufctl_t  free;
> } slab_t;
>
> static inline void kmem_cache_init_objs (kmem_cache_t * cachep,
>    slab_t * slabp, unsigned long ctor_flags)
> {
>  int i;
>
>  for (i = 0; i < cachep->num; i++) {
>   void* objp = slabp->s_mem+cachep->objsize*i; // it's one of the
> externsions to the C Language Family in GNU CC
> ...
> }

Shouldn't that *i be +i?

As Richard Bos pointed out, arithmetic on void pointers and function
pointers is allowed in GNU C, but not in C99.  If you want to perform
arithmetic on a void pointer, cast it to char*:

void *p = whatever;
void *q;
q = p + 42;                /* GNU C */
q = (void*)((char*)p + 24) /* C99 (also valid GNU C) */

(If you want to perform arithmetic on a function pointer, you're doing
something exceedingly odd.)

--
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center             <*>  <http://users.sdsc.edu/~kst>
We must do something.  This is something.  Therefore, we must do this.

Report this thread to moderator Post Follow-up to this message
Old Post
Keith Thompson
07-23-04 01:55 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

C archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 09:13 PM.

 

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.