read int from char * question

Started by
39 comments, last by ApochPiQ 6 years, 10 months ago

This does not compile. You do not understand how references work, and you should look them up.

Yes, I know, this is how I expected it to work, and I must admit, that I have used it in that way long ago?

Advertisement

and I must admit, that I have used it in that way long ago?

I'm not an expert on changes made to C++, but I'm fairly certain references have never worked like that, and that they have always worked the way we (me + ncsu121978 + Juliean) have described in this thread.

Hello to all my stalkers.

Would agree with Lactose, as far as I can remember I started coding with C++ 98 and am pretty sure that even since that standard it has worked the same way as it works now, I mean cant speak for before then.. but that is going back some 20 years to reach that kinda age :o

I know that Visual C++ compiler extensions (in the past and maybe today) would bind temporary variables when passing by reference to non-const functions. This is different from passing a constant by non-const reference though, but maybe early compilers like Visual C++ 6.0 allowed that too for some reason. It wasn't very standard's compliant, after all. Alternately, Johnny Code is remembering wrong.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

I know that Visual C++ compiler extensions (in the past and maybe today) would bind temporary variables when passing by reference to non-const functions. This is different from passing a constant by non-const reference though, but maybe early compilers like Visual C++ 6.0 allowed that too for some reason.

I haven't used references for myself. I have worked in Visual C++ 6.0 professionaly, and I didn't encounter references on the scope of entire project code base.

I think references quite break memory security, since you can introduce them to (type*) parameters of functions, while they can be addresses of local/temporary variables, breaking heap allocation guarantees.

WiredCat could have done this

inline int read_int_from_stream(int count, int index, char * src)
{

return *((int*)(src+index*4))

}

I think references quite break memory security, since you can introduce them to (type*) parameters of functions, while they can be addresses of local/temporary variables, breaking heap allocation guarantees.


Not sure what you mean by "memory security", but you can technically do that with pointers, too. It's still undefined behaviour in both cases.

int* naughty() {
   int x = 5;
   return &x;
}
So... uh, don't do that.

WiredCat could have done this inline int read_int_from_stream(int count, int index, char * src) { return *((int*)(src+index*4)) }

The entire point of passing the index as a reference & incrementing it was to sequentially unpack the buffer though:


int index = 0;

readInt(src, index); // index is 0
readInt(src, index); // index is 4
readInt(src, index); // index is 8
...

With your method, you'd have to do:


int index = 0;

readInt(src, index); // index is 0
index += sizeof(int);
readInt(src, index); // index is 4
index += sizeof(int);
readInt(src, index); // index is 8
index += sizeof(int); 

Which results in more redundant code, which is also vulnerable to typos, harder to maintain...

Which results in more redundant code, which is also vulnerable to typos, harder to maintain...

The second method, though seems to have redundant code (since you seem to unpack a cycle? dunno), is not introducing cache incoherency, just to do something as trivial as an index change, at ideal best, the inline function of first method would reach nearly-the same cache coherency as the second method (maybe). You still can use sane overloads to retrieve indexed infromation, yet I cannot read why since you know count and index and type up front the call?


Not sure what you mean by "memory security", but you can technically do that with pointers

int* naughty() { int x = 5; return &x; }

You have used a reference.

You have used a reference.


No, that is a pointer. This code uses a reference to do the same thing as the example I posted earlier.

int& naughty() {
    int x = 5;
    return x;
}
You have used a reference.

We've said multiple times now that you do not understand references and how they are different/similar to pointers. I even had a post showing that this way of using the & symbol does not mean a reference, but "address of", which you use when pointing a pointer to an already existing variable.

Maybe it's time to stop posting about the subject until you have read more about it and understand things better. All you are doing now is spreading misinformation.

Hello to all my stalkers.

This topic is closed to new replies.

Advertisement