473,662 Members | 2,666 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 3396
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)+si zeof(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)+si zeof(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)+si zeof(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)+si zeof(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
3130
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 type may vary, as outlined below. So I don't understand why
3
21026
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 the difference between a pointer and an array). This pointer in effect is a pointer to a void pointer: void**. But iirc, this is not entirely valid and may become a problem when dereferencing and doing pointer arithmetic. And in all honesty,...
22
12726
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 exactly this, and similarly generic functions seem to have useful applications. In a few posts I read, it was suggested to avoid void pointer arithmetic by casting to a char pointer, and performing arithmetic on that (in multiples of...
2
1723
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 them out? in linux\mm\Slab.c, typedef struct slab_s { struct list_head list; unsigned long colouroff;
188
17316
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
26
7746
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 AFAIK, there is nothing to worry about is there? I'm using VC 7.1 Why are such assignments being treated as errors?
27
8950
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 advance. Erik
14
3012
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 friend's machine gives it as zero. Have anyone tried this ? I believe it should give an error because i think there is nothing called void. Regards, arun..
6
13710
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
5607
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
8345
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8857
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8768
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8547
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6186
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5655
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4181
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1999
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.