473,569 Members | 2,481 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

void* used in arithmetics

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
it is a compiler error and not only a warning. I can superced this by:
p = ((char*) p)+1;
but I don't like it.

class Type;
class TypeA : public Type; // has as sizeof 10 for example
class TypeB : public Type; // has as sizeof 20 for example

class Parent
{
public:
class Iterator
{
public:
Type* operator->() const { return reinterpret_cas t<Type*>(p_); }
Iterator& operator++() { p_ += stride_; return *this; }
protected:
friend class Parent;
Iterator(void* p,int s) : p_(p),stride_(s ) {}
private:
void* p_;
int stride_;
}
protected:
template <class T>
Iterator iterator(const T* t)
{
return Iterator(t,size of(T));
}
};
Then I can have sublclasses of Parent using vectors of TypeA or TypeB on
which I can iterate using the Type interface thru the Parent interface.

--
+-------------------------------------------------+
| Xavier Décoret - Post Doct |
| Graphics Lab (LCS) - MIT |
| mailto: de*****@graphic s.lcs.mit.edu |
| home : http://www.graphics.lc s.mit.edu/~decoret|
+-------------------------------------------------+

Jul 19 '05 #1
2 3128
On Sat, 19 Jul 2003 00:47:38 -0400, Xavier Decoret
<de*****@graphi cs.lcs.mit.edu> wrote in comp.lang.c++:
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
it is a compiler error and not only a warning. I can superced this by:
p = ((char*) p)+1;
but I don't like it.
Your reasons might or might not be good, but neither C nor C++ have
ever allowed this, and they never will. A pointer is not just a
number, it has additional qualities, namely the size of the type
pointed to.

If you add 1 to a pointer to char, the pointer points to the next char
(next byte).

But if you add 1 to a pointer to int, the pointer points to the next
int, so the actual address increases by sizeof(int). If you have a
pointer to a struct or a class with a size of 10 bytes, adding 1 to
that pointer causes the actual memory location pointed to to change by
10 bytes.

Pointers are not integer types, and do not behave like integer types.

To perform arithmetic on any pointer type, it must be a pointer to a
complete type, meaning that the compiler must have a definition for
the type in scope from which it can determine the size of the type.

"void" is an incomplete type which cannot be completed.

You can't perform arithmetic on pointers to any incomplete type.
That's just the way it is, the way it has always been, and almost
certainly the way it's going to stay.

Pointers to the character types are pointers to complete types, and
objects of these types (signed, unsigned, or "plain" char) are
ordinary bytes. The conversion of a pointer to void into a pointer to
any of the character types is guaranteed to leave the representation
unchanged, and then you can do arithmetic using the size of objects,
which is also expressed in terms of bytes.
class Type;
class TypeA : public Type; // has as sizeof 10 for example
class TypeB : public Type; // has as sizeof 20 for example

class Parent
{
public:
class Iterator
{
public:
Type* operator->() const { return reinterpret_cas t<Type*>(p_); }
Iterator& operator++() { p_ += stride_; return *this; }
protected:
friend class Parent;
Iterator(void* p,int s) : p_(p),stride_(s ) {}
private:
void* p_;
int stride_;
}
protected:
template <class T>
Iterator iterator(const T* t)
{
return Iterator(t,size of(T));
}
};
Then I can have sublclasses of Parent using vectors of TypeA or TypeB on
which I can iterate using the Type interface thru the Parent interface.


If you want to perform this sort of low-level pointer manipulation you
have no choice but to convert to pointer to one of the character
types. The language does not allow adding integers to pointer to void
because it would have to multiply the integer by sizeof(void), which
is not defined.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Jul 19 '05 #2
>object whose type may vary, as outlined below. So I don't understand why
it is a compiler error and not only a warning. I can superced this by:


Because the compiler doesn't know how far to move the pointer. If the
void* is an array of char, it would move it 1 byte for every
increment. On the other hand, if the void* points to an array of
double, it wouldn't have to move by one byte anymore, because a double
takes more than 1 byte of memory.

You must cast as you did in your sample. Sorry you don't like it, but
that the glamorous life of a C++ programmer...

</dib>

John Dibling
email: dib@substitute_ my_full_last_na me_here.com
Witty banter omitted for your protection
Jul 19 '05 #3

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

Similar topics

52
6271
by: Vladimir Grul | last post by:
Hello, I have a class member function declared as class some_class { .... virtual int call(void); }; Can I use this-> inside the function body?
6
1220
by: Terence | last post by:
I need some clarification with pointer arithmetics on void *. Example 1: ======== char s; char *ptr = s; ptr += 1; // I assume ptr is increased by 1 byte, pointing to the 2nd element in the array, right? Example 2:
21
2007
by: Milan Čermák | last post by:
Hi all, I'm about to write an arithmetics object library. It should contain classes like ShortInteger (for 32-bit longs), Double and Float as standard numeric type wrappers and LongInteger (really long numbers, not only 64-bit) and Fraction (to express high precission). All these classes may inherit from abstract class Number. I got stuck...
1
366
by: Jonas Ernst | last post by:
Hi, Can somebody give me some hints how to do a line interpolation without using floating point arithemtics? The function shall do a linear interpolation between 2 points (line interp?) and it shall return the y value to any x value given. e.g. p1: (0,0) p2 (1000,50) y value to calculate at x=600
1
1556
by: Rein Anders Apeland | last post by:
Hi all, We are developing a PoS application for Linux and are in need of a LGPL'ed og other 'free'-licensed library that can do exact decimal arithmetics for us. It should be similar or according to the specs at http://www2.hursley.ibm.com/decimal/ Somebody pointed my to the GNU Multiple Precision library, but that still
188
17265
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
17
6330
by: Frederick Ding | last post by:
Hi, guys,I met a problem, Please look at the problem below: int* bit = (int*)malloc(10000*sizeof(int)); memset(bit, 1, 10000*sizeof(int)); printf("%d %d %d\n", bit,bit, bit); Output: 16843009 16843009 16843009 Obviously I set the bit to bit to 1, but it outputs are not 1's.
27
8926
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
16
2674
by: PeterAPIIT | last post by:
Hello all C++ expert programmer, i have wrote partial general allocator for my container. After reading standard C++ library and code guru article, i have several questions. 1. Why allocator write forward declaration then allocation for void* rather than directly wrote allocator first ?
0
7618
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...
0
7926
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. ...
0
6286
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...
0
5222
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...
0
3656
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...
0
3644
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2116
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
1
1226
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
944
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...

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.