w5 - CSC209: Dynamic Memory in C Kante Easley University of...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: CSC209: Dynamic Memory in C Kante Easley University of Toronto June 10, 2009 Kante Easley CSC209 CSC209: Multidimensional arrays Recall from last lecture: Multidimensional arrays are just single-dimensional arrays stored back to back. int a[2][3] = {{1, 2, 3},{4, 5, 6}}; 1 2 3 4 5 6 C stores multidimensional arrays in row-major order. x[i][j] == *((int*)x + i*n + j) , where n is the row size of x note the cast to int* that wasnt there last week! Kante Easley CSC209 CSC209: Dynamic Memory So we know how to create blocks of memory, but we need to know the size of the block before compile time. This is called static memory allocation. We want to do some dynamic memory allocation . Use the commands malloc , calloc . int *a; //nough memory for 1 pointer a = (int*)malloc(6*sizeof(int));//enough memory for 6 int Kante Easley CSC209 CSC209: Malloc int *a; a = (int*)malloc(6*sizeof(int)); Things to note: The return value of malloc is void* so we should cast it to int* We need to use sizeof to find the number of bytes in an int...
View Full Document

This note was uploaded on 04/10/2010 for the course CSC CSC209 taught by Professor Kante during the Spring '10 term at University of Toronto.

Page1 / 10

w5 - CSC209: Dynamic Memory in C Kante Easley University of...

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

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