Growing Arrays in C Nathan Lee.

Slides:



Advertisements
Similar presentations
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Advertisements

Stacks, Queues, and Linked Lists
Linked Lists.
CSCE 3110 Data Structures & Algorithm Analysis
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
SEE C GO Provisional Title. Syntax Types int, float, double, char, void Identifiers foo Operators + - * / ^ Delimiters ; {} () “” ‘’ Keywords return,
Data Structure Lecture-5
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
The University of Adelaide, School of Computer Science
Dynamic Memory Allocation I Topics Basic representation and alignment (mainly for static memory allocation, main concepts carry over to dynamic memory.
LCS Non-Dynamic Version int function lcs (x, y, i, j) begin if (i = 0) or (j = 0) return 0; else if (x[i] = y[j]) return lcs(x, y, i-1, j-1)+1; else return.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
C Intro.
Growing Arrays in C Language. When to Use Do not use Growing Sorted Array –O(n 2 ) Operation –Avoid when n is large Use Keep track of a variable –Few.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Linked Lists in C and C++ By Ravi Prakash PGT(CS).
Introduction to C Programming CE Lecture 18 Dynamic Memory Allocation and Ragged Arrays.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Linked Structures, Project 1: Linked List Bryce Boe 2013/07/11 CS24, Summer 2013 C.
Algorithms and Data Structures* Objective: To review the fundamental algorithms and data structures that are commonly used in programs. To see how to use.
Lecture Contents Arrays and Vectors: Concepts of pointers and references. Pointer declarations and initialization. Pointer Operators. Dynamic Memory Allocation.
Week 9 Part 1 Kyle Dewey. Overview Dynamic allocation continued Heap versus stack Memory-related bugs Exam #2.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
C Programming Types & Dynamic memory & bits & others
Dynamic Storage Allocation
Memory allocation & parameter passing
Computer Organization and Design Pointers, Arrays and Strings in C
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Chapter 12 – Data Structures
Dynamic Allocation Review Structure and list processing
5.13 Recursion Recursive functions Functions that call themselves
Introduction to Programming
Chapter 19 Data Structures
Pointers and Memory Overview
C Short Overview Lembit Jürimägi.
CSCI206 - Computer Organization & Programming
Programming Languages and Paradigms
Hash Tables in C James Goerke.
Hash Tables in C Louis Manco.
CSC 253 Lecture 8.
Java for Beginners University Greenwich Computing At School DASCO
CSC 253 Lecture 8.
Lists, Strings & Multi-dimensional arrays
Pointers, Dynamic Data, and Reference Types
CMSC 202 Lesson 22 Templates I.
Growing Arrays in C Russell Stephens.
Classes Short Review of Topics already covered Constructor
Hash Tables: A basic O(1)verview
By Hector M Lugo-Cordero September 17, 2008
Review & Lab assignments
Tim Ehrlich Growing Arrays in C.
Linked Lists in C and C++
CS111 Computer Programming
Pointers Pointers point to memory locations
Dynamic Memory A whole heap of fun….
Programming Language C Language.
Templates I CMSC 202.
C Programming - Lecture 5
Programming with Arrays 2
C Structures and Commands
Dynamic Memory – A Review
Pointers, Dynamic Data, and Reference Types
Arrays as pointers and other stuff
Module 13 Dynamic Memory.
CSE 303 Concepts and Tools for Software Development
Presentation transcript:

Growing Arrays in C Nathan Lee

The Problem Compiler statically allocates size, content int a[5]; How do we manipulate the size of arrays in C?

Adding Values (code) struct Nameval { char *name; int value; }; *found on page 42 of textbook struct Nameval { char *name; int value; }; struct NVtab { int nval; int max; Nameval *nameval; } nvtab; enum { NVINIT = 1, NVGROW = 2};

Adding Values (code) int addname (Nameval newname) { Nameval *nvp; if (nvtab.nameval == NULL) { nvtab.nameval = (Nameval *) malloc (NVINIT * sizeof (Nameval)); if (nvtab.nameval == NULL) return -1; nvtab.max = NVINIT; nvtab.nval = 0; } …

Adding Values (code) …else if (nvtab.nval >= nvtab.max) { nvp = (Nameval *) realloc(nvtab.nameval, (NVGROW*nvtab.max) * sizeof(Nameval)); if (nvp == NULL) return -1; nvtab.max *= NVGROW; nvtab.nameval = nvp; } nvtab.nameval[nvtab.nval] = newname; return nvtab.nval++;

Adding Values realloc doubles the size of the array (performance) Adding 100 elements: growing 100 times vs growing (log2 100 ≈ 7) times ‘nvp’ is a temp value – if realloc fails Not Sorted!

The Bottom Line Arrays are good for static, non-sorted allocation We can minimize the cost of allocation in some ways For fluctuating values and contents, don’t use arrays

Resources Used: Kernighan, Brian W. and Rob Pike. The Practice of Programming. 1999 Lucent Technologies.