Multi-dimensional Arrays and other Array Oddities Rudra Dutta CSC 230 - Spring 2007, Section 001.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
C Programming - Lecture 5
Programming in C Pointers and Arrays, malloc( ). 7/28/092 Dynamic memory In Java, objects are created on the heap using reference variables and the new.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
More Pointers Write a program that: –Calls a function to input an integer value –The above function calls another function that will double the input value.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Kernighan/Ritchie: Kelley/Pohl:
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:
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
Pointers CS 308 – Data Structures. Getting the address of a variable You need to use the address operator & #include void main() { int num; num = 22;
What does this program do ? #include int main(int argc, char* argv[]) { int i; printf("%d arguments\n", argc); for(i = 0; i < argc; i++) printf(" %d: %s\n",
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
15213 C Primer 17 September Outline Overview comparison of C and Java Good evening Preprocessor Command line arguments Arrays and structures Pointers.
Copyright 2001 Oxford Consulting, Ltd1 January Pointers and More Pointers Pointers and Arrays We’ve now looked at  Pointers  Arrays  Strings.
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
Outline Midterm results Static variables Memory model
C for Java Programmers Tomasz Müldner Copyright:  Addison-Wesley Publishing Company, 2000 Introduction to C Muldner, Chapters 1, 2.
Pointers, Heap Memory Rudra Dutta CSC Spring 2007, Section 001.
Functions, Pointers, Structures Keerthi Nelaturu.
CPT: Arrays of Pointers/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to illustrate the use of arrays.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Pointers CSE 5100 Data Structures and Algorithms.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
C Programming in Linux Jacob Chan. C/C++ and Java  Portable  Code written in one system and works in another  But in C, there are some libraries that.
Dynamic memory allocation and Pointers Lecture 4.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Functions & Pointers in C Jordan Erenrich
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
Fall 2004CS-183 Dr. Mark L. Hornick 1 C++ Arrays C++ (like Java) supports the concept of collections – mechanisms to sort and manipulate many instances.
More Pointers and Arrays Kernighan/Ritchie: Kelley/Pohl: Chapter 5 Chapter 6.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
What we will cover A crash course in the basics of C “Teach yourself C in 21 days”
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
First Compilation Rudra Dutta CSC Spring 2007, Section 001.
Today’s Material Strings Definition Representation Initialization
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Arrays and Pointers (part 2) CSE 2031 Fall March 2016.
1 Homework Continue with K&R Chapter 5 –Skipping sections for now –Not covering section 5.12 Continue on HW5.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Pointers to pointers & multi-dimensional arrays
Stack and Heap Memory Stack resident variables include:
C Primer.
Day 03 Introduction to C.
COSC 220 Computer Science II
Day 03 Introduction to C.
Command-Line Arguments
Programming Languages and Paradigms
Memory Allocation CS 217.
Outline Defining and using Pointers Operations on pointers
Homework Continue with K&R Chapter 5 Skipping sections for now
C Programming - Lecture 5
15213 C Primer 17 September 2002.
Rudra Dutta CSC Spring 2007, Section 001
Presentation transcript:

Multi-dimensional Arrays and other Array Oddities Rudra Dutta CSC Spring 2007, Section 001

Copyright Rudra Dutta, NCSU, Spring, Array Syntax Recall - – Arrays are alternative representations of blocks of memory pointed to by pointers To define an array: – type arrayName[size]; Size should be static (compiler can compute) For example – int id[1000]; id is like int * – char *names[2*50+1]; names is like char** – #define SIZE 10 – double scores[SIZE+1]; Variable size is okay on the stack – Not in global variables – (C99 prohibits variable even on stack)

Copyright Rudra Dutta, NCSU, Spring, Passing Arrays, Again When passing as argument, it is purely a pointer – Size must be passed some other way – Additional parameter is obvious way – void func1 (double val[], int num_elements); – void func1 (double *val, int num_elements); – void func3 (double val[20]); ???

Copyright Rudra Dutta, NCSU, Spring, Arrays and sizeof int id [1000]; int *pointerId; sizeof(id) is 1000*sizeof(int) – NOT 1000 sizeof(pointerId) is the number of bytes used to store a pointer to an int. The following sets pointerId to the last element of the array id : pointerId = id + sizeof(id)/sizeof(id[0]) - 1;

Copyright Rudra Dutta, NCSU, Spring, Passing Arrays #include typedef int mintar[1000]; void test (int fid[20]) { printf ("%d \n", sizeof(fid)); } int main () { int id[1000]; int * const pid = malloc (1000 * sizeof(int)); int k = rand(); int vid[k]; mintar tid; int iid[] = {1, 2, 3}; id[1] = 20; pid[20] = 1; vid[0] = 6; tid[6] = 21; printf ("%d %d %d %d %d %d\n", sizeof(id), sizeof(pid), sizeof(vid), k, sizeof(tid), sizeof(iid)); test(id); }

Copyright Rudra Dutta, NCSU, Spring, dimensional Arrays int values[2][3]; Recall that values[] looks just like a pointer access int a [3] – “ a is a pointer to a block of 3 int s” int values [2] [3]; – “ values is an array of 2 pointers to block of 3 int s” – “Pointer to block of 2 pointers to block of 3 int s” But internally “flattened” Can be accessed as pointers

Copyright Rudra Dutta, NCSU, Spring, Blocks Containing Pointers Can create the structure explicitly with pointers – block points to a block containing three pointers to double – In order to access a single object, the code has to apply dereferencing twice to block For a block b of pointers, b[i][j] refers to the j-th object in a block pointed to by b[i]

Copyright Rudra Dutta, NCSU, Spring, Blocks Containing Pointers double **block; #define SIZE 3 if((block=calloc(SIZE, sizeof(double*)))==NULL) { /* error code */ } for(i = 0; i < SIZE; i++) if((block[i]=calloc(1, sizeof(double)))==NULL) { /* error code */ } *(*block) = 2.1; block[0][0] = 2.1; /* initialize block */ for(i = 0; i < SIZE; i++) block[i][0] = i; /* free memory */ for(i = 0; i < SIZE; i++) free(block[i]); free(block); block = NULL;

Copyright Rudra Dutta, NCSU, Spring, Pointers vs. Multi-Dim Arrays Array notation may be simpler (initially) – Is less efficient Internally, compiler re-creates pointer structure – Wastes space for strings Internally, space must be reserved for longest sub-array Pointer notation is complicated, but consistent – But incurs explicitly space for extra pointers – However, only space required is actually used In general, memory management becomes programmer’s task beyond simple cases

Copyright Rudra Dutta, NCSU, Spring, Another Way: Explicit “Flattening” Ultimately, multi-dimensional arrays must be kept in (1- dimensional) memory This “flattening” can be explicitly handled by the programmer to avoid the overhead of pointer arrays Disadvantage: cannot access with multi-d array notation any more int *p; p = (int *) malloc (cols * rows * sizeof (int); *(p + cols *i + j) = 5; /* element of Row i, Column j */

Copyright Rudra Dutta, NCSU, Spring, Main Function’s Arguments hex file1 file2 int main(int argc, char **argv); int main(int argc, char *argv[]);

Copyright Rudra Dutta, NCSU, Spring, Command Line Arguments Many (most?) programs have command line options or arguments – E.g., man, gcc, … These are passed to the program as input parameters of the function main() – First parameter: count of the number of arguments – Second parameter: an array of pointers to the arguments (which are character strings) By convention, the first argument is defined to be the name by which this program was invoked

Copyright Rudra Dutta, NCSU, Spring, Command Line Arguments The following just prints out the command line arguments, one by one (starting with program name) #include int main(int argc, char **argv) { int i; for (i=0; i < argc; i++) printf("%s\n", *argv++); return 0; } Count of # of arguments Array of pointers to arguments