Solved

[C, C++, g++] Void pointer in arithmetics with warning in Ubuntu

Posted on 2012-03-17
8
848 Views
Last Modified: 2012-03-17
If i compile my code with gcc under Ubuntu i get no warning and everything works good.

If i compile the same code with g++, i get this warning:

warning: pointer of type ‘void *’ used in arithmetic

This is the code:
 ssize_t read_num(int fd, void *data, size_t count) {
	ssize_t total = 0;

	while (count) {
		ssize_t rd = read(fd, data, count);
		if (rd < 0) {
			perror("read");
			return rd;
		} else {
			count -= rd;
			data += rd; <-- PROBLEM IS HERE
			total += rd;
		}
	}
	return total;
}

Open in new window

However, the program compiles and it works fine but i'd like to solve the warning.

I know that i cannot use void pointer in math in c++ because it is not a safe operation, but i do not know how to solve the warning.

Can you help me, please?
My g++ version is 4.5.2
0
Comment
Question by:ichigokurosaki
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 37732924
void has no size.

So, adding to a pointer-to-void is nonsensical, because for doing so, the size of the objects pointed to needs to be known.

Instead of void*, use eg. unsigned char*, or whatever pointer type you need.


The reason it worked with gcc, is because of backward compatibility reasons, which treats void* to be a pointer to byte-size objects. It's a bad idea to depend on this though.
0
 

Author Comment

by:ichigokurosaki
ID: 37732959
The problem is that i have this:

typedef struct {
	unsigned char length;
	unsigned char magic;
	unsigned char type;
} zeemote_hdr_t;

// then i do:

 zeemote_hdr_t hdr;

int rd = read_num(bt, &hdr, sizeof(hdr));

ssize_t read_num(int fd, void *data, size_t count) {
	ssize_t total = 0;

	while (count) {
		ssize_t rd = read(fd, data, count);
		if (rd < 0) {
			perror("read");
			return rd;
		} else {
			count -= rd;
			data += rd;
			total += rd;
		}
	}
	return total;
}

Open in new window


So, i do not know how to properly use the data pointer because if i change the void point in (unsigned char*) or (int *), i always get errors in g++.

What do you suggest me?
0
 

Author Comment

by:ichigokurosaki
ID: 37732976
Do you think it can be ok if i do this:

ssize_t read_num(int fd, void *data, size_t count) {
	ssize_t total = 0;
        char *data_changed = (char*) data;
	while (count) {
		ssize_t rd = read(fd, data_changed, count);
		if (rd < 0) {
			perror("read");
			return rd;
		} else {
			count -= rd;
			data_changed += rd;
			total += rd;
		}
	}
	return total;
}

Open in new window


g++ does not show warnings now.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 53

Expert Comment

by:Infinity08
ID: 37733026
>> Do you think it can be ok if i do this:

In most cases, that will work.

But if you are writing code in C, why use a C++ compiler ?
Or, alternatively, if you are writing C++, why not do it the C++ way, and use streams ?
0
 

Author Comment

by:ichigokurosaki
ID: 37733068
The problem is that i'm using some C functions in a C++ programs.
C functions involve a lot of code lines..
do you think is it better to traslate all of them in C++ by using streams?

They are working fine at the moment and g++ gives me no errors or warnings..
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 37733099
That depends on what you want :)

If you are happy with the way it is now, and all is running fine, then there's no need to change it.
0
 

Author Closing Comment

by:ichigokurosaki
ID: 37733106
Thanks a lot for the suggestion!
0
 

Author Comment

by:ichigokurosaki
ID: 37733107
Yep, for the moment, it seems to be fine :D
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question