473,890 Members | 1,397 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 12766
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
3142
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
21069
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
1736
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
17526
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
26
7804
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
9004
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
3029
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
13745
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
5782
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
9980
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...
1
10926
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
10468
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
9643
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...
0
7172
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
5856
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
6064
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4683
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4278
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.