Miguel Garzon CrUise Lab - SITE. #include dynamic memory allocationdynamic memory allocation...

18
Miguel Garzon CrUise Lab - SITE

Transcript of Miguel Garzon CrUise Lab - SITE. #include dynamic memory allocationdynamic memory allocation...

Miguel GarzonCrUise Lab - SITE

#include <nom_librarie>•<stdlib.h><stdlib.h>

dynamic memory allocation dynamic memory allocation •<stdio.h><stdio.h>

Input/outputInput/output•<string.h><string.h>

String handlingString handling•<math.h><math.h>

Mathematical functionsMathematical functions•<ctype.h><ctype.h>

CharactersCharacters

Variables: Storage Duration and Scope

• Local variable– Local scope, store in the call stack

• Static local variable– Local scope, single and statically allocated

• Global variable– Accessible in every scope

Example: vars.c

IO Functions

• Standard file IO: <stdio.h>– FILE *fopen(const char *filename, const char *mode); – size_t fread(void *ptr, size_t size, size_t nitems, FILE

*stream); – size_t fwrite(const void *ptr, size_t size, size_t

nitems, FILE *stream); – int fseek(FILE *stream, long int offset, int whence); – int fclose(FILE *stream);

• Example: file.c from C file input/output

IO Functions – <stdio.h>

• printf(“the int value is %d”, var);• printf(“the string value is %s”, var);• printf(“the charactr and double values are %c and

%e”, var);• printf(“the double value is %e”, var);

• scanf(“enter an int: %d”, var);• scanf(“enter a string: %s”, var);• scanf(“enter a character and a double: %c %e”,

var);• scanf(“enter a string: %s”, var);

• struct.c• typedef struct {

int i;char c;float f;char cc[3];} aaa;

• int len = sizeof(aaa);

• sort.cvoid qsort ( void * base,

size_t num, size_t size, int ( * comparator ) ( const void *,

const void * ) );...int values[] = { 40, 10, 100, 90, 20, 25 }; int compare (const void * a, const void * b) {…}qsort (values, 6, sizeof(int), compare);

Pointers

• pointer.cint a[10];int b[] = {1, 2, 3};int *pb = &b[1];

char *arr[] = {"aaa", "bbb", "ccc", "ddd"};char **p = arr;char *q = arr[0];char **r = &arr[0];

• dmem.c void *malloc(size_t size);void free(void *pointer);void *realloc(void *pointer,size_t size);

• Parameterspointer Pointer to a memory block previously allocated with malloc, calloc or realloc to be reallocated.If this is NULL, a new block is allocated and a pointer to it is returned by the function. size New size for the memory block, in bytes.If it is 0 and ptr points to an existing block of memory, the memory block pointed by ptr is deallocated and a NULL pointer is returned.

Processes• fork() creates a child process

– On success,• the PID of the child process is returned in the parent . and a 0 is returned in the child.

– On failure,• a -1 will be returned in the parent's context,

• fork.cint x = 1; /* Parent and child will get private copies*/pid_t pid = fork();if (pid == 0)

printf("in child (%d), x = %d\n", getpid(), ++x);else

printf("in parent(%d), x = %d\n", getpid(), --x);printf("bye from process (%d) with x = %d\n", getpid(), x);

…• No boolean type in CNo boolean type in C

                  #define TRUE 1 #define TRUE 1 #define FALSE 0#define FALSE 0

                    while(1) { ; /* do nothing */ } • Two functions cannot have the same name

• Variables must be declared at the top of a basic block (for some c systems)Variables must be declared at the top of a basic block (for some c systems) (Cela ne compile pas )(Cela ne compile pas ) {    int a;{    int a;

              printf("Hello world\n");              printf("Hello world\n");              char b;              char b;         }          }

• Exceptions?

• Garbage collection in CGarbage collection in C

• bad.cchar a[10];

char *b = "bbbbb";

b[0]='a';

a[8000]='b';

• Bug:– a[8000] is accessing an invalid memory address

GDB Debug Steps

$ gcc –o bad bad.c$ gdb ./bad

(gdb) break main [set break point at main()](gdb) run [start program](gdb) step 1 [step one line]

Program received signal SIGSEGV, Segmentation fault. main () at bad.c:15

15 a[8000]='b';

Compiler

• GNU Compiler CollectionGNU Compiler Collection

• A compiler system produced by the GNU Project supporting various programming languages

• Compile C code to an executableCompile C code to an executable– gcc -o <executable-name> <.c files>gcc -o <executable-name> <.c files>

• Reporting all warning messagesReporting all warning messages– gcc -Wall -o <executable-name> <.c files>gcc -Wall -o <executable-name> <.c files>

Compiler

• An example of compiling two .c files An example of compiling two .c files into an executable programinto an executable program– gcc -Wall -o NomduFichier main.c list.cgcc -Wall -o NomduFichier main.c list.c

• An example of compiling two files and An example of compiling two files and then link them separately then link them separately – gcc -Wall -o main.o -c main.c gcc -Wall -o main.o -c main.c – gcc -Wall -o list.o -c list.c gcc -Wall -o list.o -c list.c – gcc -Wall -o assign1prob1 main.o list.o gcc -Wall -o assign1prob1 main.o list.o

Compiler

• An example of compiling two .c files An example of compiling two .c files into an executable programinto an executable program– gcc -Wall -o NomduFichier main.c list.cgcc -Wall -o NomduFichier main.c list.c

• An example of compiling two files and An example of compiling two files and then link them separately then link them separately – gcc -Wall -o main.o -c main.c gcc -Wall -o main.o -c main.c – gcc -Wall -o list.o -c list.c gcc -Wall -o list.o -c list.c – gcc -Wall -o assign1prob1 main.o list.o gcc -Wall -o assign1prob1 main.o list.o

Compiler• enter gdbenter gdb

– gdbgdb• Quit gdbQuit gdb

– quitquit• printing line from a source fileprinting line from a source file

– listlist• running a programrunning a program

– runrun• breakpoint at a line numberbreakpoint at a line number

– break <c file name>:<line number>break <c file name>:<line number>– break <line number>break <line number>

• break at a particular functionbreak at a particular function– break <c file name>:<functionname>break <c file name>:<functionname>

• set a breakpoint with condition set a breakpoint with condition – break <function or file name> if <condition>break <function or file name> if <condition>

• deleting breakpointsdeleting breakpoints– deletedelete

• proceed onto the next breakpointproceed onto the next breakpoint– continuecontinue

• Single stepSingle step– stepstep

Make file• Makefile and the make command

– We can write many commands in a script file and run it all at once;

– For example:

all:gcc hello.c –o hello.o mkdir -p folderNameCreermv hello.o folderNameCreer

1) Write the previous commands in a file called Makefile-1.txt; (in the same directory of hello.c);

2) Type the command: make -f Makefile-1.txt