“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.

Slides:



Advertisements
Similar presentations
Course Introduction Bryce Boe 2013/06/25 CS24, Summer 2013 C.
Advertisements

Dynamic Memory Allocation I Topics Basic representation and alignment (mainly for static memory allocation, main concepts carry over to dynamic memory.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Chapter 7 Process Environment Chien-Chung Shen CIS, UD
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
More Pointers Write a program that: –Calls a function to input an integer value –The above function calls another function that will double the input value.
Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015 Some slides.
Informática II Prof. Dr. Gustavo Patiño MJ
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
CSSE 332 Functions, Pointers in C. 2 Functions - why and how ? If a problem is large If a problem is large Modularization – easier to: Modularization.
David Notkin Autumn 2009 CSE303 Lecture 10 "If it weren't for C, we'd be writing programs in BASI, PASAL, and OBOL."
Stack buffer overflow
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
Memory Image of Running Programs Executable file on disk, running program in memory, activation record, C-style and Pascal-style parameter passing.
CS 536 Spring Run-time organization Lecture 19.
C and Data Structures Baojian Hua
Unix Process Environment. main Function A C program starts execution with a function called main. The prototype for the main function is: int main (int.
Run-time Environment and Program Organization
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Memory Layout C and Data Structures Baojian Hua
15213 C Primer 17 September Outline Overview comparison of C and Java Good evening Preprocessor Command line arguments Arrays and structures Pointers.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
Linked Structures, Project 1: Linked List Bryce Boe 2013/07/11 CS24, Summer 2013 C.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Runtime Environments Compiler Construction Chapter 7.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
1 Program Layout in memory –Code –Data Global variables –Stack Local variables & function Invocation Frames –Heap Dynamically allocated memory Today’s.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
File I/O, Project 1: List ADT Bryce Boe 2013/07/02 CS24, Summer 2013 C.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, Summer 2013 C.
What is exactly Exploit writing?  Writing a piece of code which is capable of exploit the vulnerability in the target software.
Functions & Pointers in C Jordan Erenrich
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
Separate Compilation Bryce Boe 2013/10/09 CS24, Fall 2013.
Fall 2004CS-183 Dr. Mark L. Hornick 1 C++ Arrays C++ (like Java) supports the concept of collections – mechanisms to sort and manipulate many instances.
What we will cover A crash course in the basics of C “Teach yourself C in 21 days”
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
1 C Basics Monday, August 30, 2010 CS 241. Announcements MP1, a short machine problem, will be released today. Due: Tuesday, Sept. 7 th at 11:59pm via.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
Variables Bryce Boe 2012/09/05 CS32, Summer 2012 B.
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
C Primer.
Day 03 Introduction to C.
Command Line Arguments
CSE 303 Concepts and Tools for Software Development
Run-time organization
Day 03 Introduction to C.
Programming Languages and Paradigms
CSC 253 Lecture 8.
CSC 253 Lecture 8.
Pointers, Dynamic Data, and Reference Types
Stack buffer overflow.
Memory Allocation CS 217.
Understanding Program Address Space
7. Pointers, Dynamic Memory
C (and C++) Pointers April 4, 2019.
15213 C Primer 17 September 2002.
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill

Memory Layout Bryce Boe 2013/10/07 CS24, Fall 2013

Outline Lab 2 Notes Wednesday Recap Memory Layout

Lab 2 Notes Don’t use functions from string.h – No strlen, strcpy, etc – DO: write these functions yourself Don’t allocate a fixed size for the new string – E.g., malloc(256) – DO: Allocate the exact number of bytes you require Partner up

WEDNESDAY RECAP

What are the sizes of the following primitive types (x86– Intel 32 bit)? int float double char void* int* char*

How large is the structure? struct MyStructA { int a, b; float f; };

How large is the structure? struct MyStructB { char msg[20]; char *another_message; };

How large is the structure? struct MyStructC { char *message; struct MyStructA a; };

How large is the structure? struct MyStructD { char *message; struct MyStructD *next; };

MEMORY LAYOUT

View from three Levels The address space of a process Function activation records on the stack Data within an activation record

Simplified process’s address space STACK HEAP CODE+ 0xFFFFFFFF 0x

Data Segments Code(+) – Program code – Initialization data, global and static variables Heap – Memory chunks allocated via malloc Stack – Function activation records

Creating and destroying activation records Program starts with main main calls strsafeconcat strsafeconcat calls strlength strlength returns strsafeconcat calls strlength strlength returns strsafeconcat returns main strsafeconcat strlength top of stack High Memory

Simplified function activation records Stores values passed into the function as parameters Stores the function’s local variables

How many bytes is the simplified activation record? int main(int argc, char *argv[]) { char buf[8]; for (int i = 0; i < argc; ++i) buf[i] = argv[i][0]; }

main’s simplified activation record argc (int) 4 bytes argc (int) 4 bytes argv (pointer) 4 bytes argv (pointer) 4 bytes buf (char array) 8 bytes buf (char array) 8 bytes i (int) 4 bytes i (int) 4 bytes Total: 20 bytes data stored

Think about it int main() { char msg[] = “hello”; char buf[] = “ ”; char msg2[] = “world”; } What value does buf[8] hold? What about msg[7]?

Similar but different int main() { int x = 0xDEADBEEF; char buf[] = “ ”; } What value does buf[8] hold?

Inspecting addresses in a program

For Wednesday Finish reading chapter 1 in the text book