10.09.2013 Views

1. Advanced Data Structure using C++

1. Advanced Data Structure using C++

1. Advanced Data Structure using C++

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

LECTURE NOTES OF ADVANCED DATA STRUCTURE (MT-CSE 110)<br />

This example uses calloc to allocate enough memory for an int array of five<br />

elements. Then realloc is called to extend the array to hold seven elements.<br />

#include<br />

#include <br />

int main() {<br />

int *ptr;<br />

int i;<br />

ptr = calloc(5, sizeof(int));<br />

if(ptr!=NULL) {<br />

*ptr = 1;<br />

*(ptr+1) = 2;<br />

ptr[2] = 4;<br />

ptr[3] = 8;<br />

ptr[4] = 16;<br />

/* ptr[5] = 32; wouldn't assign anything */<br />

ptr = realloc(ptr, 7*sizeof(int));<br />

if(ptr!=NULL) {<br />

printf("Now allocating more memory... \n");<br />

ptr[5] = 32; /* now it's legal! */<br />

ptr[6] = 64;<br />

for(i=0 ; i

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!