CPSC 259 Chapter Notes - Chapter 7.3: In C

29 views2 pages
Making memory diagrams, which use boxes to indicate variables and arrows for
pointers, are excellent ways of showing how memory is used during run time and allows
you to work quickly with coding.
Sharing:
When two pointers refer to a common pointee, which happens when you use the
operator ‘=’ then the pointers are said to be sharing the pointee. So it is two or more
entities like pointers, sharing a common memory structure. This is the key advantage of
using pointers as these allows you to save space, as you donot have to replicate the data
structure for each different entity that you use.
Shallow and deep copying
Sharing is nice since it allows different functions to communicate with each other. So a
function has a usable value or values. Instead of making copies of the data set, which
can be large and consume a lot of memory, the function just sends a pointer to the
function and then both can reference it.
This is called shallow copying, as the functions refer to the data set are sharing it. So a
change to the data set by one function would change the original data set.
Deep copying is when an additional copy is made of the original data set and both the
function get the separate copies. This is useful as each function can change this value,
without worrying about the other but also means that the program runs slowly due to
the large number of copies being made.
Bad pointers:
A bad pointer is a pointer, that is not referenced to a pointee. So it is simply a pointer,
with an arrow going no where, or an arrow moving aimlessely. It is represented by a box
with x’s in it.
You should never deference a bad pointer. As this is a serious run time error. If your
code is crashing that is usually because of a bad pointer deference.
All pointers start as bad pointers, that is they are not referenced to a pointee. You do
that by coding the pointee.
In real time languages like Java, a pointer is automatically set to a NULL value when it is
deferenced, and the complier checks everytime the pointer is accessed, meaning that
errors are automatically spotted. In C and C++, the bad pointer alters a random part of
the memory, so the program works fine but crashes at a later time in running of
operation, which is a pain obviously!
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows half of the first page of the document.
Unlock all 2 pages and 3 million more documents.

Already have an account? Log in

Get access

Grade+
$10 USD/m
Billed $120 USD annually
Homework Help
Class Notes
Textbook Notes
40 Verified Answers
Study Guides
1 Booster Class
Class+
$8 USD/m
Billed $96 USD annually
Homework Help
Class Notes
Textbook Notes
30 Verified Answers
Study Guides
1 Booster Class