cs lecture nov 9

cs lecture nov 9 - cs lecture nov 9 int *f()cfw_ int a...

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

View Full Document Right Arrow Icon
cs lecture nov 9 int *f(){ int a [100]; return a; } BAD #include <stdlib.h> int *g() int *a=(int x)malloc( size of(int)); return a; } ok int main(void) int *b=f(); b[50]=6; b=g(); b[50]=12; return 0; } chapter 17 #include <stdlib.h> void #malloc(size_t size); <-returns a pointer to allocated storage of specified size uninitialized void free(void *p); free storage allocated by malloc void *realloz(void *p, size_t size); -reallocates a previously allocated block of memory to make it larger -pointer may change problem: malloc/realloc return a null pointer if insufficient memory is available,…crash #include <stdio.h> #include<stdlib.h> #include<assert.h> void safemalloc (size_t size) void *p=malloc(size); assert(p); return p; Pointers to structures
Background image of page 1

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

View Full DocumentRight Arrow Icon
struct tod{ int hour, min; }; struct tod *t= (struct tod*)malloc(sizeof(struct tod)); t[] [ ]hour [ ]min accessing members (*t).hour OR t->hour Building better arrays -grow automatically -knows its size -initialized to 0 like java Vector class v[]
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 12/10/2011 for the course CS 137 taught by Professor Clarke during the Fall '10 term at Waterloo.

Page1 / 4

cs lecture nov 9 - cs lecture nov 9 int *f()cfw_ int a...

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