Reference to pointer and malloc - c++

Consider
int *p;
auto & pp = p;
pp = (decltype(pp))malloc(sizeof(decltype(*pp))); //line1
pp = (decltype(p))malloc(sizeof(decltype(*p))); //line2
why line1 is not acceptible but line2 is fine suggesting they are of different types.
Why pp and p are not of same type?

pp is a reference to an int *
According to https://msdn.microsoft.com/en-us/library/dd293667.aspx
"The following code fragment initializes variable x to type int, variable y to a reference to type const int, and variable fp to a pointer to a function that returns type int."
C++
int f(int x) { return x; }
int main()
{
auto x = f(0);
const auto & y = f(1);
int (*p)(int x);
p = f;
auto fp = p;
//...
}

Related

Why cant I post-increment a function call c++

Lets say I have this array and function
int a[] = {1,2};
const int * bar() {
return a;
}
Then in main
int main() {
const int * p1 = bar()++;
}
Why does this throw a compiler error? In my thinking it would return a const int pointer to a and then the post increment operator would move the pointer over to the second element in a.
test.cpp:14:25: error: lvalue required as increment operand
14 | const int * p1 = bar()++;

Difference between `int* const& x` and `int* const x` in C++

I've read of the differences between passing by value, passing by reference, and passing (a pointer) by constant reference, yet I'm don't understand the difference between the latter, and just passing a constant pointer. As an example, what is the difference between
int PI = 3;
int* getArg(int* const& x){
x = Π
return x;
}
int main() {
}
and
int PI = 3;
int* getArg(int* const x){
x = Π
return x;
}
int main() {
}
Both of these cause the same error: assignment of read-only parameter 'x'.
If you're clear on passing variables by value and by reference, then try breaking down complex types into parts to help make sense of what's going on:
using PointerToInt = int*;
using ConstPointerToInt = PointerToInt const;
int* getArg_v1(ConstPointerToInt x) {
x = Π // it's const by value so you're not allowed to change it
return x;
}
int* getArg_v2(ConstPointerToInt& x) {
x = Π // it's const by reference so you're not allowed to change it
return x;
}

reference & pointers confusing parts(declarations and their types)

I'm studying pointer and reference parts and having very hard time to study them.
I think I understand now some simple usage of reference and pointers in the function, but there is something I cannot totally understand.
Here are some variable declarations:
int a = 1;
float b = 2;
int* p = &a;
string s = "Hello";
vector<int*> values = {p};
What will be the types of the following expressions?
&a
&b
p
&p
&s
&(s.at(1))
values.at(0)
&(values.at(0))
&values
I have no idea what their types exactly are, but I tried it myself.
&a : pointer to int
&b : pointer float
p : pointer to int
&p : pointer to pointer to int
&s : pointer to string
&(s.at(1)) : pointer to string
values.at(0) : pointer to int
&(values.at(0)) : pointer to pointer to int
&values : pointer to pointer to int
and one more problem >
write the following variable declarations:
a) A pointer to a string
b) A reference to a float
c) An array of pointers-to-ints.
d) A pointer to a pointer to bool
e) A reference to a pointer to an int
and my answers are:
a: string* s = "Hello"
b: float& f = g;
c: int n =1;
int*x =&n;
int arr[] = {*x};
d: bool y = true;
bool* x = &y;
bool** qq = &x;
e: int a = 1;
int* x = &a;
int& z = *x;
I'm not sure about my answers. Please help these confusing parts.
First, a std::string is a complex object that contains a char array but it is definitely not a char array. And a std::vector<int> is also a complex object that contains an array of ints.
That means that for the first part some of your tries are wrong:
&(s.at(1)) : pointer to string WRONG: pointer to char
values.at(0) : pointer to int OK
&(values.at(0)) : pointer to pointer to int OK
&values : pointer to pointer to int WRONG: pointer to `vector<int>`
For the second part, as a string is not a char array, you cannot initialize a pointer to string with a litteral char array
string *s = "hello"; WRONG syntax error
You must first create a string and then create a pointer to it
string s = "hello"; OK std::string initialized from a const char *
string *ps = &s; OK pointer to std::string
c is wrong too int arr[] declares an array of int. You must write:
int *arr[] = { x }; OK array of 1 pointers initialized from x which is a pointer to int
For e, int&z = *x; declares a reference to int initialized as a ref to a. To get a ref to a pointer, you must write:
int *&z = x; OK ref to a pointer to int initialized as a ref to x
Part 1:
If the expression e has type T, the type of &e is pointer to T; T*.
Consider the types of the expressions s.at(1) and values more carefully.
Part 2:
c: int arr[] is not "array of pointers-to-ints", it's "array of int".
An array of T is T arr[]; an array of int* is int* arr[].
int n = 1;
int* arr[] = {&n};
e: int& z is not "reference to a pointer to an int", it's "reference to int".
A reference to T is T&; a reference to int* is int*&.
int a = 1;
int* x = &a;
int*& z = x;
If you need to know the exact type of an expression and if you can use Boost, it provides a great functionality.
#include <boost/type_index.hpp>
#include <iostream>
#include <string>
#include <vector>
#define TYPE(x) std::cout << "decltype(" << #x << ") == " << type_id_with_cvr<decltype(x)>().pretty_name() << std::endl
int main()
{
using boost::typeindex::type_id_with_cvr;
int a;
TYPE(a);
int * p = &a;
TYPE(p);
TYPE(&p);
std::vector<int*> values {p};
TYPE(values.at(0));
TYPE(&(values.at(0)));
TYPE(&values);
}
If you can't use Boost, you can still deliberately create a template error to force the compiler to show you the type of expressions (from a talk of Scott Meyers, a great expert of C++).
#include <iostream>
#include <string>
#include <vector>
template <typename T>
class TD;
template <typename T>
void function(T & param)
{
TD<T> templateType;
TD<decltype(param)> paramType;
}
int main()
{
int a;
function(a);
int * p = &a;
function(p);
function(&p);
std::vector<int*> values {p};
function(&values);
}
c) An array of pointers-to-ints
You created an array of int containing a single value, the value to wish the pointer refers to. As said previously, you need a int* [] array.
&(values.at(0)) : pointer to pointer to int OK
Why &(values.at(0)) is a pointer ?
Pointer is variable that contains address in memory
Pointer has its type
unsigned long ip = 0xC0A80A01; // 192.168.10.1
unsigned char *p = (unsigned char *)&ip;
printf("%d.%d.%d.%d\n", p[3], p[2], p[1], p[0]);
// Result: 192.168.10.1
I think it is only takes address of variable.
int a = 1;
int *p = &a;
&a is not a pointer
p is a pointer
Is it?
and
int &refVar = a;
refVar is reference

confusion about a function returning int&&

I wrote some test code.
int getInt() {
int a = 3;
return a;
}
int& getIntR() {
int a = 3;
return a;
}
int getRL() {
return int(1);
}
int&& getRRL() {
return getRL();
}
int main() {
// works, even though there may cause some problem
int& d = getIntR();
// right,reference to a temporary variable
int&& e = getInt();
// right,because the returned value is lvalue which can't be assigned to rvalue referecen
//int&& f = getIntR();
// here is the code which confused me
int& g = getRRL();
int&& ee = 10;
int& e = ee;
}
I construct a function returns a int&& type, so I need a temporatory variable.
as getRRL() show, I think the code maybe treated like this.
int&& temp = 1;
int&g = temp;
temp is a reference to rvalue, but it itself is a lvalue, so i can assign this to int&, but the conpiler says "the initial value of of a non-const refernce must be lvalue" which means the returned type is not lvalue.
But the code below works well.
int&& ee = 10;
int& e = ee;
Can someone tell me the reason? Thanks very much!

about "int const *p" and "const int *p "

#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
int i1 = 0;
int i2 = 10;
const int *p = &i1;
int const *p2 = &i1;
const int const *p3 = &i1;
p = &i2;
p2 = &i2;
p3 = &i2;
cout << *p << endl
<< *p2 <<endl
<< *p3 <<endl;
return 0;
}
The code can be compiled with both VC6.0 and VC2010.
But I have questions as blow:
const int *p = &i1;
It means what the "p" points can not be modified,but p can not be modified,am I right?
so
p = &i2;
this line can be complied,yes?
This line:
int const *p2 = &i1;
In my mind,this means p2 can not be modified while what p2 points can be changed, am i right?
Why the
p2 = &i2;
can be compiled?
About this line:
const int const *p3 = &i1;
p3 = &i2;
Oh,god... I am crazy. I have no idea why this line can be compiled with no error...
Can any body help me?
Another code which confused me is here:
class Coo2
{
public:
Coo2() : p(new int(0)) {}
~Coo2() {delete p;}
int const * getP() const
{
*p = 1;
return this->p;
}
private:
int* p;
};
why this code can be compiled?
In
int const * getP() const
I have change the value or *p !
Here we consider 4 types of pointers declarations:
int * w;
It means that w is a pointer to an integer type value. We can modify both the pointer and its content. If we initialize w while declaration as below:
int * w = &a;
Then, both of below operations are viable:
w = &b;(true)
*w = 1;(true)
int * const x;
It means x is a constant pointer that points to an integer type value. If we initialize x while declaration as below:
int * const x = &a;
Then, we cannot do: x = &b;(wrong) because x is a constant pointer and cannot be modified.
However, it is possible to do: *x = 1;(true), because the content of x is not constant.
int const * y; //both mean the same
const int * y;
It means that y is a pointer that points to a constant integer value. If we initialize y while declaration as below:
int const * y = &a;
Then, it is possible to do: y=&b;(true) because y is a non-constant pointer that can point to anywhere.
However, we cannot do: *y=1;(wrong) because the variable that y points to is a constant variable and cannot be modified.
int const * const z; //both mean the same
const int * const z;
It means that z is a constant pointer that points to a constant integer value. If we initialize z while declaration as below:
int const * const z = &a;
Therefore, non of below operations are viable:
z = &b;(wrong)
*z = 1;(wrong)
With the help of pointer, you can actually do two things.
You can change the data it is pointing to but cannot point to a different memory location.
You can point it to a different memory location but cannot change the data it is pointing to.
Now, when you say, int const* ptr or int const* ptr, it falls under first category. It's same as -
const int num = 5; // Both mean the same.
int const num = 5;
To, actually not able to change to a different location, i.e., pointer to a constant location but be able to modify the data, the semantics should be int* const. Since the content of the pointer is a constant, it should be initialized while declaration.
int num = 5;
int* const ptr; // Wrong
ptr = &num; // Wrong
int* const ptr = &num;
*ptr = 100;
However, there is a third kind. Constant pointer to a constant location, which can neither point to a different memory location nor change the data it is pointing to. ( i.e., const int* const )
And now answering the questions, the first two can be compiled because they are not pointing to constant locations. So, they can be modified at later stages too.
const int const *p3 = &i1;
p3 = &i2; // Wrong
In the above snippet, p3 is a constant pointer to a constant location. So, it cannot be modified.
const at the end of a member function says it is not going to change the state of the object. When you say *p = 1;, you are not changing the state of the object. p still points to the same memory location. This is not allowed to do -
int const * Coo2::getP() const
{
*p = 1; // State of `p` is still not modified.
p = new int ; // Error: Changing the memory location to which p points.
// This is what changing the state of object mean and
// is not allowed because of `const` keyword at the end of function
return this->p;
}
Hope, now you understand why the program compiles :)
int const * p; and const int * p are the same. It is when the const comes after the * that
the semantics of the expression change.
I know, it's crazy.
const int *p = &i1;
int const *p2 = &i1;
These both declare non-const pointers to const data.
That is, using p, you cannot change the data it points to. However, you can change the pointer itself, for example, by assigning as p = &i2 which is legal. But *p = 87987 is illegal, as the data p points to is const!
--
int * const p = &i1;
This declares const pointer to non-const data. That is, p=&i2 is illegal, but *p = 98789 is legal.
--
const int * const p = &i1;
This declares const pointer to const data. That is, now both p=&i2 and *p=87897 are illegal.
The two are exactly the same. What matters is the position of the qualifier relative to the asterisk (*):
int const *p; // normal pointer to const int
const int *p; // ditto
int *const p; // const pointer to normal int (rarely useful)
int const * const p; // const pointer to const int
Here is a pointer to a constant:
const int* p;
The following statement is illegal, because it attempts to change the
value of a constant:
*p = 3;
But this one is legal, because the pointer itself is not a constant:
p = &x;
On the other hand, this declaration shows a constant pointer to a variable.
int* const p;
In that case, the following statement is legal, as the variable can be changed:
*p = 3;
but this one is not, because it attempts to change the value of a constant.
p = &x;
Reference link:http://faculty.cs.niu.edu/~freedman/241/const-ptrs.txt
No, the const keyword before the * means that the variable you are pointing to is a "const" variable and only it can not be modified.
If you want a pointer that can't be reassigned then you need to declare it as Foo* const p = &bar;
If you want a pointer that points to a "const" object that can't be reassigned declare it as const Foo* const p = &bar
It is perfectly fine to have a pointer of const int* foo be assigned to a pointer of const int* const bar just like it is fine to have an int's value assigned to a const int. Think of it in the same manner.
int const * is the same as const int *
Succinctly; each combination of read/write int & pointer;
int main() {
int a,b;
int* w; // read/write int, read/write pointer
w= &b; // good
*w= 1; // good
int* const x = &a; // read only pointer, read/write int
// x = &b; // compilation error
*x = 0; // good
int const * y; // read/write ptr, read only int
const int * y2; // " " "
y = &a; // good
// *y = 0; // compilation error
y2 = &a; // good
// *y2 = 0; // compilation error
int const * const z = &a; // read only ptr and read only int
const int * const z2 = &b; // " " " "
// *z = 0; // compilation error
// z = &a; // compilation error
// *z2 = 0; // compilation error
// z2 = &a; // compilation error
}