cs31s11dis8

cs31s11dis8 - CS31 Introduction to Computer Science I...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Discussion 1H Notes (Week 8, May 20) TA: Brian Choi ([email protected]) Section Webpage: http://www.cs.ucla.edu/~schoi/cs31 Dynamic Allocation of Memory Recall that when you create an array, you must know the exact size of the array you’re creating. You cannot use a variable to specify the size of an array, as follows: int len = 100; double arr[len]; // error! But there are cases when the size of an array cannot be determined a priori . In such cases, the best bet for you is to estimate the largest possible size and use it to create a long array. This does not completely solve the problem, as the actual size you need might still exceed your estimate, or the estimate might be too big that the array is not utilized 100% most of the time. For better memory utilization, you’d like to use just enough memory by computing the size on-the-fly, or dynamically , and allocating exactly that much amount of memory for your array. Such a task is done as follows: int len = 100; double *arr = new double[len] ; This will create an array of doubles, with the first element pointed by the pointer arr . The length of an array can be a variable this time. More precisely, the expression new double[len] creates an array dynamically, and returns the pointer to the first element of the array. We assign the returned pointer to a pointer variable, so we can keep track of the array we just created. Consider the above code. This code compiles and runs, but has an error. Can you tell what the error is? When func() gets called, as we have seen in the previous section, a new stack of memory is allocated for func() . All variables are created in this area, with no exception for arrays. Therefore, the line int arr[10] will create an array of 10 integers in the func() stack, with a pointer arr (also locally created) pointing to this array. Now one might think if we return the pointer, then we can use this array. This is partially correct. A pointer is memory location, and thus p will point to the location of this array. However, when func() returns and exits, this stack disappears! Suddenly the memory location p points to is invalid. (You will probably get a warning from the compiler.) See the diagram (a) on the next page. CS31: Introduction to Computer Science I Spring 2011 Copyright Brian Choi 2011. All Rights Reserved. Week 8, Page 1/6 int *func(); // Creates an array of length 10 and returns it. int main()
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/06/2011 for the course CS 31 taught by Professor Melkanoff during the Spring '00 term at UCLA.

Page1 / 6

cs31s11dis8 - CS31 Introduction to Computer Science I...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online