answersLogoWhite

0


Best Answer

It means your program has undefined behaviour.

In weakly-typed languages such as C it is very easy to assign a pointer to itself but in strongly-typed languages such as C++, the type system prevents it. However, we can use a C-style cast to circumvent the type system.

Consider the following example:

#include<iostream>

int main()

{

// Declare a pointer variable.

int* p;

// Assign the pointer's address to itself using a C-style cast.

p = (int*)&p;

// Print the address of the pointer.

std::cout << "Pointer address:\t0x" << &p << std::endl;

// Print the pointer's value.

std::cout << "Pointer value:\t\t0x" << p << std::endl;

// Print the dereferenced value.

std::cout << "Dereferenced value:\t" << *p << std::endl;

// Assign a value to the address being pointed at.

*p = 42;

}

The exact address of the pointer will vary, but the output may be something like the following:

Pointer address: 0x0072FB48

Pointer value: 0x0072FB48

Dereferenced value: 7535432

The first two lines of output prove the pointer really does point to itself (the storage address and its content have the same value). The third line also proves that the pointer can be correctly dereferenced (7535432 is the decimal equivalent of 0x0072FB48).

Everything appears to work normally, but the final line of code has actually violated the type system:

*p = 42;

Think about what we're doing here. We're assigning the value 42 to the address pointed to by p. But p points to itself, thus p now holds the value 42 and is therefore pointing at memory address 42 (0x0000002A). That's clearly not what we intended but, by undermining the type safety, such errors are only inevitable. Even if memory address 42 belongs to our program, a 4-byte integer would be expected to be aligned on a 4-byte boundary but 42 is clearly not a 4-byte boundary.

Our program doesn't actually make any attempt to access address 42, so no errors will be reported. However, if this code were part of a much larger program we'd be in a whole world of hurt because it may not be immediately obvious that a problem exists until we try to dereference that pointer, which may be a long time after we assigned the value. That's a time-bomb waiting to go off!

There's also the problem of what happens when the type being pointed at is larger than that of the pointer itself. If we attempt to assign a value that will not fit inside a pointer then we'd end up overwriting memory that doesn't actually belong to the pointer.

Either way we introduce undefined behaviour. Even if our program appears to work as intended (as in the example above), it makes no sense to deliberately undermine a system that is intended to prevent such problems arising in the first place.

User Avatar

Wiki User

8y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What if the storage address and content both have the same value?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Is call by reference and pass by reference same thing?

Strictly speaking there is no such term as call by value. Functions are called while parameters are passed. When someone uses the term call by value they really mean pass by value, so in that sense they are the same. However call by value is incorrect terminology.


What factors determine the structure and content of a training program?

The organizational structure, work activities, and informational content identified in a job analysis serve as the basis for developing both the structure and content of a training program


Multiple indirection in c language?

C uses pointers for indirection. So, using a pointer to a pointer would be multiple indirection. For example, the following code uses multiple indirection:int i = 42;int *pi = &i;int **ppi = &pi;**ppi++;printf("i is now %d\n", i);


Pre increment and post increment?

Both increment the value of the variable by one. The difference is the value of the increments expression itself. With preincrement value is taken after incrementing, and with postincrement value is taken before incrementing. Example: Let x have value 5. y = ++x; Both y and x are assigned value 6. Again let x have value 5. y = x++; y is assigned value 5. x is assigned value 6.


What is the difference between pre and post processing incrementation?

To increment a value by 1, you have 4 choices:value++;++value;value += 1;value = value + 1;Pre and post processing incrementation/decrementation refers to the first two: ++value and value++.Both do exactly the same, as both will increase the value of 'value' by one.If we have a situation like this:int value = 0;int value1 = 0;value1 = value++;This essentially means:value1 = value;value = value + 1;Where ++value means:value = value + 1;value1 = value;

Related questions

Where can one find businesses that handle both moving and storage?

Yellowbooks have a section on moving help. Businesses will be listed in alphabetical order and with their phone number and address. Calling their number will confirm whether or not they handle both moving and storage.


What are primary storage and secondary storage in common?

both are the storage devices both store the data in binary form both are the costlier


How much internal harddrive do you need 4 internet?

BOTH Hard drive (GB) and Internet (Mbps) have different terms of measurement. In storage they relate as how much data you wish to store for uploading or downloading content from Internet you can decide as per your system's storage capacity (or yours how much you can buy and keep it in a safe place - external storage drives).


Are gold jewelry buyers buying the jewelry or are they looking to melt it down and extract the gold content?

Both. The gold jewelry has intrinsic value depending on the gold content, and they can possibly sell it for more if the price of gold increases. They also melt the gold down to extract the gold content, especially if they have a large quantity of lower-quality gold jewelry they want to get value from.


A food storage area in both the roots and stems is called the?

The cortex is the food storage area found in both the roots and stems. Starch is the main form of food stored by the cortex.


Does the processor uses permanent storage sometimes called secondary storage to hold both data and instructions while it is processing them?

False, the processor does not use permanent storage to hold both data and instructions while it is processing them. The processor uses temporary storage to hold data and instructions.


Is Zip drive is a input or output device?

Zip drives are both input-output and storage devices.


Do storage beds have storage on 2 sides?

There are many types of under bed storage and it is true that most storage beds have drawers for storage on both sides. There's also other types of storage bed, for example the ones with the liftup mattress.


What rhymes with both syllables in the word orange?

storage


The processor uses temporary storage called to temporarily hold both data and instructions while it is processing them?

The processor uses temporary storage called primary storage, to temporarily hold both data and instructions while it is processing them. Another word for primary storage is called memory.


Is content a synonym or antonyms of satisfied?

Content is a synonym of satisfied, because they both have similar meanings.


Is a hard disk a storage device or storage media?

The hard drive would be considered both, as the media is not removable.