Methods Valid in C that are not in c++

Started by
3 comments, last by Stowelly 16 years, 2 months ago
im trying to port an example given out by sony called vsync.c which basically demonstrates how to use kernel function calls to access the framebuffer and blit it. basically this sample is created in C and i want to use it within C++ but the following error occurs host.cpp:47: error: pointer of type void * used in arithmetic host.cpp:50: error: pointer of type void * used in arithmetic which after a bit of googling it seems it is unsafe in c++ and not in C but i have no idea how to rectify this problem, there is the full source of the offending function thanks

void prepare_buffer(void *addr, struct ps3fb_ioctl_res *res,
			uint32_t num_frames, int color)
{
	uint32_t i;
	int x, y, x_size, y_size;
	uint32_t r, g, b;
	uint32_t *p;
	void *fb;

	x_size = res->xres - 2 * res->xoff;
	y_size = res->yres - 2 * res->yoff;

	for (i = 0; i < num_frames; i++) {
		fb = addr + (res->xres * res->yres * BPP) * i; 
//		printf("addr:%d %p\n", i, fb);
		for (y = 0; y < y_size ; y++) {
			p = (uint32_t *)(fb + y * res->xres * BPP);
			for (x = 0; x < x_size ; x++) {
				if (color) {
					r = 0xff * (i & 0x01);
					g = 0xff * (i & 0x02);
					b = 0xff * !(i & 0x01);
				} else {
					r = 0xff * 0;
					g = 0xff * 0;
					b = 0xff * 0;
				}
				*p = (r << 16  | g << 8 | b);
				p++;
			}
		}
	}
}
http://stowelly.co.uk/
Advertisement
The short answer is that you need to cast the void pointers to char pointers to do pointer arithmetic on them.
Having a void* at all is generally not a good idea. It's pointing to something, and that something has a type. A framebuffer is a large array of bytes, shorts or longs and the pointer type should reflect that.

The reason this is an error is that it makes no sense to perform arithmetic on a void*. When performing pointer arithmetic, the C++ compiler has to know how large the pointees are. For example, if I have an int* p and I use something like p++ (which will advance the pointer to the next pointee in the array), this is not equivalent to taking the value of p and adding 1 to it. Instead, this will take p and add sizeof(int) to it. Since there is no sizeof(void), it's impossible to do pointer arithmetic on a void*.
Quote:Original post by Stowelly
host.cpp:47: error: pointer of type void * used in arithmetic
host.cpp:50: error: pointer of type void * used in arithmetic

which after a bit of googling it seems it is unsafe in c++ and not in C

Arithmetic on void pointers is not standard C or C++.
If it worked on one C compiler then that's because that compiler was being kind, and is probably GNU based, as you found out it won't necessarily work on another more stringent compiler.
Thanks for your quick replys

the void* pointer addr is initialised using memset
mmap(NULL, length, PROT_WRITE, MAP_SHARED, fd, 0);
memset(addr, 0, length);

(length is the total memory size of 2 frames)


would this be better achieved using an array initialised to the size of this? thinking about it now it seems like this should have been really obvious to me
http://stowelly.co.uk/

This topic is closed to new replies.

Advertisement