473,506 Members | 16,201 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

(void) pointer arithmetic

Hi all,

if I have:

void *vptr= malloc( ENOUGH );

is this guaranteed to always be true:

( (sometype*)vptr ) + 1 == vptr + sizeof(sometype)

it fails of course if sometype is void, and gcc sometimes screams
about void pointer arithmetic.

Also, is sizeof(char) guaranteed to always be 1? How does this
interact with CHAR_BIT?

Also, are

sizeof(unsigned sometype) == sizeof(sometype)
sizeof(unsigned sometype) == sizeof(signed sometype)

always guaranteed to be true for types which can be modified by signed
and unsigned?

On a similar track, what about:

( (unsigned sometype*)vptr ) + 1 == ( (sometype*)vptr ) + 1
( (unsigned sometype*)vptr ) + 1 == ( (signed sometype*)vptr ) + 1

Thanks,
viza

Jun 27 '08 #1
5 3382
viza wrote:
if I have:

void *vptr= malloc( ENOUGH );

is this guaranteed to always be true:

( (sometype*)vptr ) + 1 == vptr + sizeof(sometype)
It's not legal C, since you can't do arithmetic on void*
values in standard C.
it fails of course if sometype is void, and gcc sometimes screams
about void pointer arithmetic.
gcc defaults to allowing void* arithmetic, but can be disuaded
with suitable command-line options.
Also, is sizeof(char) guaranteed to always be 1?
Yes.
How does this interact with CHAR_BIT?
It doesn't. 1 is 1 and all alone and ever more shall be so.

--
"It would have to be enough." /Brokedown Palace/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Jun 27 '08 #2
On Jun 10, 5:17 pm, viza <tom.v...@gmail.comwrote:
Hi all,

if I have:

void *vptr= malloc( ENOUGH );

is this guaranteed to always be true:

( (sometype*)vptr ) + 1 == vptr + sizeof(sometype)
Don't you mean
((type*)ptr)+1 == ((char*)ptr)+sizeof(type)
In that case, yes, it is guaranteed.
>
it fails of course if sometype is void, and gcc sometimes screams
about void pointer arithmetic.
void pointer arithmetic is not allowed by ISO C90/C99. (and ANSI C89)
However, gcc allows it as an extension, (it will *error* in c89/c99
mode)
Also, is sizeof(char) guaranteed to always be 1? How does this
interact with CHAR_BIT?
It does not. sizeof(char) is guaranteed to be 1 and CHAR_BIT at least
8.
Also, are

sizeof(unsigned sometype) == sizeof(sometype)
sizeof(unsigned sometype) == sizeof(signed sometype)

always guaranteed to be true for types which can be modified by signed
and unsigned?
Types are not modified by signed and unsigned, but yes, it is
guaranteed.
>
On a similar track, what about:

( (unsigned sometype*)vptr ) + 1 == ( (sometype*)vptr ) + 1
( (unsigned sometype*)vptr ) + 1 == ( (signed sometype*)vptr ) + 1
Yes
Jun 27 '08 #3
vi******@gmail.com writes:
On Jun 10, 5:17 pm, viza <tom.v...@gmail.comwrote:
>if I have:

void *vptr= malloc( ENOUGH );

is this guaranteed to always be true:

( (sometype*)vptr ) + 1 == vptr + sizeof(sometype)
Don't you mean
((type*)ptr)+1 == ((char*)ptr)+sizeof(type)
In that case, yes, it is guaranteed.
If *you* mean:

(void *)((type *)ptr + 1) == (void *)((char *)ptr + sizeof(type))

then yes. In your example the two pointer types can't be compared
(unless type is a character type).

--
Ben.
Jun 27 '08 #4
On Jun 10, 7:33 pm, vipps...@gmail.com wrote:
On Jun 10, 5:17 pm, viza <tom.v...@gmail.comwrote:Hi all,
if I have:
void *vptr= malloc( ENOUGH );
is this guaranteed to always be true:
( (sometype*)vptr ) + 1 == vptr + sizeof(sometype)

Don't you mean
((type*)ptr)+1 == ((char*)ptr)+sizeof(type)
In that case, yes, it is guaranteed.
it fails of course if sometype is void, and gcc sometimes screams
about void pointer arithmetic.

void pointer arithmetic is not allowed by ISO C90/C99. (and ANSI C89)
However, gcc allows it as an extension, (it will *error* in c89/c99
mode)Also, is sizeof(char) guaranteed to always be 1? How does this
interact with CHAR_BIT?

It does not. sizeof(char) is guaranteed to be 1 and CHAR_BIT at least
8.Also, are
sizeof(unsigned sometype) == sizeof(sometype)
sizeof(unsigned sometype) == sizeof(signed sometype)
always guaranteed to be true for types which can be modified by signed
and unsigned?

Types are not modified by signed and unsigned, but yes, it is
guaranteed.
On a similar track, what about:
( (unsigned sometype*)vptr ) + 1 == ( (sometype*)vptr ) + 1
( (unsigned sometype*)vptr ) + 1 == ( (signed sometype*)vptr ) + 1

Yes
Correct me if I am wrong:

sizeof(char) is 1 and CHAR_BITS is >= 8. What exactly sizeof returns?
AFAIK it returns number of bytes. So 1 byte in C is sizeof char but
not necesarily this byte has to be 8 bits. Its not that the sizeof
char is defined in terms of bytes but bytes are defined in terms of
sizeof char.
Jun 27 '08 #5
Ben Bacarisse wrote:
vi******@gmail.com writes:
>On Jun 10, 5:17 pm, viza <tom.v...@gmail.comwrote:
>>if I have:

void *vptr= malloc( ENOUGH );

is this guaranteed to always be true:

( (sometype*)vptr ) + 1 == vptr + sizeof(sometype)
>Don't you mean
((type*)ptr)+1 == ((char*)ptr)+sizeof(type)
In that case, yes, it is guaranteed.

If *you* mean:

(void *)((type *)ptr + 1) == (void *)((char *)ptr + sizeof(type))

then yes. In your example the two pointer types can't be compared
(unless type is a character type).
Assuming that vptr is not a null pointer.

--
Thad
Jun 27 '08 #6

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

Similar topics

2
3123
by: Xavier Decoret | last post by:
This is a compiler error that I get when I try to do: void* p; unsigned int stride; // ... p += stride; I have good reason to do this, namely an iterator over an array of object whose...
3
21010
by: Martijn | last post by:
Hi, I am looking over some of my old implementations. One of them has an "array" of void pointers defined as a pointer to the first element (without getting stuck on nomenclature, I am aware of...
22
12706
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...
2
1715
by: Xiangliang Meng | last post by:
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...
188
17198
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
26
7688
by: Alfonso Morra | last post by:
Hi, I'm getting a compiler error of "pointer truncation from 'void *' to 'int'" because I'm not explicitly casting from void* to (datatype*). Pointers are stored as ints (is that right?), so as...
27
8907
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in...
14
2995
by: arun | last post by:
Hi, Why sizeof operator when applied on void returns one when compiled with gcc compiler ??. When i tried it on VC++ compiler, it gives an error. But another version of the VC++ compiler on my...
6
13688
by: Angus | last post by:
I am using a global which is a void* I have it defined in one file as: void* hFlag; and one other header file as: extern void* hFlag; But I get this compile error:
160
5481
by: raphfrk | last post by:
Is this valid? int a; void *b; b = (void *)a; // b points to a b += 5*sizeof(*a); // b points to a a = 100;
0
7220
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
7105
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7371
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5617
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
4702
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...
0
3188
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
410
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.