473,408 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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
Nov 14 '05 #1
2 1705
"Xiangliang Meng" <xi*************@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
Nov 14 '05 #2
"Xiangliang Meng" <xi*************@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) ks***@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.
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: wired | last post by:
Hi, I've just taught myself C++, so I haven't learnt much about style or the like from any single source, and I'm quite styleless as a result. But at the same time, I really want nice code and I...
1
by: Az Tech | last post by:
Hi people, (Sorry for the somewhat long post). I request some of the people on this group who have good experience using object-orientation in the field, to please give some good ideas for...
5
by: Sona | last post by:
I understand the problem I'm having but am not sure how to fix it. My code passes two char* to a function which reads in some strings from a file and copies the contents into the two char*s. Now...
22
by: Alex Fraser | last post by:
From searching Google Groups, I understand that void pointer arithmetic is a constraint violation, which is understandable. However, generic functions like qsort() and bsearch() must in essence do...
1
by: Pieter Claassen | last post by:
Ok, I have something that works, but I don't understand why. I use libpcap to get data of the wire and then initially I casted the packet to ethernet, then marched along the memory in chunks of...
6
by: Francois Grieu | last post by:
Are these programs correct ? #include <stdio.h> unsigned char a = {1,2}; int main(void) { unsigned char j; for(j=1; j<=2; ++j) printf("%u\n", *( a+j-1 )); return 0; }
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
21
by: codergem | last post by:
One common answer is that all compilers keep the size of integer the same as the size of the register on a particular architecture. Thus, to know whether the machine is 32 bit or 64 bit, just see...
9
by: xiao | last post by:
It always dumped when I tried to run it... But it compiles OK. What I want to do is to do a test: Read information from a .dat file and then write it to another file. The original DAT file is...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.