Solved

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

Posted on 2012-03-17
8
812 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
  • 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now