answersLogoWhite

0

What is meant when you say you dereference a pointer variable?

Updated: 12/13/2022
User Avatar

RichardIsaacsgp7883

Lvl 1
9y ago

Best Answer

When you dereference a pointer you are referring to the object being pointed at.

int* ptr = new int (42);

int& ref = *ptr;

You must be careful when you dereference pointers like this because ptr is volatile and could be deleted at any time. If ref is still in scope after ptr is released, ref will refer to invalid memory and thus invalidates your program. Moreover, if ptr falls from scope without releasing the memory it points to and ref remains in scope, there's no way to release the memory without invalidating ref which, again, invalidates your program. In order to be valid, references to dereferenced pointer values must be placed in a separate scope from the pointer and must fall from scope before the pointer falls from scope. Even if a reference falls from scope immediately after a pointer falls from scope, your program is still deemed invalid.

In some cases there is no need to hold a reference to a dereferenced pointer value, you can simply store the value by copying it to another variable.

int i = *ptr;

However, if the pointer is a complex object, keep in mind that this could invoke a cascade of copy constructors, which may impact performance.

The following code snippet shows all the major properties of a pointer: its own address, its value (the address being pointed to) and the indirect value (the dereferenced value).

std::cout << "Address: &ptr = 0x" << &ptr << std::endl;

std::cout << "Value: ptr = 0x" << ptr << std::endl;

std::cout << "Dereference: *ptr = " << *ptr << std::endl;

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is meant when you say you dereference a pointer variable?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What does a - followed by a greater than symbol mean in a c plus plus program?

It is a type of pointer dereference operation. If you have a pointer p to an object that has methods or attributes, you can say (*p).m to refer to the m method of the object, or you can say p-&gt;m to do the exact same thing.


How are pointer different from other variables?

A pointer variable is a variable that contains the memory location of another variable or an array (or anything else in memory). Effectively, it points to another memory location. For standard variables, you define a type, assign a value to that variable or read the value from it, and you can also read the memory location (&amp;n = memory location of n) of the variable. For pointers, you can point them to any variable, even another pointer, and you can get the value of the variable it points to (*p), the location of that variable in memory (p), or the address in memory of the pointer itself (&amp;p). Consider: long n = 65; long *p; p = &amp;n; Results: Type | Name | Location | Value long n 0xA3FF 65 long * p 0x31DF 0xA3FF So p points to n. Now, n = 65 &amp;n = 0xA3FF p = 0xA3FF *p = 65 &amp;p = 0x31DF You may find yourself having to use typecasts frequently when using pointers. Pointers are useful when passing data to functions. For instance, consider the following function: void add(int a, int b, int c) { c = a + b; } The problem here is that the computer copies each variable into a new memory location before passing them to the function as local variables. This function effectively does nothing. However, if you change the function to: void add(int a, int b, int *c) { c = a + b; } and call the function by passing in the location of the variable to the function: add(a,b,&amp;c); then you can modify the variable itself. Pointers are also good for working with arrays: char *c = "Hello World"; int i=0; while (c[i] != 0x00) { cout &lt;&lt; c[i]; c++ } //print one letter at a time.


What is the difference between structure and pointer?

A structure is a collection of primitives or other structures. A pointer is a memory address. Comparison of the two is like comparing bowling balls to cinder blocks. You can say that a structure defines the layout of the data, while a pointer points to data that is a particular structure.


What is dereferencing pointer?

They're related but not totally comparable.A pointer is an address in memory where a variable is located.An array is a conceptual data representation consisting of a list of more than one item of a particular scalar type (int, float, char, structure, etc.) where each element is accessed by its index. The index can be thought of as a counter or enumerator telling you how many elements you have to skip over to get to the one you're interested in. Here's where addresses come in ... the location of a particular element in memory is offset from the so-called base address (i.e. the address of the starting element) by the value(sizeof(one element) * index #)The C compiler is smart enough to take the sizeof() into account when you do pointer arithmetic, so you can find the address of the i-th element as (address of base) + i, rather than having to do the multiplication explicitly.The idea of element address as offset also accounts for C's use of zero-relative array definitions. Since the first element is offset by 0 positions from the first element its index in C is 0, not 1. The second (ordinal) element is offset from the first by 1 position so its index is 1, and so on.


What is stray pointer?

stray pointer is a that pointer which pin points nothing or anywhere but we dont know... for example: int *ptr; ptr=new int[10]; //this memory is on heap. and at the end of the programm if we dont delete this memory mean to say if we dont deallocate this memory then this type of pointer is pointing towards nothing or anywhere because when we work on heap deletion of pointer is must if we dont delete pointers than they pin point stray or anywhere so that sort of pointer is stray pointer. i think you understand...

Related questions

How are we going to use pointer in a program?

A pointer is a variable used specifically to store a memory address. We say the variable "points to" the memory address because we can dereference the pointer to access the value stored at that address. The pointer's type determines how that dereferenced value will be interpreted. Being a variable, we can change the stored address and thus change which value we point at. This makes it possible for the same variable to refer to different objects in memory, which includes other pointer variables.


What does a - followed by a greater than symbol mean in a c plus plus program?

It is a type of pointer dereference operation. If you have a pointer p to an object that has methods or attributes, you can say (*p).m to refer to the m method of the object, or you can say p-&gt;m to do the exact same thing.


What text results in variable space?

Meant to say variable white space


What is a structure pointer?

A pointer is a variable that holds address information. For example, in C++, say you have a Car class and another class that can access Car. Then, declaring Car *car1 =new Car() creates a pointer to a Car object.. The variable "car1" holds an address location.


How are pointer different from other variables?

A pointer variable is a variable that contains the memory location of another variable or an array (or anything else in memory). Effectively, it points to another memory location. For standard variables, you define a type, assign a value to that variable or read the value from it, and you can also read the memory location (&amp;n = memory location of n) of the variable. For pointers, you can point them to any variable, even another pointer, and you can get the value of the variable it points to (*p), the location of that variable in memory (p), or the address in memory of the pointer itself (&amp;p). Consider: long n = 65; long *p; p = &amp;n; Results: Type | Name | Location | Value long n 0xA3FF 65 long * p 0x31DF 0xA3FF So p points to n. Now, n = 65 &amp;n = 0xA3FF p = 0xA3FF *p = 65 &amp;p = 0x31DF You may find yourself having to use typecasts frequently when using pointers. Pointers are useful when passing data to functions. For instance, consider the following function: void add(int a, int b, int c) { c = a + b; } The problem here is that the computer copies each variable into a new memory location before passing them to the function as local variables. This function effectively does nothing. However, if you change the function to: void add(int a, int b, int *c) { c = a + b; } and call the function by passing in the location of the variable to the function: add(a,b,&amp;c); then you can modify the variable itself. Pointers are also good for working with arrays: char *c = "Hello World"; int i=0; while (c[i] != 0x00) { cout &lt;&lt; c[i]; c++ } //print one letter at a time.


What is the difference between structure and pointer?

A structure is a collection of primitives or other structures. A pointer is a memory address. Comparison of the two is like comparing bowling balls to cinder blocks. You can say that a structure defines the layout of the data, while a pointer points to data that is a particular structure.


What is pointer to a structure?

A pointer is a variable that holds address information. For example, in C++, say you have a Car class and another class that can access Car. Then, declaring Car *car1 =new Car() creates a pointer to a Car object.. The variable "car1" holds an address location.


What is meant when you say dependent variable y does not depend on independent variable x?

you can change the value of x to any new value and it has no effect on the value of y.


How do you say pointer in spanish?

el puntero


What is dereferencing pointer?

They're related but not totally comparable.A pointer is an address in memory where a variable is located.An array is a conceptual data representation consisting of a list of more than one item of a particular scalar type (int, float, char, structure, etc.) where each element is accessed by its index. The index can be thought of as a counter or enumerator telling you how many elements you have to skip over to get to the one you're interested in. Here's where addresses come in ... the location of a particular element in memory is offset from the so-called base address (i.e. the address of the starting element) by the value(sizeof(one element) * index #)The C compiler is smart enough to take the sizeof() into account when you do pointer arithmetic, so you can find the address of the i-th element as (address of base) + i, rather than having to do the multiplication explicitly.The idea of element address as offset also accounts for C's use of zero-relative array definitions. Since the first element is offset by 0 positions from the first element its index in C is 0, not 1. The second (ordinal) element is offset from the first by 1 position so its index is 1, and so on.


Differentiate the dependent from independent variable?

Say y = x2 y is the dependent variable, x is the independent variable.


What did the variable say to the equation?

I want