c programming
play

C Programming Basics GCC 8.2.0 & GDB 8.1.1 nike.cs.uga.edu - PDF document

8/21/18 C Programming Basics GCC 8.2.0 & GDB 8.1.1 nike.cs.uga.edu brew macOS gcc-8 brew install gcc, or brew upgrade gcc Also need Xcode CLT xcode-select install Brew info gcc brew install gdb


  1. 8/21/18 C Programming Basics GCC 8.2.0 & GDB 8.1.1 • nike.cs.uga.edu • brew macOS � gcc-8 – brew install gcc, or – brew upgrade gcc Also need Xcode CLT – xcode-select – install – Brew info gcc • brew install gdb • https://sourceware.org/gdb/wiki/BuildingOnDarwin 1

  2. 8/21/18 Set up: LIBRARY : PATH • On nike.cs.uga.edu • LD_LIBRARY_PATH – ~maria/usr/GDB-8.1.1/lib – ~maria/usr/GCC-8.2.0/lib64 – ~maria/usr/GCC-8.2.0/lib • PATH – ~maria/usr/GCC-8.2.0/bin/gcc .login (tcsh) or .profile (bash/ksh) ls /home/profs/maria/usr/GCC-8.2.0/ ls /home/profs/maria/usr/GDB-8.1.1/ setenv GCC_HOME /home/profs/maria/usr/GCC-8.2.0/ csh/tcsh setenv GDB_HOME /usr/profs/maria/usr/GDB-8.1.1/ setenv PATH ${GCC_HOME}/bin/:${PATH} setenv PATH ${GDB_HOME}/bin/:${PATH} setenv LD_LIBRARY_PATH ${GCC_HOME}/lib:${LD_LIBRARY_PATH} setenv LD_LIBRARY_PATH ${GCC_HOME}/lib64:${LD_LIBRARY_PATH} setenv LD_LIBRARY_PATH ${GDB_HOME}/lib:${LD_LIBRARY_PATH} export GCC_HOME=/home/profs/maria/usr/GCC-8.2.0/ bash/ksh export GDB_HOME=/usr/profs/maria/usr/GDB-8.1.1/ PATH= ${GCC_HOME}/bin/:${PATH} PATH =${GDB_HOME}/bin/:${PATH} export=LD_LIBRARY_PATH=${GCC_HOME}/lib:${LD_LIBRARY_PATH} export=LD_LIBRARY_PATH=${GCC_HOME}/lib64:${LD_LIBRARY_PATH} export=LD_LIBRARY_PATH=${GDB_HOME}/lib:${LD_LIBRARY_PATH} 2

  3. 8/21/18 • C Basics • Debugging Tools • C Standard Library – getopt – stdio.h – stdlib.h – string.h C Basics • For Some: – Overlap of C++ System Programming. • Pointers, Arrays, structs, Casting • Memory Management • Function Pointers / Generic Types • Strings 3

  4. Carnegie Mellon 8/21/18 Variable Declarations & Qualifiers Global Variables: ■ Defined outside functions, seen by all files ■ Use “extern” keyword to use a Scope of Variables ■ global variable defined in another file Const Variables: ■ • Global Variables: For variables that won’t change ■ – Declared outside functions Data stored in read-only data section • Seen by all files ■ • Use extern to use a global variables Static Variables: ■ declared in another file • const Variables For locals, keeps value between invocations ■ – Data stored in read-only segment – USE SPARINGLY ■ (variable does not change) Note: static has a different meaning when • static Variables ■ referring to functions (not visible outside of – In functions – value is persistent between invocations (not volatile). object file) – Static Functions – not visible outside object file (not same semantic). https://www.geeksforgeeks.org/scope-rules-in-c/ https://www.geeksforgeeks.org/const-qualifier-in-c/ Casting • Converts variables to different types: – Implicit : (automatic by compiler) • Type promotion (upgraded to ‘data type). – trigged by multiple data types in an expression. • Type “demotion” (can happen, look out for this): – Loss of sign, and overflow. – Example: long long � float https://www.geeksforgeeks.org/type-conversion-c/ 4

  5. 8/21/18 ctutorial/global[hmv].c – Explicit - “type casting” • (type) expression Cautions: – Casting down: • Truncates – Casting up pointers ctutorial/pointers.c Pointers • Stores address of a value in memory • Access the value by de-referencing *aptr. – Used when reading or writing a value to a given address • Be aware: – De-referencing NULL causes undefined behavior. E.g., segmentation error. 5

  6. 8/21/18 ctutorial/pointers.c Carnegie Mellon Pointers Pointers • Pointers to type A ■ Pointer to type A references a block references a block of of sizeof(A) bytes sizeof(A) bytes. ■ Get the address of a value in • Get the address of a memory with the ‘ & ’ operator ■ Pointers can be aliased , or pointed value in memory with to same address & • Pointers can be aliased, or pointed to the same address. generate_scheduler.c • pointers.c – Demo of pointer arithmetic – Type affect the arithmetic • Adding x to a pointer A – A + x � (A+x* sizeof(TYPE_OF_PTR_A)) 6

  7. 8/21/18 passing_args.c Call by value vs. Call by reference • Call-by-value: – Changes made to arguments passed to a function aren’t reflected in the calling function • Call-by-reference: – Changes made to arguments passed to a function are reflected in the calling function • C is call by value by default, so anything past is by value, you can use a pointer to simulate call by reference – Parameter is an address and the address still does not change but now the content what it points to can. Arrays and Strings • Arrays : fixed-size collection of elements of the same type. – Can allocate on the stack or on the heap: int A[10]; //A on the stack //A on the heap . int * A = calloc(10, sizeof(int)); • Strings : Null-character (‘\0’) terminated character arrays – Null-character tells us where the string ends – All standard C library functions on strings assume null-termination 7

  8. 8/21/18 structs.c Struct • Collection of values under on name in a single block of memory – Access fields using ‘.’ – Use a pointer access via ‘ � ’ typedef.c typedef • Typedefs • Function pointers. https://www.newty.de/fpt/fpt.html https://www.geeksforgeeks.org/function-pointer-in-c/ 8

  9. 8/21/18 Carnegie Mellon C program Memory Layout C Program Memory Layout • Heap & Stack Stack vs. Heap Data vs. Data • Stack: – Local function variables, Function arguments. – Deallocated after variable leaves scope • Do not return pointer to a stack allocated variable • Do not reference the address of a variable outside its scope • Heap: – Dynamically allocated (malloc/calloc) • Data Section: – Global, constants. • Example: // a is a pointer on the stack to a // memory block on the heap. int * a = malloc( sizeof(int) ) 9

  10. 8/21/18 Heap: Malloc, Free, Calloc void* malloc (size_t size): allocate block of memory of size bytes does not initialize memory void* calloc (size_t num, size_t size): allocate block of memory for array of num elements each size bytes long initializes memory to zero void free(void* ptr): frees memory block, previously allocated by malloc, calloc, realloc, pointed by ptr use exactly once for each pointer you allocate size argument: should be computed using the sizeof operator sizeof: takes a type and gives you its size e.g., sizeof(int), mem_mgt_valgrind.sh Memory Management Rules • Client should free memory allocated by client code • Library should free memory allocated by Library code • Number of mallocs = Number of frees – # of mallocs > # of frees // memory leak – # of mallocs < # frees // double free. • Only malloc when necessary – Persistent, variable size data structure = necessary. 10

  11. 8/21/18 Valgrind • Find common errors detect memory leaks • Common Errors – Illegal read/write errors – Use of unitialized values – Illegal frees – Overlapping source/destination addresses • Questions: – Enough memory? – Did you accidentally free? – Did something multiple times (but did not mean to) – Use something you just freed. Valgrind http://valgrind.org/docs/manual/quick-start.html 11

  12. 8/21/18 Other Topics • Head files and Header Guards • Macros • C-Libraries. Header Guards • Avoids the double inclusion problem. #ifndef MARIA_H #define MARIA_H #endif 12

  13. 8/21/18 Macros • Replace a name with a macro definition – No function call overhead – Find and replace mechanics of a text editor. • Typical Uses: – Defining constants (INT_MAX, ARRAY_SIZE) – Simple operations: MAX(a,b) – Require, Ensure • Caution: – Use parenthesis around expressions, to avoid substitution problems – Do not pass expression with side effect as arguments to macros. #define INT_MAX 0x7FFFFFFFF #define MAX(A, B) ((A) > (B) ? (A) : (B)) #define REQUIRES(COND) assert(COND) #define WORD_SIZE 4 #define NEXT_WORD(a) ((char*)(a) + WORD_SIZE) string.h • Be Aware of ‘ \0 ’ string termination. • dest need to be large enough to hold src • src contains n bytes (check). • src and dest do not overlap 13

  14. 8/21/18 Common Array Methods void memcpy( void *dest, void *src, size_t n ) void strcpy( void *dest, void *src ) char *strncat(char *dest, char *src, size_t n) char *strcat(char *dest, char *src) char *strdup( const char *s1) char *strncat (char *dest, char *src, size_t n) char *strcat (char *dest, char *src) int strncmp (char *str1, char *str2, size_t n) int strcmp(char *str1, char *str2) char *strstr (char *str1, char *str2) char *strtok (char *str, char *delimiters) size_t strlen (const char *str) void *memset (void *ptr, int val, size_t n) stdlib.h • Malloc, calloc, free • atoi • System Calls – exit(), abort() • Searching, Sorting – bsearch() – qsort() 14

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend