Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2012-03-17
8
Medium Priority
?
869 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 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Cloud Training Guides

FREE GUIDES: In-depth and hand-crafted Linux, AWS, OpenStack, DevOps, Azure, and Cloud training guides created by Linux Academy instructors and the community.

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 is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

715 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