CS101x S438A Structures and Pointers Part 1 IIT Bombay
CS101x S438A Structures and Pointers Part 1 IIT Bombay
Computer Programming
Dr. Deepak B Phatak
Dr. Supratik Chakraborty
Department of Computer Science and Engineering
IIT Bombay
Session: Structures and Pointers – Part 1
MEMORY
Main memory is a sequence of
401 10111111
physical storage locations 402 10010001
• Each location stores 1 byte (8 bits) 403 1 0 1 1 0 1 1 1
404 10010001
Content/value of location 405 10000111
• Each physical memory location 406 11110001
407 10000000
identified by a unique address 408 11111111
MAIN
• Index in sequence of memory 409 00000000
locations 40a 11110000
MEMORY
by a process STACK SEGMENT
• Divided into:
Code segment: Stores DATA SEGMENT
executable instructions in
program
CODE SEGMENT
MAIN
Data segment: For
dynamically allocated data
Stack segment: Call stack
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 5
Structures in Main Memory
IIT Bombay
int main()
{
struct MyStructType {
char z;
int x, y;
}; Needs 4 bytes of storage
MyStructType p1;
int a;
… Rest of code …
return 0;
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 6
Structures in Main Memory
IIT Bombay
int main()
MEMORY
{ STACK SEGMENT
struct MyStructType {
char z;
int x, y;
};
MyStructType p1; a
int a; DATA SEGMENT
… Rest of code …
MAIN
return 0; CODE SEGMENT
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 7
Structures in Main Memory
IIT Bombay
int main()
{ Needs 1 + 4 + 4,
struct MyStructType { i.e. 9 bytes of
char z; storage
int x, y;
};
MyStructType p1;
int a;
… Rest of code …
return 0;
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 8
Structures in Main Memory
IIT Bombay
int main()
MEMORY
{ STACK SEGMENT
p1.z
struct MyStructType { p1.x
char z;
int x, y; p1.y
};
MyStructType p1; a
int a; DATA SEGMENT
… Rest of code …
MAIN
return 0; CODE SEGMENT
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 9
Structures in Main Memory
IIT Bombay
MEMORY
{ STACK SEGMENT
gap/padding? p1.z
struct MyStructType { p1.x
char z;Wait for a few
int x, y; slides p1.y
};
MyStructType p1; a
int a; DATA SEGMENT
… Rest of code …
MAIN
return 0; CODE SEGMENT
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 10
Structures in Main Memory
IIT Bombay
int main()
MEMORY
{ STACK SEGMENT
p1.z
struct MyStructType { p1.x
char z;
int x, y; p1.y
};
MyStructType p1; a
int a; DATA SEGMENT
… Rest of code
p1,…a: local variables of “main”
MAIN
return
Memory0; for “p1” and “a” allocatedCODE
in activation
SEGMENT
}
record of “main” in call stack
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 11
What Can We Safely Assume About Structures?
IIT Bombay
struct MyStructType {
MEMORY
STACK SEGMENT
char z; p1.z
int x, y; p1.x
}; p1.y
MyStructType p1;
No assumptions about
DATA SEGMENT
relative layout of different
members within memory
MAIN
CODE SEGMENT
allocated for a structure
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 12
What Can We Safely Assume About Structures?
IIT Bombay
struct MyStructType {
MEMORY
STACK SEGMENT
char z; p1.z
int x, y; p1.x
}; p1.y
MyStructType p1;
DATA SEGMENT
No assumptions about
“padding” (unused memory locations) after locations
MAIN
CODE SEGMENT
allocated for different members of a structure
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 13
What Can We Safely Assume About Structures?
IIT Bombay
struct MyStructType {
MEMORY
STACK SEGMENT
char z; p1.z
int x, y; p1.x
}; p1.y
MyStructType p1;
DATA SEGMENT
No assumptions about
“padding” (unused memory locations) after locations
MAIN
CODE SEGMENT
allocated for different members of a structure
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 14
What Can We Safely Assume About Structures?
IIT Bombay
struct MyStructType {
MEMORY
STACK SEGMENT
char z; p1.z
int x, y; p1.x
}; p1.y
MyStructType p1;
Memory locations allocated for each member are
DATA SEGMENT
however contiguous (have consecutive addresses).
MAIN
E.g., four contiguous locationsCODE
for p1.x,
SEGMENT
four contiguous locations for p1.y
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 15
Recall: “&” and “*” Operators
IIT Bombay