🔍

C Debugging

TypeQuiz 4 Material

Notes

Pass by Value and Pass by Reference When Using Pointers

When we pass-by-value we are passing a copy of the variable to a function. When we pass-by-reference we are passing an alias of the variable to a function. C can pass a pointer into a function but that is still pass-by-value. It is copying the value of the pointer, the address, into the function. In C++ a reference is an alias for another variable. C doesn’t have this concept, so it is always pass by value.

Basically passing in a pointer is like telling the callee where the variable to be changed is, so the callee can then go there and actually change it. the pointer itself in the callee function is just a copy of what was passed in, so any changes made to that will directly will not be reflected in any of the caller's variables

// pass by reference
void swap(int *a, int *b) {
	int t;

	t = *a;
	- a = *b;
	- b = t;
	}

Debugging

gdb → GNU Debugger

Steps

Malloc()

The C library function void *malloc(size_t size) allocates the requested memory and returns a pointer to it.

#include <stdio.h>
#include <stdlib.h>

int main () {
   char *str;

   /* Initial memory allocation */
   str = (char *) malloc(15);
   strcpy(str, "tutorialspoint");
   printf("String = %s,  Address = %u\n", str, str);

   /* Reallocating memory */
   str = (char *) realloc(str, 25);
   strcat(str, ".com");
   printf("String = %s,  Address = %u\n", str, str);

   free(str);

   return(0);
}

//compile and run:
String = tutorialspoint, Address = 355090448
String = tutorialspoint.com, Address = 355090448

Calloc

“calloc” or “contiguous allocation” method in C is used to dynamically allocate the specified number of blocks of memory of the specified type. it is very much similar to malloc() but has two different points and these are:

ptr = (float*) calloc(25, sizeof(float));

This statement allocates contiguous space in memory for 25 elements each with the size of the float.

#include <stdio.h>
#include <stdlib.h>

int main()
{
	// This pointer will hold the
	// base address of the block created
	int* ptr;
	int n, i;
	
	// Get the number of elements for the array
	n = 5;
	printf("Enter number of elements: %d\\n", n);
	
	// Dynamically allocate memory using calloc()
	ptr = (int*)calloc(n, sizeof(int));
	
	// Check if the memory has been successfully
	// allocated by calloc or not
	if (ptr == NULL) {
		printf("Memory not allocated.\\n");
		exit(0);
	}
	else {
	
		// Memory has been successfully allocated
		printf("Memory successfully allocated using calloc.\\n");
	
		// Get the elements of the array
		for (i = 0; i < n; ++i) {
			ptr[i] = i + 1;
		}
	
		// Print the elements of the array
		printf("The elements of the array are: ");
		for (i = 0; i < n; ++i) {
			printf("%d, ", ptr[i]);
		}
	}
	
	return 0;
}

C realloc() method

“realloc” or “re-allocation” method in C is used to dynamically change the memory allocation of a previously allocated memory. In other words, if the memory previously allocated with the help of malloc or calloc is insufficient, realloc can be used to dynamically re-allocate memory. re-allocation of memory maintains the already present value and new blocks will be initialized with the default garbage value.

ptr = realloc(ptr, newSize);

where ptr is reallocated with new size 'newSize'.

#include <stdio.h>
#include <stdlib.h>

int main()
{

	// This pointer will hold the
	// base address of the block created
	int* ptr;
	int n, i;

	// Get the number of elements for the array
	n = 5;
	printf("Enter number of elements: %d\n", n);

	// Dynamically allocate memory using calloc()
	ptr = (int*)calloc(n, sizeof(int));

	// Check if the memory has been successfully
	// allocated by malloc or not
	if (ptr == NULL) {
		printf("Memory not allocated.\n");
		exit(0);
	}
	else {

		// Memory has been successfully allocated
		printf("Memory successfully allocated using calloc.\n");

		// Get the elements of the array
		for (i = 0; i < n; ++i) {
			ptr[i] = i + 1;
		}

		// Print the elements of the array
		printf("The elements of the array are: ");
		for (i = 0; i < n; ++i) {
			printf("%d, ", ptr[i]);
		}

		// Get the new size for the array
		n = 10;
		printf("\n\nEnter the new size of the array: %d\n", n);

		// Dynamically re-allocate memory using realloc()
		ptr = realloc(ptr, n * sizeof(int));

		// Memory has been successfully allocated
		printf("Memory successfully re-allocated using realloc.\n");

		// Get the new elements of the array
		for (i = 5; i < n; ++i) {
			ptr[i] = i + 1;
		}

		// Print the elements of the array
		printf("The elements of the array are: ");
		for (i = 0; i < n; ++i) {
			printf("%d, ", ptr[i]);
		}

		free(ptr);
	}

	return 0;
}

Questions & Answers