SlideServe
Menu
Préférences
© SlideServe · v2026

Lists, Strings & Multi-dimensional arrays

brygid avatar
22 diapositives · 360 Views · Jul 21, 2014

This guide delves into the nuances of data storage within programming, focusing on static and dynamic storage techniques. It outlines how static storage reserves space on disk and RAM, maintaining values post-function termination, while dynamic storage allocates space at runtime as needed, without taking up disk space. Key examples highlight the functionality of both approaches. Additionally, it introduces multidimensional arrays and their usage, alongside practical string handling techniques in C/C++. Learn about linked lists and their performance, enabling you to optimize memory management in your applications.

Lists, Strings & Multi-dimensional arrays - slide 1
01 / 22Lists, Strings & Multi-dimensional arrays
1 / 17

Télécharger la présentation

Lists, Strings & Multi-dimensional arrays
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Lists, Strings&Multi-dimensional arrays

  2. Data Storage • Static • compiled into the disk-image of program • always takes up space on disk & RAM • usage depends on program logic • Dynamic • requested at runtime • no space taken on disk • usage as needed

  3. Static Storage • Examples: static int X; // initially=0 static int Y=5; extern int Z; // initially=0 • Compiler reserves space for these • Location for Y contains the 5 • no runtime code used to put it there • All KEEP their values, even after termination of the function

  4. Dynamic Storage • Examples: int X; int Y=5; // re-initialized at each call auto int Z; // "auto" is redundant // cannot use "auto" w/specification of a value auto int Q=5; // ILLEGAL • Compiler adds code in function-start to REQUEST RAM for automatic variables • Location for Y contains the 5 at fcn startup • runtime code IS used to put it there

  5. Dynamic Storage - 2 int X; int *xptr; xptr = &X; //xptr points to X (holds the RAM address of X) (NOT the value of X) printf ("X=%d", *X); (print the value of what X points to)

  6. Dynamic lists • Each node is a struct • Struct data elements are created at "malloc" time • Rules for static & auto apply • Only static, const, integer variables may be initialized in a struct • do it yourself at "malloc" time

  7. Double-linked lists • Simplifies traversals • Slightly slower for insert/remove • No need for temp ptr for internal insert/remove • Uses more storage (trivial vs. data size) first data data data bptr bptr bptr fptr fptr fptr

  8. Structure struct dl_list { mydatatype X; dl_list * prev; dl_list * next; };

  9. List creation dl_list * start; // only creates a pointer start = (*dl_list) malloc (sizeof (dl_list)); // creates a SINGLE element start -> prev = &start; // pt BACK to home start -> next=NULL; // end of list // now add on a new node start -> next = (*dl_list) malloc (sizeof (dl_list)); start -> next -> next=NULL; // new end of list start -> next -> prev=start->next; //<- old end of list

  10. Linked List Performance (textbook's names) • O(n) • Clear_list deletes the whole list • Delete_list deletes ONEentry (one node) • Insert_list inserts ONE entry (new node) • Retreive_list gets ONE value (w/o harm) • Replace_list replace ONE value (destructive) • O(k) • CreateList • ListEmpty/Full state of the list = empty (t/f) • ListSize # nodes

  11. Other useful operations • Insert/Delete_First • Insert/Delete_Last • Swap_entries • Copy_List • Problematic for single-linked list having fptrs: • Traverse_back • Reverse_List

  12. Strings

  13. basics • strings are arrays • stored like arrays • accessed "like" arrays • always a collection of "char" • compiler adds the 0x00 ending IF initialized • Example char * mystring = "aha"; // length=4, incl0x00 compiler allocates space, sets ptr to it

  14. Library functions • strcopy – copies a string • strncpy – copies 'n' chars • strcat – appends string 2 to end of string 1 • strcmp – compares 2 strings • strch – finds a char in string • strlen - returns length of string (w/o 0x00)

  15. Danger!!! • You must always ensure the 0x00 is there • likewise, when searching, must be careful of actual max length • compiler • does NOT generate protective code • does NOT check for overruns • but DOES do it for arrays • char * Z

  16. Multidimensional Arrays

  17. Syntax • Type name [d1][d2][d3] {init list}; • int X [3] [4] [5]; • Leftmost subscript varies most slowly – known as row major order

  18. Arrays as arguments #define SIZE 10 function X (int x, int y, int a [ ] [SIZE]) ; • New standard as of 1999 implemented function X (int a[ ] [SIZE]) { ... } highest dimension is required

  19. Dynamic arrays (1) • Requires the use of the new operator • Syntax: new Type [amount] int * ptr; // a ptr to an array of int's ptr= new int[5]; // note constant size if (dptr==NULL) … is better than if (dptr==0)…

  20. Dynamic arrays (2) int y; cin>>y; int * ptr; // a ptr to an array of int's ptr= new int[y]; // note size determined at //run time BUT: the size of the array is still FIXED (i.e.; size does not vary during execution)

  21. Deleting storage • Given the previous array: delete ptr; // deletes a SINGLE datum • the one memory loc that ptr refers to • Even if it's an array (only deletes array[0]) • delete [ ] ptr; // deletes the whole array

  22. Strings • C-style strings (array of char) • char *x; x=new char [n] • Or • Using namespace std; • #include <cstring> • C++ style (true) strings • #include <string.h> • or • using namespace std; • #include <string>