-3

I'm doing an UDP connection, between a microcontroller and a computer. The framework I'm using is c++ based and has a function to send an UDP packet with the following prototype:

bool UdpConnection::send(const char *data, int length)

int length is the number of bytes that the pointer contains.

But I'm doing some input reading using a function that returns a uint16_t type.

I cannot change anything directly in those two functions.

Then I did the following:

UdpConnection udp;
uint16_t dummy = 256;
udp.send(reinterpret_cast<char*>(dummy),2);

But I'm a curious guy so I tried the following:

UdpConnection udp;
uint16_t dummy = 256;
udp.send((char*)dummy,2);

When I compile this last code I get:

error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]

In my analysis both snippets do the same thing, why I get an error in the last one but not in the first?

EDIT:

The first snippet of code compiles but gives segmentation fault when the code runs. So neither code works.

EDIT 2:

An efficient and tested solution for the problem, but not an answer to the original question, is this:

union Shifter {
    uint16_t b16;
    char b8[2];
} static shifter;

shifter.b16 = 256;
udp.send(shifter.b8,2);

This solution is widely used, but it's not portable as it's dependent on CPU byte ordering, so do a test in your application before to be sure.

Oshio
  • 133
  • 4
  • 17
  • As the *`data`* parameter, wouldn't it be sensible to actually create a `char*` string message and pass that in? You can also calculate the value for `length` from that string. – ifconfig Jul 30 '19 at 04:26
  • Yes doesn't make sense, see my edit. – Oshio Jul 30 '19 at 04:31
  • Can someone clarify me why the down-vote in the question? – Oshio Jul 30 '19 at 04:33
  • @ifconfig I'm striving for efficiency. But you are right! – Oshio Jul 30 '19 at 04:42
  • "everything works" you send something, what does the other end receive? – n. m. could be an AI Jul 30 '19 at 04:57
  • @n.m I see, at the time I wrote that I tried to explain the compilation was successfully, without running the code, then I saw that it gives segmentation fault, (see the first edit), but I understand the down vote now, thanks. – Oshio Jul 30 '19 at 05:03
  • The two codes are exactly the same, the compiler just warns in one case but not the other for no apparent reason – M.M Jul 30 '19 at 05:14
  • @M.M I read in somewhere that `reinterpret_cast<>` is like I'm saying to the compiler: "-Never mind this I know what I'm doing". Quoting cppreference.com: "_It is purely a compile-time directive which instructs the compiler to treat expression as if it had the type new_type._ " Maybe that is the reason, but I'm not sure. – Oshio Jul 30 '19 at 05:21
  • @DavidSchwartz the question is not pointless, what I'm trying to do is very simple, see Radosław Cybulski answer, I was missing ampersand operator, as he **respectfully** pointed out. – Oshio Jul 30 '19 at 05:35
  • The posted answer doesn't address the question of "why I get an error in the last one but not in the first?". But you accepted it. So it is not really clear what your question was. If the question was "Why doesn't my code work?" you should have asked as much. Also, it would improve the question to include a [MRE](https://stackoverflow.com/help/minimal-reproducible-example) – M.M Jul 30 '19 at 06:22
  • Also the "EDIT 2" method causes undefined behaviour. It would be better off to stick to the original question (once you sort out what that was), rather than answering your own question in the question box, and even asking new questions as you did. You can certainly post something in the Answer box that answers your original question. If you have new questions, and can't find an answer on the site already, then post a new question. This site uses a Q/A format, one question per question as such. – M.M Jul 30 '19 at 06:29
  • @M.M Your explanation is clear, the adjustments will be made, but regarding the "EDIT 2", I did mentioned it in the comments of the accepted answer, I think it will be disrespectful to just delete it, how would you proceed in such situation? – Oshio Jul 30 '19 at 20:03
  • @Oshio you could just delete it, comments don't matter (comments are for requesting clarification and moderators regularly clean them up) – M.M Jul 30 '19 at 20:39

1 Answers1

2

I would assume this is correct:

udp.send(reinterpret_cast<char*>(&dummy),2);

Note ampersand. Otherwise you are sending two byes from address 256, which are probably random (at best). This is microcontroller, so it might not crash. Second version should be:

udp.send((char*)&dummy,2);
Radosław Cybulski
  • 2,952
  • 10
  • 21
  • can you see my "EDIT 2"? could you tell what is the most efficient method? – Oshio Jul 30 '19 at 05:06
  • 1
    There's no difference in the efficiency of all these methods. And in any case it makes no sense to worry about the efficiency of code when the inefficient step in your program is that you are sending UDP packets. That takes longer than anything else and the time taken has nothing to do with the code you write. – john Jul 30 '19 at 06:24
  • 1
    There's no difference as @john wrote. On top of that using union the way u did is undefined behaviour. Even if there was any difference, time of sending / retrieving UDP package will thrumph any performance issues involved with the code itself. – Radosław Cybulski Jul 30 '19 at 09:09