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?

I have been programming with C++ for just over 25 years, and this is how references have always worked, even in the cfront 1.0 days. The fundamentals of the language have not changed in the last few years. If they had changed so radically, there would be mobs brandishing pitchforks and torches trying to beat down the doors of the compiler vendors because most of the existing codebase would be broken, and the doyens of international commerce do not take kindly to their transactions being delayed.

Stephen M. Webb
Professional Free Software Developer

Advertisement
Just to clarifiy the reference vs pointer thing a very last time:

For the compiler a reference is the same as a pointer.
It just points to some address in memory where the data starts. Thats a simple fact!

But references have a lot of benefits:
- Automatic referencing and deferencing
- Less typing (No -> needed)
- const makes it more safe
- (Maybe more type checking)
- (Maybe magic optimizations by the compiler)

int value = 0;
int *ptrValue = &value; // Referencing is explicit
int &refValue = value; // Referencing is done automatically
 
*ptrValue = 10; // Deferencing is explicit
refValue = 42; // Deferencing is done automatically
 
// Return value is 42
return value;
Btw. my view about c++ has changed: I love it and wont code in C anymore ;-)

Now back to topic:

Why do you not do this?

- In this case, count are not needed i think
- Memcpy is not needed to done explicit
- Naming things explicitly to int32, int16, int64 whatever is much cleaner than just read_int, especially when you work with streams or network
- When you do more than reading, like converting or something like that you name it unpack_int32_from_stream
- There is no safety check here, you should include a maxStreamCount and check boundary

inline int32_t read_int32_from_stream(int &streamIndex, const char *streamSource, const int maxStreamCount)
{
  assert((streamIndex + 4) <= maxStreamCount);
  int32_t result = *(int32_t *)(uint8_t *)(streamSource + streamIndex);
  streamIndex += sizeof(int32_t);  
  return result;
}
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

I know, I did. My meaning is that reference, wheather l-value, or r-value, is still a reference? And I say you use it when you actualy invoke it. I's aware of misunderstanding them already away, I hadn't continued my speach in that pattern anymore.

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

You have used a reference, to establish a pointer from it.

I have been programming with C++ for just over 25 years,

In C language for example, references could not be l-values, in C++ they could, that is perhaps where I picked it from. And as I mentioned, introducing heap allocation guarantee break by making type& lval possible in C++ (in a more hidden obscure ways I mean).

I personaly am interested why is WiredCat solving the inline function like that, by manipulating index in the reading function, doing stack-crossing.

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

You have used a reference, to establish a pointer from it.

No, he didnt use a reference. he used he used "return &x" .. notice how the & is directly to the left of the variable, this is the "Address Of" operator... its functionality is that it gets the address in memory of an object.. so he didnt use any references.. he used an operator to get an address in memory...

You have used a reference, to establish a pointer from it.

He did not use a reference. You have been corrected several times, but you still persist in being wrong.

The & symbol can mean "reference", but it can also mean "address of". In the code Oberon_Command posted, it is not a reference to the variable, it is getting the address of the variable. These are different things.

A symbol can mean different things. & does not always mean reference.

This is similar to:


struct myStruct
{
    float a;
};

myStruct s;
s.a = 5.0f;

Here, the . symbol does not always mean "decimal point that you use to write floats or doubles". Sometimes, it means "access the struct's members". Context matters.

Hello to all my stalkers.

In C language for example, references could not be l-values


C does not have references. C only has pointers.

I am beginning to suspect that this is a language difficulty.

so he didnt use any references.. he used an operator to get an address in memory...

I have explained it, operator & is used, thus a reference is used.

C does not have references. C only has pointers.

It did have references, but in your defintion that I observe from here, didn't. Realize you are doing an exclusion from the definition, while I was doing my own mistaken exclusion from defintion too of course.

C had & operator that provided addresses of instances, for example for managing of pointers, not the memory they point to. But did not have a reference in extended way of it being an l-value typed variable.

We can stop this discussion, I just insist that by using & operator you have used a reference (though yet did not use referenced memory yet).

I have explained it, operator & is used, thus a reference is used.

You have been told multiple times that this is wrong. Repeating it won't make it right. You clearly have no idea what you are talking about, so why do you continue arguing? Accept that you are wrong and move on. Jeez.

You have been told multiple times that this is wrong. Repeating it won't make it right. You clearly have no idea what you are talking about, so why do you continue arguing? Accept that you are wrong and move on. Jeez.

You realy got stuck on something, and I have said it multiple times as well: You say that using & operator is not using a reference, that only using the referenced object (cpp dereference), by a reference variable (that is lvalue of & type)- is using a reference. Then you support this statement by my mistaken knowledge of references in cpp I had.

It went too off topic, and I will not respond to that issue anymore.

operator & is used, thus a reference is used.

You are wrong. Please research references and pointers.

Hello to all my stalkers.

This topic is closed to new replies.

Advertisement