Vous êtes sur la page 1sur 26

Files_3

How to debug your code


Segmentation fault Prompt> gcc g file.c o file Prompt> gdb ./file Use r <enter> Wherever the program stops, type up to get the error list

Exercise
Write a program that compares two files and returns 0 if they are equal and I if they are not.

Step 1: Open two files in read mode

Step 2: compare two files


test1.c
That is a test line

test2.c
This is a test line

This is a test line

This is a test line

This is a test line123

This is a test line

This is a test line

This is a test line123

Contd.
flag
1: Files not same 0: Files are same

Step 3: print the outcome of comparison

Command line arguments


It is possible to pass arguments to C programs when they are executed. The brackets which follow main are used for this purpose. argc refers to the number of arguments passed, and argv[] is an array of pointer which points to each argument which is passed to main.

Example
welcome to BITS

Example

Exercise
Rewrite the program which compares files (in slide 2), to accept the source and destination filenames from the command line. Include a check on the number of arguments passed.

Contd

Dynamic Memory Allocation


The process of allocating memory at runtime is called Dynamic Memory Allocation Need:
What happens when the array size outgrows the specified size? How to specify number of elements at the runtime? We need an ability in our language to calculate and assign the memory space required at runtime.

Memory Allocation Process


Local variables stack Heap

Free Memory
Global Variables C Program instructions

Permanent storage area

Dynamic Memory Allocation


C has four library routines (Memory Management Functions) to do this for us
malloc calloc free realloc

They allocate and free memory during program execution.

Memory Allocation Functions


Function Task
Allocate requested size of bytes and returns a pointer to the first byte of the allocated space
Allocates space for an array of elements, initializes them to zero and then returns a pointer to the memory

malloc
calloc

free
realloc

Frees previously allocated space


Modifies the size of previously allocated space

malloc()
malloc reserves the memory during the program execution. malloc reserves a block of memory of specified size Returns void pointer: we can assign it to any type of pointer Syntax:
ptr = (cast-type *) malloc (byte-size);

Needs to type cast the appropriate pointer type

Examples
1) 2) 3) 4) x = (int *) malloc (100*sizeof(int)); cptr = (char *) malloc (10); ptr = (int *) malloc (j * sizeof(int)); St_var = (struct store *) malloc (sizeof( struct store));

Note:
malloc allocates a block of contiguous bytes if heap is not sufficient to satisfy the request, malloc returns a NULL.

free(ptr) releases the used space

malloc example
Write a program to store values in an array and print the same.

malloc() example

calloc()
calloc() reserves the memory at program execution Syntax:

ptr = (cast-type *) calloc(n, elem-size);


Example: bb = (char *) calloc( j, sizeof(char));

calloc() allocates multiple blocks of storage space, each of same size, and sets all bytes to zero. (malloc allocates a single block of space)

calloc() example

free()
Dynamically release the memory when not in use Syntax:
free(ptr);

ptr is a pointer to a memory block which has already been created by malloc or calloc To note: 1. Use of invalid pointer in the call can cause the system to crash. 2. It is not the pointer that is being released but rather what it points to 3. To release an array of memory that was allocated by calloc we need only to release the pointer once. It is an error to attempt to release elements individually

Reallocate
Syntax: ptr = realloc (ptr, newsize) Note: newsize may be larger or smaller than earlier size returns NULL if unsuccessful

realloc()

NOTICE
There will be a LAB Session for Thursday batch students on 2/9/2010 at 10.30 a.m.

Vous aimerez peut-être aussi