Understanding Dynamic Memory Allocation and Pointer Operations

dynamic memory allocation n.w
1 / 26
Embed
Share

Explore concepts related to dynamic memory allocation, pointer management, dangling pointers, memory leaks, and instruction set architecture in computer programming. Learn about common issues like memory leaks and dangling pointers, as well as the basics of instruction set architecture.

  • Memory Management
  • Pointers
  • Dangling Pointers
  • Instruction Set Architecture
  • Computer Programming

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Dynamic Memory Allocation CSE 30 1

  2. node *list_add(node* head, char *string) { node *new_node=(node*) malloc(sizeof(node)); new_node->value = (char*) malloc(strlen(string)+1); strcpy(new_node->value, string); //Line 4 } What should Line 4 be to achieve the pointer diagram (below)? A. new_node->next =head; B. next=head; C. head=new_node; D. new_node->next =*head; head new_node NULL value next abc

  3. Dangling pointers and memory leaks Dangling pointer: Pointer points to a memory location that no longer exists Memory leaks (tardy free) Memory in heap that can no longer be accessed

  4. Q: Which of the following functions returns a pointer to an object that may not exist in memory? int * f1(int num){ int *mem1 =(int *)malloc(num*sizeof(int)); return(mem1); } int * f2(int num){ int mem2[num]; return(mem2); } A. f1 B. f2 C. Both

  5. Which of the following is an example of a dangling pointer? A. void foo(int bytes) { char *ch =(char *) malloc(bytes); . . . . free (ch); . . . . } B. int * foo(int bytes) { int i=14; . . . . return (&i); } main () { int *p = foo(10); } C. char* foo(int bytes) { char *ch =(char *) malloc(bytes); return (ch); } D. A combination of the above

  6. Memory Leak Example void foo (int bytes) { int *p = (int *) malloc(bytes); }

  7. Lecture 6: Instruction Set Architecture CSE 30: Computer Organization and Systems Programming Summer 2015 Diba Mirza Dept. of Computer Science and Engineering University of California, San Diego 7

  8. Outline 1. 2. Steps in program translation Hardware/Software Interface Preliminaries 1. Instruction Set Architecture General ISA Design (Architecture) Different types of ISA: RISC vs CISC 2. Assembly programmer s view of the system Registers: Special and general purpose Assembly and machine code (program translation detail) Layout of ARM instructions in memory Steps in program execution 3. Basic Types of ARM Assembly Instructions 8

  9. Compile time: What does gcc do? Executable : Equivalent program in machine language % gcc hello.c Source Program in C gcc a.out hello.c 0000 1001 1100 0110 1010 1111 0101 1000 1010 1111 0101 1000 0000 1001 1100 0110 1100 0110 1010 1111 0101 1000 0000 1001 0101 1000 0000 1001 1100 0110 1010 1111 #include <stdio.h> void func1(int a, char *b) { if(a > 0) { *b = a ; } } int main() { .. func1(); printf( \abc ); } 9

  10. Steps in gcc The translation is actually done in a number of steps Linker (ld) Compiler (cpp/cc1) a.out hello.o hello.c hello.s Assembler(as) gcc 10

  11. Include code written by others Code written by others (libraries) can be included ld (linkage editor) merges one or more object files with the relevant libraries to produce a single executable hello.o cpp ld a.out hello.c hello.s cc1 as Library files e.g. math.o: the math library gcc Libraries can be linked statically or dynamically 11

  12. Demo hello.s Assembler Directives A directive is an assembler command that is executed by the assembler. Directives never produce any machine code. Directives are used to assign start of code, data and end of the program Assembly code Symbolic representation of instructions that can be executed by the processor 12

  13. Assembly Language A. Is the binary representation of a program. B. A symbolic representation of machine instructions C. A set of instructions that the machine can directly execute 15

  14. Machine Instructions Q: Which of the following is NOTtrue about machine instructions A. Binary representation of Assembly instructions B. Instructions the machine can directly execute without further translation C. Input to the assembler (as) program of gcc D. The text portion of the program executable 16

  15. What is an Instruction Set Architecture? 1. Everything about h/w that is visible to the s/w and can be manipulated by it via basic machine instructions (System state) Example: Registers: How many? What size? Memory: How to access contents? 17

  16. What is an Instruction Set Architecture? 1. Everything about h/w that is visible to the s/w and can be manipulated by it via basic machine instructions (System state) Example: Registers: How many? What size? Memory: How to access contents? 2. The set of basic machine instructions: A. What each instruction does B. How each instruction is encoded in binary 18

  17. Is the ISA different for different CPUs? Different CPUs implement different sets of instructions. Examples: ARM, Intel x86, IBM/Motorola PowerPC (Macintosh), MIPS, Intel IA32 Two styles of CPU design: RISC (Reduced Instruction Set Computing) CISC (Complex Instruction Set Computing) 19

  18. RISC versus CISC (Historically) Complex Instruction Set Computing e.g x86 Larger instruction set More complicated instructions built into hardware Variable length Multiple clock cycles per instruction Reduced Instruction Set Computing e.g. ARM Small, highly optimized set of instructions Memory accesses are specific instructions Instructions are of the same size and fixed format 20

  19. A = A*B CISC RISC LOAD A, eax LOAD B, ebx PROD eax, ebx STORE ebx, A MULT B, A 21

  20. Translations High-level language program (in C) swap (int v[], int k) { int temp; temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; } Assembly language program (for MIPS) swap: sll $2, $5, 2 add $2, $4, $2 lw $15, 0($2) lw $16, 4($2) sw $16, 0($2) sw $15, 4($2) jr $31 Machine (object, binary) code (for MIPS) 000000 00000 00101 0001000010000000 000000 00100 00010 0001000000100000 . . . C compiler one-to-many one-to-one assembler 22

  21. ARM Assembly Variables: Registers Unlike HLL like C or Java, assembly cannot use variables Why not? Keep Hardware Simple Data is put into a register before it is used for arithmetic, tested, etc. Result is stored in a register (later stored to memory) Benefit: Since registers are directly in hardware, they are very fast 23

  22. C, Java Variables vs. Registers In C (and most High Level Languages) variables declared first and given a type Example: int fahr, celsius; char a, b, c, d, e; Each variable can ONLY represent a value of the type it was declared as (cannot mix and match int and char variables) In Assembly Language, the registers have no type; operation determines how register contents are treated 24

  23. The ARM Register Set Registers: (Very) Small amount of memory inside the CPU Each ARM register is 32 bits wide 30 general purpose registers 6 status registers 1 program counter r0 r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 General Purpose Registers r11 r12 r13 (sp) Special Purpose Registers r14 (lr) r15 (pc) cpsr 25

  24. Steps in program execution 0x4000 Swap: mov r2, r5 0x4004 add r4, r2, #1 0x4008 ldr r10, [r6,r2] 0x400c ldr r11, [r6,r4] 0x4010 mov r1, r10 0x4014 str r11,[r6,r2] 0x4018 str r1, [r6,r4] 0x401c bx lr Instructions Data Stack Memory PC:0x4000 CPU 26

  25. Basic Types of Instructions 1. Arithmetic: Only involves processor and registers compute the sum (or difference) of two registers, store the result in a register move the contents of one register to another 2. Data Transfer Instructions: Interacts with memory load a word from memory into a register store the contents of a register into a memory word 3. Control Transfer Instructions: Change flow of execution jump to another instruction conditional jump (e.g., branch if register == 0) jump to a subroutine 27

  26. What is the min. number of assembly instructions needed to perform the following line of C code ? a = b + c + d - e; A. Single instruction B. Two instructions C. Three instructions D. Four instructions Assume the value of each variable is stored in a register.

Related


More Related Content