Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct



Similar documents
The programming language C. sws1 1

5 Arrays and Pointers

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Introduction to Data Structures

Stacks. Linear data structures

Illustration 1: Diagram of program function and data flow

Object Oriented Software Design II

Sources: On the Web: Slides will be available on:

CS 106 Introduction to Computer Science I

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

Module 816. File Management in C. M. Campbell 1993 Deakin University

Semantic Analysis: Types and Type Checking

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

CS 241 Data Organization Coding Standards

The C Programming Language course syllabus associate level

DNA Data and Program Representation. Alexandre David

Chapter 9, More SQL: Assertions, Views, and Programming Techniques

arrays C Programming Language - Arrays

Lecture Data Types and Types of a Language

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

How To Write Portable Programs In C

Number Representation

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Explain the relationship between a class and an object. Which is general and which is specific?

Tutorial on C Language Programming

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

Beyond the Mouse A Short Course on Programming

C Dynamic Data Structures. University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell

MPLAB TM C30 Managed PSV Pointers. Beta support included with MPLAB C30 V3.00

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Objective-C Tutorial

Creating Database Tables in Microsoft SQL Server

What is COM/DCOM. Distributed Object Systems 4 COM/DCOM. COM vs Corba 1. COM vs. Corba 2. Multiple inheritance vs multiple interfaces

C++ INTERVIEW QUESTIONS

C++ Language Tutorial

Variables, Constants, and Data Types

One Dimension Array: Declaring a fixed-array, if array-name is the name of an array

A deeper look at Inline functions

J a v a Quiz (Unit 3, Test 0 Practice)

SUGI 29 Coders' Corner

Chapter 7D The Java Virtual Machine

Introduction to SQL for Data Scientists

Assembly Language: Function Calls" Jennifer Rexford!

C++ Programming Language

IPC. Semaphores were chosen for synchronisation (out of several options).

Format string exploitation on windows Using Immunity Debugger / Python. By Abysssec Inc

Java Interview Questions and Answers

Passing 1D arrays to functions.

7.1 Our Current Model

Introduction to Programming II Winter, 2014 Assignment 2

1 Description of The Simpletron

Informatica e Sistemi in Tempo Reale

Pemrograman Dasar. Basic Elements Of Java

Chapter 3. Input and output. 3.1 The System class

8.5. <summary> Cppcheck addons Using Cppcheck addons Where to find some Cppcheck addons

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY Operating System Engineering: Fall 2005

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

2 ASCII TABLE (DOS) 3 ASCII TABLE (Window)

Lecture 12 Doubly Linked Lists (with Recursion)

IC4 Programmer s Manual

Lecture 22: C Programming 4 Embedded Systems

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Software security. Buffer overflow attacks SQL injections. Lecture 11 EIT060 Computer Security

Comp151. Definitions & Declarations

APPLICATION PROGRAMMING INTERFACE

Glossary of Object Oriented Terms

It has a parameter list Account(String n, double b) in the creation of an instance of this class.

3 - Lift with Monitors

Lecture 5: Java Fundamentals III

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

Chapter 2: Elements of Java

Computer Programming Tutorial

Quiz I Solutions MASSACHUSETTS INSTITUTE OF TECHNOLOGY Fall Department of Electrical Engineering and Computer Science

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Data Tool Platform SQL Development Tools

SQL Server An Overview

MISRA-C:2012 Standards Model Summary for C / C++

Special Topics for Embedded Programming. Reference: The C Programming Language by Kernighan & Ritchie

C++ Essentials. Sharam Hekmat PragSoft Corporation

Chapter 13 Storage classes

Technical Support Bulletin Nr.18 Modbus Tips

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

MACHINE INSTRUCTIONS AND PROGRAMS

Project: Simulated Encrypted File System (SEFS)

Programming languages C

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

No no-argument constructor. No default constructor found

02-201: Programming for Scientists

Transcription:

Dr. Martin O. Steinhauser University of Basel Graduate Lecture Spring Semester 2014 Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct Friday, 7 th March 2014 1 Structures in C This handout is supposed to provide you with a template of how you can visually understand memory assignments done by a struct in C as it was introduced in the lecture. Let s deal with the following two definitions of the structs student and fraction : 1 /* Definition of the structs student and fraction as user - defined data type */ 2 3 typedef struct { 4 char * name ; 5 char suid [8]; 6 int numc ; 7 } student ; 8 9 typedef struct { 10 int nun ; 11 int denom ; 12 } fraction ; By using the very useful command typedef in C you can declare your own data types. In this case you declare two new data types student and fraction which are the structures as seen above. The obvious advantage of this is that now you can declare variables of the type fraction or student anywhere in your code, as if it was a normal standard basic datatype such as e.g. int, i.e. in your code you can now type something like, e.g. 1 /* Sample use of the newly defined struct data types in the code above */ 2 3 fraction bigfraction ; 4 fraction superfraction ; 5 student gradstudent ; 6 fraction * pointtoafraction ; 7 student * pointtoastudent = & gradstudent ; 1

The new variables bigfraction and superfraction in this code snippet are of type fraction which means that they are structures with memory allocated for two integers (i.e. 8 bytes) in the memory stack of the function in which these structs are defined. The new variable gradstudent is of type student, which in turn is a struct that allows memory space for a char-pointer (of type char *), a char-field with 8 entries and an integer (16 bytes altogether). pointtoafraction is declared as a pointer of type fraction (and fraction itself is of type struct with space for two integers), so this pointer now points to the base address of some reserved space (8 bytes) in the memory stack of the function from within this command is issued. The next command declares the pointer pointtoastudent and then initializes it with the address (indicated by the operator &) of struct gradstudent; so now pointtoafraction points to the base address of struct gradstudent. Note that pointtoafraction is still not initialized, not even with NULL, so it is pointing to just some undefined space in memory. This is all right, as long as you don t start de-referencing this pointer and thus overwriting the memory content that happens to be at the address to which pointtoafraction happens to point at. The members of the structs can then be accessed by the usual. and -> operators like this: 1 bigfraction. denom = 34; 2 superfraction. num = 16; 3 pointtoafraction -> denom = 45; /* Because this is a pointer, it has to be \ 4 de - referenced if you want to write something \ 5 in this memory location ; and with structs you do \ 6 this with the -> Operator. 7 */ 8 pointtoastudent -> numc = 4; If you have, for example, defined somewhere else in your code several student name strings (strings are simply fields of characters) in the following way: 1 char student1 [12] = Johnny Depp ; /* Initialized at the definition */ 2 char student2 [17] = Albert Einstein! ; /* Initialized at the definition */ and also: 1 char student3 [17]; /* NOT Initialized at the definition - This means, that later on, \ 2 an assignment like student3 = Some Name is NOT ALLOWED! \ 3 Instead, you have to use the string functions that are \ 4 provided in < string.h> for the handling of strings in C! 5 */ you could then do the following in your code: 1 strcpy ( student3, student2 ); /* Now, student3 is Albert Einstein */ 2 strcpy ( student3, New grad student ); /* Now student3 has been changed */ 3 strcpy ( pointtoastudent - >name, student3 ); /* Now the char pointer of the variable \ 4 pointtoastudent points to the base \ 5 address of student3. 6 */ 2

Hence, the command printf( Student Name: %s %p\n,pointtoastudent->name,pointtoastudent.name); prints out New grad student and the address (with control sign %p) of the memory location to which pointtoastudent.name points to. On my system for example, the printed address by the above printf statement is: 0x7fff57ed9f70 (Try it out on your own system!). 0x in C denotes a memory address, which is always provided as a hexadecimal number. Note the difference in accessing the member and the address of the variable gradstudent here: You need the operator -> for de-referencing the address of the pointer pointtoastudent.name, i.e. to actually access the content of memory at this address. 1.1 Further Remarks By using the backslash \ you can extend C-style comments to several lines without having to close the comment with */. This is often done in the way you see it in the last code snippet above at the end of page 2. As a general rule, you should always comment your code and explain what you are doing this is just good coding practices. The operator = allows you to have spaces between = and the operands it works on, so the following statements are all OK as long as they are in the same command line (i.e. as long as you haven t hit the return key and thus issued a newline command which will be noticed by the compiler): 1 variable =5; 2 variable = 17; 3 variable1 = 1890.56; 4 var = 15; Usually, you use this freedom in writing C-code for aligning all the operators in consecutive lines which makes the code more readable (also good coding practices!) as you can see in the various code snippets in this handout. 2 Structs in Memory Now, let s execute the following 7 commands and draw consecutive pictures of what each one of these commands means in terms of memory which is allocated in the function s stack in which the structure student from page 1 resides. To make it easier for you to follow the consecutive commands I always repeat the definition of the struct student. 3

2.1 Execution of First Command 2.2 Execution of Second Command 4

2.3 Execution of Third Command 2.4 Execution of Fourth Command 5

2.5 Execution of Fifth Command 2.6 Execution of Sixth Command 6

2.7 Execution of Seventh Command Don t let yourself be fooled by the crazy *(char ***) in the last assignment; this is just a reinterpretation (i.e. a cast) of what stands right of it. It is re-interpreted to now de-reference a pointerto-a-pointer-to-a-pointer to a character. If you have really understood the memory pictures including this one: Congratulations! 7