Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is raw pointer in c++? How they differ from normal pointer? [duplicate]

Tags:

c++

pointers

Is there is any special meaning for raw pointer?

Is normal pointer variable and raw pointer are same?

like image 579
VINOTH ENERGETIC Avatar asked Mar 13 '14 07:03

VINOTH ENERGETIC


People also ask

What is raw pointer in C?

A raw pointer is a pointer whose lifetime isn't controlled by an encapsulating object, such as a smart pointer. A raw pointer can be assigned the address of another non-pointer variable, or it can be assigned a value of nullptr .

How do smart pointers differ from regular pointers?

A Smart Pointer is a wrapper class over a pointer with an operator like * and -> overloaded. The objects of the smart pointer class look like normal pointers. But, unlike Normal Pointers it can deallocate and free destroyed object memory.

What's the difference between pointer and reference?

References are used to refer an existing variable in another name whereas pointers are used to store address of variable. References cannot have a null value assigned but pointer can. A reference variable can be referenced by pass by value whereas a pointer can be referenced by pass by reference.

How do you make a raw pointer in C++?

The raw pointers are exactly the same with normal pointers, they can be written like this: type * pointer_name = & variable_name; Since C++11, we have some special pointers, called "smart pointers". They are called "smart" because they know when they have to delete the used memory.


2 Answers

The raw pointers are exactly the same with normal pointers, they can be written like this:

type * pointer_name = & variable_name;

Since C++11, we have some special pointers, called "smart pointers". They are called "smart" because they know when they have to delete the used memory. They do it when nothing else in your program uses that block of memory. There are 3 types of smart pointers in C++11:

unique_ptr<typename> pointer_name;
weak_ptr<typename> pointer_name;
shared_ptr<typename> pointer_name;

You can read more about using these types of pointers here.

like image 122
Victor Avatar answered Oct 06 '22 21:10

Victor


Yes, a raw pointer is a normal pointer.

like image 4
QuestionC Avatar answered Oct 06 '22 23:10

QuestionC