473,756 Members | 3,390 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

void pointer arithmetic

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 sizeof(original _type)). Thus the pointer is cast through the sequence
original_type* -> void* -> char* -> void* -> original_type*.

I am quite sure this would work on many implementations (where the casts are
no-ops), but the threads I saw containing the above suggestion did not make
clear whether or not it was undefined (or implementation defined)
behaviour - is it? If so, is there any way to achieve the desired effect
while strictly conforming to the standards?

TIA,
Alex
Nov 13 '05
22 12740
Dan Pop wrote:
In <HB************ **@bombur.uio.n o> Hallvard B Furuseth
<h.b.furuseth(n ospam)@usit.uio (nospam).no> writes:


<snip>

If you want to _implement_ qsort, you need to cast to char* and do
pointer arithmetic on the result. If you want to _use_ it, you almost
certainly need not.


Are you reading impaired, or what? He explicitly told you that he was
talking about the *implementation * of qsort and bsearch and, as you
agree, the casts are unavoidable. So, where is he wrong?


A small nit: the /conversions/ are unavoidable. They can, however, be
implicit conversions. They do not need to be casts. All you need is a spare
char * to hold the relevant pointer value. (Or possibly more than one. I
haven't checked.)

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #11
In <bp**********@h ercules.btinter net.com> Richard Heathfield <do******@addre ss.co.uk.invali d> writes:
Dan Pop wrote:
In <HB************ **@bombur.uio.n o> Hallvard B Furuseth
<h.b.furuseth(n ospam)@usit.uio (nospam).no> writes:


<snip>

If you want to _implement_ qsort, you need to cast to char* and do
pointer arithmetic on the result. If you want to _use_ it, you almost
certainly need not.


Are you reading impaired, or what? He explicitly told you that he was
talking about the *implementation * of qsort and bsearch and, as you
agree, the casts are unavoidable. So, where is he wrong?


A small nit: the /conversions/ are unavoidable. They can, however, be
implicit conversions. They do not need to be casts. All you need is a spare
char * to hold the relevant pointer value. (Or possibly more than one. I
haven't checked.)


But that's the point: the casts (which are effectively no-ops in terms of
generated code) avoid the need of temporary pointers that serve no other
purpose than avoiding the casts.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #12
Dan Pop wrote:
In <bp**********@h ercules.btinter net.com> Richard Heathfield
<do******@addre ss.co.uk.invali d> writes:
A small nit: the /conversions/ are unavoidable. They can, however, be
implicit conversions. They do not need to be casts. All you need is a
spare char * to hold the relevant pointer value. (Or possibly more than
one. I haven't checked.)


But that's the point: the casts (which are effectively no-ops in terms of
generated code) avoid the need of temporary pointers that serve no other
purpose than avoiding the casts.


You missed the *primary* purpose of the temporary pointers, which is that
they make the code easier to read.

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #13
Richard Heathfield <do******@addre ss.co.uk.invali d> wrote in message news:<bp******* ***@titan.btint ernet.com>...
Dan Pop wrote:
In <bp**********@h ercules.btinter net.com> Richard Heathfield
<do******@addre ss.co.uk.invali d> writes:
A small nit: the /conversions/ are unavoidable. They can, however, be
implicit conversions. They do not need to be casts. All you need is a
spare char * to hold the relevant pointer value. (Or possibly more than
one. I haven't checked.)


But that's the point: the casts (which are effectively no-ops in terms of
generated code) avoid the need of temporary pointers that serve no other
purpose than avoiding the casts.


You missed the *primary* purpose of the temporary pointers, which is that
they make the code easier to read.


It also makes for a disciplined evaluation of NULL pointers, as in:

MyStruct *my;
void *voidmyptr;

if( my = voidmyptr )
continue ok;
else
return errpunt ("My struct doesn't exist"

SO, I'm with you on this one, 100%. karl m
Nov 13 '05 #14
karl malbrain wrote:
Richard Heathfield <do******@addre ss.co.uk.invali d> wrote in message
news:<bp******* ***@titan.btint ernet.com>...
Dan Pop wrote:
> But that's the point: the casts (which are effectively no-ops in terms
> of generated code) avoid the need of temporary pointers that serve no
> other purpose than avoiding the casts.


You missed the *primary* purpose of the temporary pointers, which is that
they make the code easier to read.


It also makes for a disciplined evaluation of NULL pointers, as in:

MyStruct *my;
void *voidmyptr;

if( my = voidmyptr )
continue ok;
else
return errpunt ("My struct doesn't exist"

SO, I'm with you on this one, 100%. karl m


Nevertheless, I stand by what I said.
--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #15
In <bp**********@t itan.btinternet .com> Richard Heathfield <do******@addre ss.co.uk.invali d> writes:
Dan Pop wrote:
In <bp**********@h ercules.btinter net.com> Richard Heathfield
<do******@addre ss.co.uk.invali d> writes:
A small nit: the /conversions/ are unavoidable. They can, however, be
implicit conversions. They do not need to be casts. All you need is a
spare char * to hold the relevant pointer value. (Or possibly more than
one. I haven't checked.)


But that's the point: the casts (which are effectively no-ops in terms of
generated code) avoid the need of temporary pointers that serve no other
purpose than avoiding the casts.


You missed the *primary* purpose of the temporary pointers, which is that
they make the code easier to read.


That's not automatically true in my experience. It heavily depends on the
context.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #16
Richard Heathfield <do******@addre ss.co.uk.invali d> wrote:
Dan Pop wrote:
But that's the point: the casts (which are effectively no-ops in terms of
generated code) avoid the need of temporary pointers that serve no other
purpose than avoiding the casts.


You missed the *primary* purpose of the temporary pointers, which is that
they make the code easier to read.


Says you. In some often-occurring cases (e.g., qsort()ing strings using
a wrapper for strcpy()), the cast version is, IMO, easier to read than
the extra-object version.

Richard
Nov 13 '05 #17


Richard Bos wrote:
Richard Heathfield <do******@addre ss.co.uk.invali d> wrote:

Dan Pop wrote:

But that's the point: the casts (which are effectively no-ops in terms of
generated code) avoid the need of temporary pointers that serve no other
purpose than avoiding the casts.


You missed the *primary* purpose of the temporary pointers, which is that
they make the code easier to read.

Says you. In some often-occurring cases (e.g., qsort()ing strings using
a wrapper for strcpy()), the cast version is, IMO, easier to read than
the extra-object version.


What is this often-occuring case (qsort()ing strings using a
wrapper for strcpy()?

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapi dsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 13 '05 #18
In <bp************ *@ID-169908.news.uni-berlin.de> Al Bowers <xa******@rapid sys.com> writes:


Richard Bos wrote:
Richard Heathfield <do******@addre ss.co.uk.invali d> wrote:
Dan Pop wrote:

But that's the point: the casts (which are effectively no-ops in terms of
generated code) avoid the need of temporary pointers that serve no other
purpose than avoiding the casts.

You missed the *primary* purpose of the temporary pointers, which is that
they make the code easier to read.

Says you. In some often-occurring cases (e.g., qsort()ing strings using
a wrapper for strcpy()), the cast version is, IMO, easier to read than
the extra-object version.


What is this often-occuring case (qsort()ing strings using a
wrapper for strcpy()?


Obvious typo: he meant strcmp(). Consider the case when the comparison
has to omit a fixed length header, that might contain binary data:

int compare(const void *s1, const void *s2)
{
return strcmp((char *)s1 + OFFSET, (char *)s2 + OFFSET);
}

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #19
Dan Pop wrote:
In <bp************ *@ID-169908.news.uni-berlin.de> Al Bowers <xa******@rapid sys.com> writes:
Richard Bos wrote:
Richard Heathfield <do******@addre ss.co.uk.invali d> wrote:

Dan Pop wrote:

>But that's the point: the casts (which are effectively no-ops in terms of
>generate d code) avoid the need of temporary pointers that serve no other
>purpose than avoiding the casts.

You missed the *primary* purpose of the temporary pointers, which is that
they make the code easier to read.
Says you. In some often-occurring cases (e.g., qsort()ing strings using
a wrapper for strcpy()), the cast version is, IMO, easier to read than
the extra-object version.


What is this often-occuring case (qsort()ing strings using a
wrapper for strcpy()?


Obvious typo: he meant strcmp(). Consider the case when the comparison
has to omit a fixed length header, that might contain binary data:

int compare(const void *s1, const void *s2)
{
return strcmp((char *)s1 + OFFSET, (char *)s2 + OFFSET);
}


I think this needs to be something like:

int compare(const void *s1, const void *s2)
{
return strcmp(*(char **)s1 + OFFSET, *(char **)s2 + OFFSET);
}

Jeremy.
Nov 13 '05 #20

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

Similar topics

2
3136
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
21041
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,...
2
1730
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
17421
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
26
7785
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
8969
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
3021
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
13732
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
5667
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
9455
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9271
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
9869
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...
0
9708
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...
0
8709
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7242
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
5140
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...
0
5302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2665
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.