SlideShare ist ein Scribd-Unternehmen logo
1 von 68
Uri Dekel Carnegie Mellon University [email_address] Introduction to Pointers and  Memory Management in C
Assumptions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Intro to Pointers and Memory in C
Topics ,[object Object],[object Object],[object Object],[object Object],[object Object],Intro to Pointers and Memory in C
Focus ,[object Object],[object Object],[object Object],Intro to Pointers and Memory in C
Note on C versions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Intro to Pointers and Memory in C
Introduction to Computer Memory Intro to Pointers and Memory in C Background Dynamic Pointers
Why do we need memory? ,[object Object],[object Object],[object Object],[object Object],Background Dynamic Pointers Intro to Pointers and Memory in C
Memory needs to be organized ,[object Object],[object Object],[object Object],[object Object],[object Object],Background Dynamic Pointers Intro to Pointers and Memory in C
What do we do with multi-byte values? ,[object Object],[object Object],[object Object],[object Object],Background Dynamic Pointers Intro to Pointers and Memory in C
What do we do with multi-byte values? ,[object Object],[object Object],Background Dynamic Pointers Intro to Pointers and Memory in C
Virtual Memory ,[object Object],[object Object],[object Object],[object Object],[object Object],Background Dynamic Pointers Intro to Pointers and Memory in C
Variables and Function Calls in C Background Dynamic Pointers Intro to Pointers and Memory in C
Variables ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Background Dynamic Pointers Intro to Pointers and Memory in C
Automatic variables ,[object Object],[object Object],Background Dynamic Pointers Intro to Pointers and Memory in C
Allocating space for variables ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Background Dynamic Pointers Intro to Pointers and Memory in C
Program code and memory ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Background Dynamic Pointers Intro to Pointers and Memory in C
Stack example (In function) Background Dynamic Pointers Intro to Pointers and Memory in C
Stack example (In function) Background Dynamic Pointers Intro to Pointers and Memory in C
What happens in function call? ,[object Object],[object Object],[object Object],Background Dynamic Pointers Intro to Pointers and Memory in C
What happens in function call? Background Dynamic Pointers Intro to Pointers and Memory in C
Returning from functions… ,[object Object],[object Object],[object Object],[object Object],[object Object],Background Dynamic Pointers Intro to Pointers and Memory in C
Returning from functions… Background Dynamic Pointers Intro to Pointers and Memory in C
Attempting to write a swap function… Background Dynamic Pointers Intro to Pointers and Memory in C
Attempting to write a swap function… Background Dynamic Pointers Intro to Pointers and Memory in C
Attempting to write a swap function… Background Dynamic Pointers Intro to Pointers and Memory in C
Attempting to write a swap function… Background Dynamic Pointers Intro to Pointers and Memory in C
Pointers Background Dynamic Pointers Intro to Pointers and Memory in C
Why pass-by-pointer? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Background Dynamic Pointers Intro to Pointers and Memory in C
Pointers ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Background Dynamic Pointers Intro to Pointers and Memory in C
Assigning values to pointers ,[object Object],[object Object],[object Object],[object Object],[object Object],Background Dynamic Pointers Intro to Pointers and Memory in C
Dereferencing pointers ,[object Object],[object Object],[object Object],[object Object],Background Dynamic Pointers Intro to Pointers and Memory in C
Implementing Swap Background Dynamic Pointers Intro to Pointers and Memory in C
Implementing Swap Background Dynamic Pointers Intro to Pointers and Memory in C
Implementing Swap Background Dynamic Pointers Intro to Pointers and Memory in C
Implementing Swap Background Dynamic Pointers Intro to Pointers and Memory in C
Caveat – Uninitialized pointers ,[object Object],[object Object],[object Object],Background Dynamic Pointers Intro to Pointers and Memory in C
Caveat – NULL pointers ,[object Object],[object Object],[object Object],Background Dynamic Pointers Intro to Pointers and Memory in C
Caveats – Dangling references ,[object Object],[object Object],Background Dynamic Pointers Intro to Pointers and Memory in C
Casting pointers ,[object Object],[object Object],Background Dynamic Pointers Intro to Pointers and Memory in C
Void* ,[object Object],[object Object],[object Object],Background Dynamic Pointers Intro to Pointers and Memory in C
Const pointers Background Dynamic Pointers Intro to Pointers and Memory in C
Pointers to pointers Background Dynamic Pointers Intro to Pointers and Memory in C
Pointers to pointers Background Dynamic Pointers Intro to Pointers and Memory in C
Pointers to pointers Background Dynamic Pointers Intro to Pointers and Memory in C
Pointers to pointers Background Dynamic Pointers Intro to Pointers and Memory in C
Pointer Arithmetic ,[object Object],[object Object],[object Object],Background Dynamic Pointers Intro to Pointers and Memory in C
Pointers to array elements ,[object Object],[object Object],Background Dynamic Pointers Intro to Pointers and Memory in C
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],The array “Variable” Background Dynamic Pointers Intro to Pointers and Memory in C
Dynamic Memory Allocation Background Dynamic Pointers Intro to Pointers and Memory in C
Why do we need to allocate memory? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Intro to Pointers and Memory in C Background Dynamic Pointers
Why do we need to allocate memory? ,[object Object],[object Object],Intro to Pointers and Memory in C Background Dynamic Pointers
Why do we need to allocate memory? ,[object Object],[object Object],[object Object],[object Object],Intro to Pointers and Memory in C Background Dynamic Pointers
How do we allocate memory? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Intro to Pointers and Memory in C Background Dynamic Pointers
The heap ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Intro to Pointers and Memory in C Background Dynamic Pointers
Allocated memory must be freed! ,[object Object],[object Object],[object Object],[object Object],[object Object],Intro to Pointers and Memory in C Background Dynamic Pointers
Malloc/Free example Intro to Pointers and Memory in C Background Dynamic Pointers
free() caveats ,[object Object],[object Object],[object Object],Intro to Pointers and Memory in C Background Dynamic Pointers
free() caveats ,[object Object],[object Object],Intro to Pointers and Memory in C Background Dynamic Pointers
free() caveats ,[object Object],[object Object],Intro to Pointers and Memory in C Background Dynamic Pointers
Summary of covered topics ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Intro to Pointers and Memory in C Background Dynamic Pointers
Questions?
Backup materials Intro to Pointers and Memory in C
Topics not covered ,[object Object],[object Object],[object Object],[object Object]
Do not rely on stack organization!
Pointers can be compared
Caveats – Dangling references ,[object Object]
Null-terminated strings ,[object Object],[object Object],[object Object],[object Object],[object Object]
Global Variables ,[object Object],[object Object],[object Object],[object Object],[object Object]

Weitere ähnliche Inhalte

Was ist angesagt?

Chapter 2 instructions language of the computer
Chapter 2 instructions language of the computerChapter 2 instructions language of the computer
Chapter 2 instructions language of the computer
BATMUNHMUNHZAYA
 

Was ist angesagt? (20)

Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
 
Pointer in c program
Pointer in c programPointer in c program
Pointer in c program
 
Implementation of Pipe in Linux
Implementation of Pipe in LinuxImplementation of Pipe in Linux
Implementation of Pipe in Linux
 
Processes and threads
Processes and threadsProcesses and threads
Processes and threads
 
Lesson 2 Understanding Linux File System
Lesson 2 Understanding Linux File SystemLesson 2 Understanding Linux File System
Lesson 2 Understanding Linux File System
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
 
Instruction format
Instruction formatInstruction format
Instruction format
 
Distributed Operating System_1
Distributed Operating System_1Distributed Operating System_1
Distributed Operating System_1
 
Semaphore
SemaphoreSemaphore
Semaphore
 
Chapter 2 instructions language of the computer
Chapter 2 instructions language of the computerChapter 2 instructions language of the computer
Chapter 2 instructions language of the computer
 
Pointers in c language
Pointers in c languagePointers in c language
Pointers in c language
 
linux file sysytem& input and output
linux file sysytem& input and outputlinux file sysytem& input and output
linux file sysytem& input and output
 
Semaphores
SemaphoresSemaphores
Semaphores
 
Ipc in linux
Ipc in linuxIpc in linux
Ipc in linux
 
Scheduling algorithms
Scheduling algorithmsScheduling algorithms
Scheduling algorithms
 
File Management in Operating System
File Management in Operating SystemFile Management in Operating System
File Management in Operating System
 
Cpu scheduling in operating System.
Cpu scheduling in operating System.Cpu scheduling in operating System.
Cpu scheduling in operating System.
 
16 dynamic-memory-allocation
16 dynamic-memory-allocation16 dynamic-memory-allocation
16 dynamic-memory-allocation
 
Memory organization
Memory organizationMemory organization
Memory organization
 

Andere mochten auch

Low Level Exploits
Low Level ExploitsLow Level Exploits
Low Level Exploits
hughpearse
 
Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)
tech4us
 

Andere mochten auch (20)

Smashing The Stack
Smashing The StackSmashing The Stack
Smashing The Stack
 
Introduction to Linux Exploit Development
Introduction to Linux Exploit DevelopmentIntroduction to Linux Exploit Development
Introduction to Linux Exploit Development
 
Exploit techniques and mitigation
Exploit techniques and mitigationExploit techniques and mitigation
Exploit techniques and mitigation
 
Low Level Exploits
Low Level ExploitsLow Level Exploits
Low Level Exploits
 
Debugging Applications with GNU Debugger
Debugging Applications with GNU DebuggerDebugging Applications with GNU Debugger
Debugging Applications with GNU Debugger
 
The Stack Frame
The Stack FrameThe Stack Frame
The Stack Frame
 
How Functions Work
How Functions WorkHow Functions Work
How Functions Work
 
C++ Pointers
C++ PointersC++ Pointers
C++ Pointers
 
Insecure coding in C (and C++)
Insecure coding in C (and C++)Insecure coding in C (and C++)
Insecure coding in C (and C++)
 
Ctf hello,world!
Ctf hello,world! Ctf hello,world!
Ctf hello,world!
 
C Pointers
C PointersC Pointers
C Pointers
 
Basic of Exploitation
Basic of ExploitationBasic of Exploitation
Basic of Exploitation
 
Objective-c memory
Objective-c memoryObjective-c memory
Objective-c memory
 
Pointers in c++ by minal
Pointers in c++ by minalPointers in c++ by minal
Pointers in c++ by minal
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)
 
pointers
pointerspointers
pointers
 
Pf cs102 programming-9 [pointers]
Pf cs102 programming-9 [pointers]Pf cs102 programming-9 [pointers]
Pf cs102 programming-9 [pointers]
 
C programming - Pointer and DMA
C programming - Pointer and DMAC programming - Pointer and DMA
C programming - Pointer and DMA
 
Pointers
PointersPointers
Pointers
 

Ähnlich wie Introduction to pointers and memory management in C

(246431835) instruction set principles (2) (1)
(246431835) instruction set principles (2) (1)(246431835) instruction set principles (2) (1)
(246431835) instruction set principles (2) (1)
Alveena Saleem
 
10 Instruction Sets Characteristics
10  Instruction  Sets Characteristics10  Instruction  Sets Characteristics
10 Instruction Sets Characteristics
Jeanie Delos Arcos
 

Ähnlich wie Introduction to pointers and memory management in C (20)

Programming in C.pptx
Programming in C.pptxProgramming in C.pptx
Programming in C.pptx
 
Basic Concepts of C Language.pptx
Basic Concepts of C Language.pptxBasic Concepts of C Language.pptx
Basic Concepts of C Language.pptx
 
Programming construction tools
Programming construction toolsProgramming construction tools
Programming construction tools
 
C programming basic concepts of mahi.pptx
C programming basic concepts of mahi.pptxC programming basic concepts of mahi.pptx
C programming basic concepts of mahi.pptx
 
Theory1&2
Theory1&2Theory1&2
Theory1&2
 
Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++
 
The best every notes on c language is here check it out
The best every notes on c language is here check it outThe best every notes on c language is here check it out
The best every notes on c language is here check it out
 
Wk1to4
Wk1to4Wk1to4
Wk1to4
 
Lesson 3.2 data types for memory location
Lesson 3.2 data types for memory locationLesson 3.2 data types for memory location
Lesson 3.2 data types for memory location
 
Ch02
Ch02Ch02
Ch02
 
Advanced+pointers
Advanced+pointersAdvanced+pointers
Advanced+pointers
 
Pointers
PointersPointers
Pointers
 
Interview Questions For C Language .pptx
Interview Questions For C Language .pptxInterview Questions For C Language .pptx
Interview Questions For C Language .pptx
 
C programming language
C programming languageC programming language
C programming language
 
5 introduction-to-c
5 introduction-to-c5 introduction-to-c
5 introduction-to-c
 
CProgrammingTutorial
CProgrammingTutorialCProgrammingTutorial
CProgrammingTutorial
 
C programming tutorial for Beginner
C programming tutorial for BeginnerC programming tutorial for Beginner
C programming tutorial for Beginner
 
(246431835) instruction set principles (2) (1)
(246431835) instruction set principles (2) (1)(246431835) instruction set principles (2) (1)
(246431835) instruction set principles (2) (1)
 
Interview Questions For C Language
Interview Questions For C Language Interview Questions For C Language
Interview Questions For C Language
 
10 Instruction Sets Characteristics
10  Instruction  Sets Characteristics10  Instruction  Sets Characteristics
10 Instruction Sets Characteristics
 

Kürzlich hochgeladen

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Kürzlich hochgeladen (20)

Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 

Introduction to pointers and memory management in C