CSCI 270 Structures, Pointers, and Dynamic Memory Allocation

wackybabiesSoftware and s/w Development

Dec 14, 2013 (3 years and 7 months ago)

73 views


1

CSCI 270
Structures, Pointers, and Dynamic Memory Allocation


#include <iostream>

using namespace std;


struct date

{



int month;





int day;

};

typedef
date *

datePtr
;






//

datePtr

can be used in place of
date*




datePtr

CreateDate

(

);





// or
date *
Create
Date ();

void AssignDate




(
datePtr &
);






// or (
date * &
)

Pass a pointer by reference

void PrintDate
1





(
datePtr
);



/
/ or
(
date *
)

Pass a pointer b
y value

void PrintDate2

(
date
);








// Pass
es

a struct by value

void ChangeDate



(
date &
);





//
P
ass
es

a struct by reference

int

ChangeMonth


(

);








//
R
eturn
s

a value to be assigned to a field

in a struct

void

ChangeDay



(int & day);






//
P
ass
es

a struct field by reference

void PrintMonth




(int month);







//
P
ass
es

a struct field by value


int main ()

{


cout << "This program demonstrates the notation used with pointers to
\
n"


<< "str
uctures and various methods of passing pointer parameters.
\
n"


<< "You will be asked twice to enter a date and then change it.
\
n
\
n
\
n";




datePtr a, b;



















// or
date
*a, *b
;



















cout << "Create a new date" << endl;

// CreateDate () will
create a new struct, assign values


a
= CreateDate ();














//
to its fields, and return its address





cout << "Your date is ";


PrintDate
1
(
a
);










// Pass a pointer by value

(struct can be changed)


cout << "
\
n
\
nNow change this date" << endl;


ChangeDate (
*a
);








// Pass a
struct

by reference

(struct can be changed)


cout << "Now your date is ";


PrintDate
2
(
*
a
);










// Pass a
struct

by value

(struct can’t be changed)



cout << "
\
n
\
nCreate a second date" << endl;


AssignDate (
b
);










// Pass a pointer by reference



function creates struct



cout << "Your
second
date i
s ";



P
rintDate
2
(
*
b
);

















// Pass a struct by value

(struct can’t be changed)


cout << "
\
n
\
nNow change the second date..." << endl;


b
-
>month

= ChangeMonth();




// Assign a returned value to a field


ChangeDay (
b
-
>day
);








// Pass a field of a struct by reference


cout << "
\
nNow your second date is ";


PrintDate
1

(
b
);









// Pass a pointer by value

(struct can be changed)


cout << endl << endl;




delete

a, b;












// Deallocate the structs a and b are pointing to.















// This is not necessary here because any















// dynamically allocated memory is automatically
















// released when the program terminates.


a = NULL;














//
Assigning NULL to a pointer is a safeguard for


b = NULL;














//
remembering
that the pointers

no longer point to


system("pause");














// meaningful locations.


return 0;

}


2



datePtr

CreateDate ()








// or
date *

CreateDate ()

{


da
tePtr

p1
;










//
or
date
* p1
; (
Pointer to a date structure
)



p1 = new
(nothrow)
date;








// Create a structure and make

p1 point to it



if (
p1

== NULL
)





// or if (

!
p1

)


{













// (
this form of
new returns NULL
(int 0)
if there is

not


cout << "Out of memory";





//

enough
memory available)


return (NULL);









// or take other action as appropriate


}



cout << "
\
nEnter a month number
--
> (1 = Jan, 2 = Feb, ...) ";


cin >>
p1
-
>month
;


cout << "What day of ";


PrintMonth (
p1
-
>month
);


cout << "? ";


cin >>
p1
-
>day
;


return
p1
;

}






//
pointer

to a struct is passed by

value

void PrintDate
1

(
datePtr

p2
)





// or void PrintDate
1

(const
Date &

p2
)

{













// (prototype would also have to be changed)


PrintMonth (
p2
-
>mon
th
);


cout << " " <<
p2
-
>day
;


//
Are these assignments legal? If so, w
hat
variables, if any, are changed?

//

p2
-
>month

= 4;

//

p2
-
>day

= 1;

}



void PrintDate2

(
date

d
)













// Pass a struct by
value

{






























// The funct
ion doesn’
t know that the struct was


PrintMonth (
d.
month
);












//
dynamically
allocated



no pointer
is
used here


cout << " " <<
d.
day
;












// The function only gets a copy of the struct


// Are these assignments legal? If so, what
variables, if any, are changed?

//

d.
month

= 4;

//

d.
day

= 1;

}




void ChangeDate (
date &

d
)










//
S
tructure is passed

by
reference


{


cout << "
\
nEnter new month number (1 = Jan, 2 = Feb, ...) ";


cin >>
d.month
;
















// d is a refe
rence to the structure (another name


cout << "What day of ";











// for a struct variable
)
, so use the dot notation


PrintMonth (
d.month
);












// rather than pointer notation. The function has


cout << "? ";



















// no
idea that the struct was actually dynamically


cin >>
d.day
;


















// allocated.


}



3

// These two functions demonstrate passing individual fields in a struct variable as

// parameters (if you don't
want/
need to pass the whole structure).

//

When you call the function, you have to qualify the field name to identify the value/address

// to be passed, but the function sees only a single variable and doesn't know that it is

// actually part of a structure. We could just as easily pass to Chan
geMonth or ChangeDay

// one value that is an element in an array of ints
-

the function sees only an int.


int ChangeMonth ()









// Return type is not qualified because the return

{














// value i
s not part of a struct here; the return value




// could be assigned to any integer variable


int month;


cout << "
\
nEnter a new month number (1 = Jan, 2 = Feb, ...) ";



cin >>
month
;


return
month
;

}



void ChangeDay (
int & day
)







// Note
that the
parameter name is not qualified
.































// That would be trying to declare a field without































// a struct. The
function doesn’t know that the

{














//
parameter we’re passing is

a field in a struct
.


cout << "Enter a new day number ";


cin >>
day
;

}




void AssignDate (
datePtr &

pRef
)



// or void GetDate (
date
* &

pRef
)

{













// pRef is a reference (another name)

for p, which













//
will point to a

newly
-
created structure.



pRef = new
(nothrow)
date;








// Create a n
ew structure and make













/
/ pRef (and therefore p) point to it.



if (pRef

== 0
)









// or if (pRef == NULL)


{


cout << "Out of memory";








// or whatever action wo
uld be


return;











// appropriate for the application


}



cout << "
\
nEnter the month number (1 = Jan, 2 = Feb, ...) ";


cin >>
pRef
-
>month
;












// A reference may be used i
n exactly the same


cout << "What day of ";











// way as the variable for which it is a reference.


PrintMonth (
pRef
-
>month
);








// Pass only the month field of the struct.


cout << "? ";


cin >>
pRef
-
>day
;















}



4


void Pr
intMonth (int month)










//
Note that the
parameter is not qualified
.


{





























// The function doesn't know the int is part of a struct


switch (month)


{



case 1 :

cout << "January";


break;




case 2 :

cout << "February";


break;



case 3 :

cout << "March";



break;





case 4 :

cout << "April";




break;





case 5 :

cout << "May";



break;





case 6 :

cout << "June";



break;






case 7 :

cout << "July";




break;





case 8 :

cout << "August";



break;





case 9 :

cout << "September";

break;





case 10 :

cout << "October";


break;





case 11 :

cout << "November";

break;





case 12 :

c
out << "December";

break;





default :

cout << "invalid month number";


}


}