473,322 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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_cast<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,sizeof(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*****@graphics.lcs.mit.edu |
| home : http://www.graphics.lcs.mit.edu/~decoret|
+-------------------------------------------------+

Jul 19 '05 #1
2 3110
On Sat, 19 Jul 2003 00:47:38 -0400, Xavier Decoret
<de*****@graphics.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_cast<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,sizeof(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.learn.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_name_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
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
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...
21
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...
1
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...
1
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...
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
17
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: ...
27
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...
16
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.