If your program creates and array to store data in a file, and the file (rather than you the programmer) is what determines how many elements ought to be in the array. The size of the array is bound to change from run to run of the program, because the file may change.
Q: For a static array A, do you have to delete []
A, when you don't need it anymore?
No. (Only when A is created by new int [6]).
struct Quad
{
char label;
point vert[4];
};
If A is a static array, the pointer A cannot be changed.
To understand the difference between this version of
Quad and the previous version, consider this picture:
Note:
Quad object; the array is outside of the Quad object,
somewhere else in memory.
vert is embedded in the
Quad object.
The above picture really tells you all you need to know to understand the difference between using static and dynamic arrays ... when you really can use static arrays.
Q: Can you change the contents of a static array?
A: Of course, yes
Q:
Suppose that I have a Quad object S that contains the
label 'Q' and the vertices (0,0) (1,0) (1,1) (0,1). I assign S to a new Quad
object R and change the label in R to 'P'. I then print out
S and then R. What will I get?
A:
It depends whether I'm using the static version of
|
|
| Dynamic Version | Static Version |
|
|
Q:
How does swap(A,B) behave differently for
two Quads, A and B,
with the dynamic versus static array versions of
Quad?
A: Once again, the above picture should tell you that while the result is the same, a lot more work gets done in the static case, where the entire contents of the arrays are swapped, rather than simply the pointers.
| point.h | point.cpp | main1.cpp |
|
|
|
| triangle.h | triangle.cpp | |
|
|
$ g++ -c point.cpp
$ g++ -c triangle.cpp
$ g++ -c main1.cpp
In file included from triangle.h:1:0,
from main1.cpp:2:
point.h:1:8: error: redefinition of 'struct point'
point.h:1:8: error: previous definition of 'struct point'
The problem is that this causes the struct point to be defined
twice in main.cpp. Why?
main.cpp #includes point.h.
This gives the first definition of point in main.cpp
main.cpp also #includes
triangle.h, which in turn #includes
point.h! This amounts to the second definition of point in
main.cpp!
point and triangle to go back and worry about this
stuff.
.h files
with #defines. The #include that we've
been using and the #define that we're about to use are
examples of "preprocessor directives".
#define lets you give a string x and say
"every time you see x in the source code, replace it with string
y."
#define to
determine whether or not this is the first time the compiler has seen our
.h while compiling a given .cpp file.
#ifndef (read as "if not defined") preprocessor
directive allows us to test whether or not a certain string has been
#defined already.
| code | If SILLYSTRING has not been #defined
| If SILLYSTRING has been #defined
|
|
|
|
SILLYSTRING has not been #defined previously in
our program, the word not will be printed out --- the
#ifndef SILLYSTRING asks if it's true that
SILLYSTRING hasn't been defined. If it is, then all the text until
the #endif is seen by the compiler. Otherwise, it
is as if the code in between was never there.
How does this help us with our multiple-definition problem?
point.h |
triangle.h |
main.cpp |
|
|
|
main.cpp we first #include the file
point.h. Since this is the first time the compiler has seen
point.h it'll find that POINTHEADER has not been
defined. Therefore all the code until the #endif, which is
essentially the whole file, will be seen and evaluated by the compiler. This
will include the line that #defines POINTHEADER.
point.h, which is when main.cpp
does the #include "triangle.h", the compiler will hit that
#ifndef line and will find that POINTHEADER has been
defined, and therefore everything up to the #endif, which is
essentially the whole file, will be ignored by the compiler. This way we never
get any multiple definitions.
.h files should be "protected"
by #ifndefs this way!
Tip: It might be helpful to know that a static array can be initialized with a list of values in { }'s. For example, an array of the first 10 prime numbers can be constructed like this:
int prime[10] = {2,3,5,7,11,13,17,19,23,29};
Note: this problem is purely about static arrays, it doesn't
concern structs at all.
Here's a solution.