0% found this document useful (0 votes)
4 views26 pages

Lecture 5 - Array of Structure - Padding - Packing

The document covers topics in Computer Programming II, focusing on arrays of structures, structure padding, and packing. It explains how to define and initialize structures, the importance of padding for memory alignment, and techniques to minimize memory usage through packing. Additionally, it discusses passing structures to functions in C++, including methods such as pass by value, reference, and pointer.

Uploaded by

adorabitong1227
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views26 pages

Lecture 5 - Array of Structure - Padding - Packing

The document covers topics in Computer Programming II, focusing on arrays of structures, structure padding, and packing. It explains how to define and initialize structures, the importance of padding for memory alignment, and techniques to minimize memory usage through packing. Additionally, it discusses passing structures to functions in C++, including methods such as pass by value, reference, and pointer.

Uploaded by

adorabitong1227
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Computer programming II

Lecture : Array of Structure, Padding and Packing


WINCHESTER R. GEREZ
Instructor, Department of Computer Science
[email protected]

RALF L. DELA CRUZ


Instructor, Department of Computer Science
[email protected]
Array of Structure
• In an Array of
Structure, each
element of the array
holds a complete
structure.
Array of Structure
• Structure Definition

• Array Creation
Array of Structure
• Initialization

• Displaying the Array


Structure Padding
• Padding refers to the insertion of unused bytes
between members of a structure to ensure proper
alignment.
• Most compilers automatically add padding to align
data members within a structure, especially to
optimize memory access and improve performance.
Structure Padding
Let the size of int is 4 bytes
and size of char is 1 byte.
Structure Padding
Let the size of int is 4 bytes
and size of char is 1 byte.

Processor doesn’t read 1


byte at a time from memory.
It reads 1 word at a time
Structure Padding
Structure Padding
Structure Packing
• Packing is the process of minimizing the padding
added between structure members to conserve
memory.
• We can avoid the wastage of memory by simply
writing #pragma pack(1)
Structure Packing
We can avoid the wastage of memory by simply writing
#pragma pack(1)
Activity
• Create two structures • Print each student’s
• Date – for birthdate information directly
inside
• Student – ID, Name,
Grade, and a Date
• Create an Array of 3
students inside the
main function
• Manually input
student data
Passing Structures to Functions
and Structures within
structures.
Laboratory 6
Structure
• Structure (struct) is
a user-defined data
type in C++.
• It lets you group
multiple variables
(of different types)
into one name.
Concept of Structure within a
Structure (Nested Struct)
• In C++, you can
declare a struct as a
member of another
struct.
How to Access Nested Structure
Members
• When you have a
nested struct, you
access the inner fields
by chaining the dot
operator (.)
What Happen if you use only
p1.address?
• p1.address refers to
the entire Address
structure inside p1.
• It's NOT a single value
— it's the whole set
(street, city, zipCode).
Example
Passing Structures to Functions in
C++
Basics of Passing
Structures
1. Pass by Value
2. Pass by Reference
3. Pass by Pointer
Pass by Value
• A copy of the
structure is sent to the
function.
• Changes made inside
the function do not
affect the original.
Pass by Value
Pass by Reference (&)
• Instead of sending a
copy, the function
works directly on the
original structure.
• Changes inside the
function will affect
the original structure!
Pass by Pointer (*)
• You send the memory
address of the
structure.
• Useful in dynamic
memory situations.
Questions?

You, right now.


Get in
Touch
WithusUs
Send a message
or visit us
City of Batac, Ilocos Norte,
Philippines
(63) 77-600-0459
[email protected]

Follow us for
updates
facebook.com/MMSUofficial
www.mmsu.edu.ph

You might also like