W19-PointerExamples

W19-PointerExamples - Pointer Examples 11/7/05 1 Dynamic...

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

View Full Document Right Arrow Icon
11/7/05 1 Pointer Examples 11/7/05 2 Dynamic Memory Allocation • Getting Memory from the OS • The Problem. – We want to determine an array size when the program runs, not when it is compiled • The Solution. – Get memory for the array from the OS (Windows). 11/7/05 3 The OS is "Keeper of the Heap" • Each program in Win2000 or WinXP can have up to 4 GBytes of memory. • You just have to ask the Operating System to give you some. • This process called 'memory allocation' -- the OS is allocating memory for your use. • Memory from OS is from the 'heap'
Background image of page 1

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

View Full DocumentRight Arrow Icon
11/7/05 4 void * malloc(int size) • Stands for 'memory allocation'. size is number of bytes you want. • returns a pointer to the first byte of your newly allocated memory. • cast it into type of pointer you want ( char *, int *, etc.) • Use the memory however you like. 11/7/05 5 BigArray Example of malloc Use #include <stdio.h> #include <stdlib.h> #define SIZE 1000 int main( void ) { int *p, i; p = (int *)malloc(SIZE * sizeof(int));
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 06/28/2009 for the course CS 151 taught by Professor Staff during the Spring '08 term at Oregon State.

Page1 / 6

W19-PointerExamples - Pointer Examples 11/7/05 1 Dynamic...

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

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