473,549 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reinterpret or static_cast?

Hi,
look at this function, which does some low-level IO.

void writen(int fd, const void* data, size_t size)
{
while (size) {
int n = write(fd, data, size);
if (n < 0)
throw logic_error...

static_cast<con st char*>(data) += n; // reinterpret_cas t?
size -= n;
}
}

Here it is necessary to do a cast in order to do some pointer arithmetic.
Is the static_cast or the reinterpret_cas t appropriate?

/FAU
Jul 22 '05 #1
2 2108

"Frank A. Uepping" <nu********@spa mgourmet.com> wrote in message news:bp******** *****@news.t-online.com...
Here it is necessary to do a cast in order to do some pointer arithmetic.
Is the static_cast or the reinterpret_cas t appropriate?


I'd prefer static_cast (in it's undo defined conversions role), but I don't know that
it is more appropriate than reinterpret_cas t in this case.
Jul 22 '05 #2
Frank A. Uepping wrote:
Hi,
look at this function, which does some low-level IO.

void writen(int fd, const void* data, size_t size)
{
while (size) {
int n = write(fd, data, size);
if (n < 0)
throw logic_error...

static_cast<con st char*>(data) += n; // reinterpret_cas t?
This will not even compile. The result of this cast in an rvalue.
Operator '+=' cannot be applied to an rvalue. Most likely is was
supposed to be

static_cast<con st char*&>(data) += n

But I don't think this is a valid technique, regardless of the cast
operator used. The more proper way to do this would be the following

data = static_cast<con st char*>(data) + n;

size -= n;
}
}

Here it is necessary to do a cast in order to do some pointer arithmetic.
Is the static_cast or the reinterpret_cas t appropriate?
...


--
Best regards,
Andrey Tarasevich

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
6347
by: Gary Labowitz | last post by:
Am I doing this correctly? It is a sample program for my class. #include <iostream> using namespace std; int main( ) { int x=3, y=4;
5
9713
by: A | last post by:
Hi, I have not been able to find an example for using the reinterpret cast. Any ideas? Regards, A
26
2521
by: Steven T. Hatton | last post by:
The code shown below is an example from the Coin3D documentation. I believe the use of the C-style cast is safe under the circumstances, but from what I've been exposed to (TC++PL(SE)), I would favor using a static_cast. Is there any technical reason to favor the C-style over a static_cast? http://doc.coin3d.org/Coin/index.html void...
2
1951
by: Amit | last post by:
Greetings. I am having some problem while using a cast operation(static_cast and/or dynamic_cast) between base and derived objects when passing to functions. what I have is something like this.. template <class T> class Node{ protected: T e; Node* left;
15
6961
by: thomas.mertes | last post by:
For a hash function I want to reinterpret the bits of a float expression as unsigned long. The normal cast (unsigned long) float_expression truncates the float to an (unsigned long) integer. But this is not the effect I want for a hash function. With unions my hash function can be implemented:
24
4724
by: Rahul | last post by:
Hi, I have a class A : public B {...member functions......data members}; and am doing the following A *p=new A(); void *p=static_cast<void *>(p); factory_instance->process(p);
0
1330
by: NiveditaB06 | last post by:
it is recommended not to use reinterpret cast that is mostly used for type converting one pointer type to other totally different type( like int ).After casting one member variable to another type using reinterpret cast you should cast back the member variable to actual type before using.Please anyone explain the reason behind this.If it is for...
5
3870
by: jason.cipriani | last post by:
There have been some recent threads about casting pointers to and from void* that have me rethinking some of my usual practices. I have a couple of questions. 1. What is the purpose of C++'s static_cast<>? In other words, is there any real difference between statements like (with non-pointer types): double a = 3.4; int b = (int)a; //...
3
367
by: Rahul | last post by:
Hi, Everywhere I read that static_cast<only work fine for the conversion which are implicitly allowed by the compiler hence the following does not work int *i; double *d; d = i; // Compilation error , OK i= static_cast<int *>(d); // Compilation error. OK
0
7475
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7836
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6078
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5393
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5116
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3521
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3502
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1970
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1084
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.