Intermediate Programming, Spring 2017*

Similar documents
Intermediate Programming, Spring 2017*

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

SYSC 2006 C Winter 2012

Intermediate Programming, Spring 2017*

CS 11 C track: lecture 5

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

o Code, executable, and process o Main memory vs. virtual memory

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

Dynamic Memory Allocation

Intermediate Programming, Spring 2017*

CSC 1600 Memory Layout for Unix Processes"

CS 61c: Great Ideas in Computer Architecture

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

CS61C : Machine Structures

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

Understanding Pointers

CS61C : Machine Structures

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

First of all, it is a variable, just like other variables you studied

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

ch = argv[i][++j]; /* why does ++j but j++ does not? */

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

POINTER AND ARRAY SUNU WIBIRAMA

CS61C : Machine Structures

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

Intermediate Programming, Spring 2017*

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

ECE 15B COMPUTER ORGANIZATION

Lecture 2: C Programm

Arrays and Pointers in C. Alan L. Cox

Systems Programming and Computer Architecture ( )

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

Heap Arrays and Linked Lists. Steven R. Bagley

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

Lab 3. Pointers Programming Lab (Using C) XU Silei

CS 137 Part 5. Pointers, Arrays, Malloc, Variable Sized Arrays, Vectors. October 25th, 2017

In Java we have the keyword null, which is the value of an uninitialized reference type

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Dynamic memory allocation (malloc)

Lecture 04 Introduction to pointers

Arrays, Pointers and Memory Management

Week 9 Part 1. Kyle Dewey. Tuesday, August 28, 12

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

Pointers. 10/5/07 Pointers 1

CA31-1K DIS. Pointers. TA: You Lu

How about them A s!! Go Oaktown!! CS61C - Machine Structures. Lecture 4 C Structures Memory Management Dan Garcia.

Arrays and Pointers (part 1)

Number Review. Lecture #3 More C intro, C Strings, Arrays, & Malloc Variables. Clarification about counting down

[0569] p 0318 garbage

Homework #3 CS2255 Fall 2012

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

Intermediate Programming, Spring 2017*

Reference slides! C Strings! A string in C is just an array of characters.!!!char string[] = "abc";! How do you tell how long a string is?!

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

Heap Arrays. Steven R. Bagley

Kurt Schmidt. October 30, 2018

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

Outline. Pointers arithme.c and others Func.ons & pointers

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

CS 222: Pointers and Manual Memory Management

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS ) Pointers. Chapter No 7

CS61C : Machine Structures

Dynamic Memory Allocation

C Syntax Arrays and Loops Math Strings Structures Pointers File I/O. Final Review CS Prof. Jonathan Ventura. Prof. Jonathan Ventura Final Review

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

PRINCIPLES OF OPERATING SYSTEMS

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012

Memory, Arrays & Pointers

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

by Pearson Education, Inc. All Rights Reserved.

THE GOOD, BAD AND UGLY ABOUT POINTERS. Problem Solving with Computers-I

Reference slides! Garcia, Fall 2011 UCB! CS61C L04 Introduction to C (pt 2) (1)!

Pointers and File Handling

Lecture 7: Procedures and Program Execution Preview

Lecture Notes on Types in C

Class Information ANNOUCEMENTS

Language comparison. C has pointers. Java has references. C++ has pointers and references

Pointers, Arrays, Memory: AKA the cause of those Segfaults

Quick review pointer basics (KR ch )

CSC C69: OPERATING SYSTEMS

Introduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

Arrays and Pointers (part 1)

CSCI 171 Chapter Outlines

Arrays and Memory Management

Dynamic memory allocation

C Tutorial. Pointers, Dynamic Memory allocation, Valgrind, Makefile - Abhishek Yeluri and Yashwant Reddy Virupaksha

Transcription:

600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general and you should not follow my lead on this.

Anouncements Homework 2 is due Wednesday

Outline Function calls and stack frames Pointers Using statically-allocated arrays Dynamic memory allocation basics

Writing a swap function in C Q: Why doesn t this code work? void swap( int x, int y ) int temp; temp = x; x = y; y = temp; int a = 1, b =2; swap( a,b ); printf( %d %d\n, a, b ); >>./a.out 1 2 >>

Local variables Each function call gets a new stack frame pushed onto the stack in memory for storing Local variables Copies of function parameters Other miscellany Once the current function call returns, the stack frame is popped off the stack, and execution returns to the calling function (so the current function s frame is always on top)

Local variables Frames get pushed on the stack when the function is called x: 4 bytes And they are popped y: 4 bytes off when the function temp: 4 bytes returns swap stack frame a: 4 bytes b: 4 bytes main stack frame void swap( int x, int y ) int temp = x; x = y; y = temp; int a = 1, b =2; swap( a,b ); printf( %d %d\n, a, b );

Writing a swap function in C Adjusting values in swap s stack frame has no impact on values x: 4 bytes in main s stack frame y: 4 bytes What we need is to give the swap function access to main s stack frame This is a job for pointers! temp: 4 bytes swap stack frame a: 4 bytes b: 4 bytes main stack frame void swap( int x, int y ) int temp = x; x = y; y = temp; int a = 1, b =2; swap( a,b ); printf( %d %d\n, a, b ); >>./a.out 1 2 >>

Pointers A pointer is a variable that contains the address of a variable. Every pointer points to a specific data type (except a pointer to void, more on that later) Declare a pointer using type of variable it will point to, and a * : int *ip is a pointer to an int double *dp is a pointer to a double etc. Operations related to pointers address-of operator &: returns address of whatever follows it dereferencing operator *: returns value being pointed to

Pointers A pointer is a variable that contains the address of a variable. Every pointer points to a specific data type (except a pointer to void, more on that later) Declare a pointer using type of variable it will point to, and a * : int *ip is a pointer to an int double *dp is a pointer to a double etc. Operations related to pointers address-of operator &: returns address of whatever follows it Note: dereferencing operator *: returns value being pointed to When declaring a pointer, the * needs to be associated with the variable name, not the type int * a, b; declares a pointer to an int called a and an int called b int * a, * b; declares a pointer to an int called a and a pointer to an int called b

Pointers Every pointer points to a specific data type (except a pointer to void, but more about that intlater) x = 1, y =2; // ints Declare a pointer using type of variable it will int point *ip; to, and a // *: a pointer to an int ip = &x; // that points to x y = *ip; // y has the value of // what ip points to (x) *ip = 0; // what ip points to (x) // has value 0 address-of operator &: returns address of whatever printf( %d follows %d\n it, x, y ); dereferencing operator *: returns value being return pointed 0; to A pointer is a variable that contains the address of a variable. int *ip is a pointer to an int double *dp is a pointer to a double etc. Operations related to pointers >>./a.out 0 1 >>

A working swap function The call in main is now swap( &a, &b ) since we need to send in the addresses of a and b Pointer arguments allow a called function to access and modify values in the calling function void swap( int* px, int* py ) int temp = *px; *px = *py; *py = temp; int a = 1, b =2; swap( &a,&b ); printf( %d %d\n, a, b ); >>./a.out 2 1 >>

A working swap function The call in main is now swap( &a, &b ) since we need to send in the addresses of a and b Pointer arguments allow a called function to access and modify values in the calling function a: 4 bytes b: 4 bytes main stack frame void swap( int* px, int* py ) int temp = *px; *px = *py; *py = temp; int a = 1, b =2; swap( &a,&b ); printf( %d %d\n, a, b );

A working swap function The call in main is now swap( &a, &b ) since we need to send in the addresses of a and b Pointer arguments allow a called function to access and modify values in the calling function a: 4 bytes b: 4 bytes main stack frame void swap( int* px, int* py ) int temp = *px; *px = *py; *py = temp; int a = 1, b =2; swap( &a,&b ); printf( %d %d\n, a, b );

A working swap function The call in main is now swap( &a, &b ) since we need to send in the addresses of a and b Pointer arguments allow a called function to access and modify values in the calling function px: 8 bytes py: 8 bytes temp: 4 bytes swap stack frame a: 4 bytes b: 4 bytes main stack frame void swap( int* px, int* py ) int temp = *px; *px = *py; *py = temp; int a = 1, b =2; swap( &a,&b ); printf( %d %d\n, a, b );

Using & and * If ip points to int x. Then *ip can be used anywhere that x makes sense: printf( %d\n, *ip ) printf( %d\n, x ) Unary ops & and * bind more tightly than binary arithmetic ops *ip += 1 x += 1 y = *ip + 1 y = x+1 [WARNING] ++*ip is the same as ++x *ip++ means something else (unary operators associate from right to left)

Pointer arithmetic A pointer is an integer value indicating a location in memory. We can add /subtract integers from a pointer b = a+2; b -= 3; b++; etc. int a[] = 1, 2, 3, 4 ; int* b = a+2;

Pointer arithmetic A pointer is an integer value indicating a location in memory. We can add /subtract integers from a pointer Q: What is the value of b? int a[] = 1, 2, 3, 4 ; int* b = a+2;

Pointer arithmetic A pointer is an integer value indicating a location in memory. We can add /subtract integers from a pointer Q: What is the value of b? A: b is the address of the integer two in from the start of the array Note: The int two elements in from the start of the array is 8 bytes away in memory Because the type of the pointer is known the compiler automatically deduces that two int lengths corresponds to 8 bytes int a[] = 1, 2, 3, 4 ; int* b = a+2; printf( %d %d\n, *a, *b ); >>./a.out 1 3 >>

Pointer arithmetic A pointer is an integer value indicating a location in memory. We can add /subtract integers from a pointer Q: What is the value of b? A: b is the address of the integer two in from the start of the array int a[] = 1, 2, 3, 4 ; int* b = a+2; void *_a =a, *_b = b; printf( %d\t, b-a ); printf( %d\n, _b-_a ); >>./a.out 2 8 >> Note: The int two elements in from the start of the array is 8 bytes away in memory Because the type of the pointer is known the compiler automatically deduces that two int lengths corresponds to 8 bytes A pointer of type void* is treated as a raw memory address (w/o size information)

Pointer arithmetic A pointer is an integer value indicating a location in memory. We can add /subtract integers from a pointer Q: So what does *b++ mean? A: It s a combination of four instruction: 1. Increment the pointer ip, 2. Return the old pointer s value, * 3. Dereference that 4. Set it to zero int a[] = 1, 2, 3, 4 ; int* b = a+2; *b++ = 0; printf( %d %d %d %d : %d\n, a[0], a[1], a[2], a[3], *b ); >>./a.out 1 2 0 4 : 4 >> * Recall that post-increment/decrement returns the old value

Pointer access We can access a pointer by dereferencing printf( %d\n, *b ); We can access array elements with [] printf( %d\n, b[0] ); Since pointers and arrays are the same these are the same operations! int a[] = 1, 2, 3, 4 ; int* b = a+2; printf( %d\n, *b ); printf( %d\n, b[0] ); More generally *(b+k) is the same as b[k] for any integer k >>./a.out 3 3 >>

Arrays within a stack frame Recall: When arrays are passed, the address of the array is copied, not the values Since main s frame is on the stack while function changefirst is running, the address is valid, and changefirst can access it s values a[]: 20 bytes main stack frame void changefirst( int* x ) x[0] = 99; int a[] = 1, 2, 3, 4, 5 ; changefirst( a ); printf( %d %d %d\n, a[0], a[1], a[2] );

Arrays within a stack frame Recall: When arrays are passed, the address of the array is copied, not the values Since main s frame is on the stack while function changefirst is running, the address is valid, and changefirst can access it s values x: 8 bytes swap stack frame a[]: 20 bytes main stack frame void changefirst( int* x ) x[0] = 99; int a[] = 1, 2, 3, 4, 5 ; changefirst( a ); printf( %d %d %d\n, a[0], a[1], a[2] ); >>./a.out 99 2 3 >>

Returning an array from a function However: When arrays are returned, the array s address is returned, not the array s values If it s the address of an array on getarray s stack frame, it won t mean anything after getarray returns int* getarray( unsigned int sz ) int a[sz]; for( int i=0 ; i<sz ; i++ ) a[i] = 1; return a; int* list = NULL; list = getarray( 10 ); for( int i=0 ; i<10 ; i++ ) printf( "%d ", list[i] ); printf( \n ); >>./a.out segmentation fault (core dumped) >>

Returning an array from a function However: When arrays are returned, the array s address is returned, not the array s values If it s the address of an array on getarray s stack frame, it won t mean anything after getarray returns list: 8 bytes main stack frame int* getarray( unsigned int sz ) int a[sz]; for( int i=0 ; i<sz ; i++ ) a[i] = 1; return a; int* list = NULL; list = getarray( 10 ); for( int i=0 ; i<10 ; i++ ) printf( "%d ", list[i] ); printf( \n );

Returning an array from a function However: When arrays are returned, the array s address is returned, not the array s values If it s the address of an array on getarray s stack frame, it won t mean anything after getarray returns []: 4*sz bytes swap stack frame list: 8 bytes main stack frame int* getarray( unsigned int sz ) int a[sz]; for( int i=0 ; i<sz ; i++ ) a[i] = 1; return a; int* list; list = getarray( 10 ); for( int i=0 ; i<10 ; i++ ) printf( "%d ", list[i] ); printf( \n );

Returning an array from a function However: When arrays are returned, the array s address is returned, not the array s values If it s the address of an array on getarray s stack frame, it won t mean anything after getarray returns []: 4*sz bytes swap stack list: 8 frame bytes? main stack frame int* getarray( unsigned int sz ) int a[sz]; for( int i=0 ; i<sz ; i++ ) a[i] = 1; return a; int* list; list = getarray( 10 ); for( int i=0 ; i<10 ; i++ ) printf( "%d ", list[i] ); printf( \n );

Allocating a very large array Stack frames have a limited size int list[1<<30]; // ~10 9 printf( %d\n, list[0] ); >>./a.out segmentation fault (core dumped) >>

Limitations of arrays allocated in a stack frame Arrays ( statitcally ) allocated on the stack frame have limitations Arrays created on a called function s frame can t be accessed by calling function Returning a is meaningless int* getarray( unsigned int sz ) int a[sz]; for( int i=0 ; i<sz ; i++ ) a[i] = 1; return a;

Limitations of arrays allocated in a stack frame Arrays ( statitcally ) allocated on the stack frame have limitations Arrays created on a called function s frame can t be accessed by calling function Size is limited by the size of stack frame (much less than available RAM) sz cannot be too large int* getarray( unsigned int sz ) int a[sz]; for( int i=0 ; i<sz ; i++ ) a[i] = 1; return a;

Limitations of arrays allocated in a stack frame Arrays ( statitcally ) allocated on the stack frame have limitations Arrays created on a called function s frame can t be accessed by calling function Size is limited by the size of stack frame (much less than available RAM) Prior to C99, could only allocate constant size arrays on the frame stack (Allocation on the stack was truly static ) Declaring int a[sz]; was illegal int* getarray( unsigned int sz ) int a[sz]; for( int i=0 ; i<sz ; i++ ) a[i] = 1; return a;

Dynamically-allocating memory Dynamically-allocated memory: Is located on the heap a part of memory separate from the stack Lives as long as we like (until the entire program ends) We don t lose access to it when function call returns We can return it to a calling function! We are responsible for managing the memory (and cleaning it up when we re done) Is not subject to frame stack s size limitations because it isn t part of the stack Has a size that can be decided (dynamically) at run time

Dynamically-allocating memory heap void foo( int x ) // do something with x int a = 1; int b = foo( a );

Dynamically-allocating memory heap a: 4 bytes b: 4 bytes main stack frame void foo( int x ) // do something with x int a = 1; int b = foo( a );

Dynamically-allocating memory heap x: 4 bytes swap stack frame a: 4 bytes b: 4 bytes main stack frame void foo( int x ) // do something with x int a = 1; int b = foo( a );

Dynamically-allocating memory heap a: 4 bytes b: 4 bytes main stack frame void foo( int x ) // do something with x int a = 1; int b = foo( a );

Dynamically-allocating memory heap void foo( int x ) // do something with x int a = 1; int b = foo( a );

Dynamically-allocating memory We are responsible for managing the memory Memory should be deallocated when it is no longer needed Allocated memory is not available to other programs/users until we deallocate it Failing to deallocate memory is the cause of memory leaks

Dynamically-allocating memory (single) We allocate memory using the malloc command (in stdlib.h) void* malloc( size_t ); Input: how much memory (in bytes) to allocate size_t is an unsigned integer type: 4 bytes on 32-bit machines 8 bytes on 64-bit machines Output: the location (on the heap) of the memory malloc doesn t need to know what you re going to store just how much memory you will need int *ip = malloc( sizeof( int ) );

Dynamically-allocating memory (single) We allocate memory using the malloc command (in stdlib.h) void* malloc( size_t ); Check that allocation succeeded int *ip = malloc( sizeof( int ) ); if( ip==null ) fprintf( stderr, \n ); // do something

Dynamically-allocating memory (single) We allocate memory using the malloc command (in stdlib.h) void* malloc( size_t ); Check that allocation succeeded After allocation with malloc, memory cannot be assumed to be initialized int *ip = malloc( sizeof( int ) ); if( ip==null ) fprintf( stderr, \n ); // do something *ip = 0;

Dynamically-allocating memory (single) We allocate memory using the malloc command (in stdlib.h) void* malloc( size_t ); Check that allocation succeeded After allocation with malloc, memory cannot be assumed to be initialized When usage of dynamically-allocated int is complete, deallocate using free void free( void* ptr ); Input: address of the memory on the heap int *ip = malloc( sizeof( int ) ); if( ip==null ) fprintf( stderr, \n ); // do something *ip = 0; free( ip );

Dynamically-allocating memory (single) We allocate memory using the malloc command (in stdlib.h) void* malloc( size_t ); Check that allocation succeeded After allocation with malloc, memory cannot be assumed to be initialized When usage of dynamically-allocated int is complete, deallocate using free void free( void* ptr ); It s good practice to set the pointer to NULL to avoid accidental use int *ip = malloc( sizeof( int ) ); if( ip==null ) fprintf( stderr, \n ); // do something *ip = 0; if( ip!=null ) free( ip ); ip = NULL;

Dynamically-allocating memory (multiple) We allocate memory using the malloc command (in stdlib.h) void* malloc( size_t ); Check that allocation succeeded After allocation with malloc, memory cannot be assumed to be initialized When usage of dynamically-allocated int is complete, deallocate using free void free( void* ptr ); It s good practice to set the pointer to NULL to avoid accidental use int *ip = malloc( sizeof( int ) * sz ); if( ip==null ) fprintf( stderr, \n ); // do something for( int i=0 ;i<sz ; i++ ) ip[i] = 0; if( ip!=null ) free( ip ); ip = NULL;

Deallocation Deallocation does not have to happen in the same function where allocation occurred... But it does have to happen somewhere! Otherwise you can get a memory leak Only the last allocation was deallocated! #include <stdlib.h> int* getarray( unsigned int sz ) int *a = malloc( sizeof(int) * sz ); for( int i=0 ; i<sz ; i++ ) a[i] = 1; return a; int* list; for( int i=0 ; i<100000 ; i++ ) list = getarray( 100000 ); // do something with list free( list );

Deallocation Deallocation does not have to happen in the same function where allocation occurred... But it does have to happen somewhere! Otherwise you can get a memory leak #include <stdlib.h> int* getarray( unsigned int sz ) int *a = malloc( sizeof(int) * sz ); for( int i=0 ; i<sz ; i++ ) a[i] = 1; return a; int* list; for( int i=0 ; i<100000 ; i++ ) list = getarray( 100000 ); // do something with list free( list );

Dynamically-allocating memory int* getarray( unsigned int sz ) int* a = malloc( sizeof(int) * sz ); for( int i=0 ; i<sz ; i++ ) a[i] = 1; return a; heap list: 8 bytes main stack frame int* list; list = getarray( 10 ); for( int i=0 ; i<10 ; i++ ) printf( "%d ", list[i] ); printf( \n );

Dynamically-allocating memory []: 4 * sz bytes heap a: 8 bytes swap stack frame list: 8 bytes main stack frame int* getarray( unsigned int sz ) int* a = malloc( sizeof(int) * sz ); for( int i=0 ; i<sz ; i++ ) a[i] = 1; return a; int* list; list = getarray( 10 ); for( int i=0 ; i<10 ; i++ ) printf( "%d ", list[i] ); printf( \n );

Dynamically-allocating memory []: 4 * sz bytes heap a: 8 bytes swap stack frame list: 8 bytes main stack frame int* getarray( unsigned int sz ) int* a = malloc( sizeof(int) * sz ); for( int i=0 ; i<sz ; i++ ) a[i] = 1; return a; int* list; list = getarray( 10 ); for( int i=0 ; i<10 ; i++ ) printf( "%d ", list[i] ); printf( \n );

Dynamically-allocating memory Q: What s wrong with this code? A: We didn t deallocate int* getarray( unsigned int sz ) int* a = malloc( sizeof(int) * sz ); for( int i=0 ; i<sz ; i++ ) a[i] = 1; return a; int* list; list = getarray( 10 ); for( int i=0 ; i<10 ; i++ ) printf( "%d ", list[i] ); printf( \n ); return >>./a.out 0; 1 1 1 1 1 1 1 1 1 1 >>