Basic Pointers, Local memory, Reference Parameters, Heap Memory - RakshithNM/articles GitHub Wiki

###Basic Pointers, Local memory, Reference Parameters, Heap Memory

####Pointers

  • A pointer stores a reference to its pointee. The pointee, in turn, stores something useful.
  • The dereference operation on a pointer accesses its pointee. A pointer may only be dereferenced after it has been assigned to refer to a pointee. Most pointer bugs involve violating this one rule.
  • Allocating a pointer does not automatically assign it to refer to a pointee. Assigning the pointer to refer to a specific pointee is a separate operation which is easy to forget.
  • Assignment between two pointers makes them refer to the same pointee which introduces sharing.
  • & - returns the address of a variable.
  • * - returns the data to which it is pointing.

####Local Memory

Locals are very convenient for what they do — providing convenient and efficient memory for a function which exists only so long as the function is executing. Locals have two deficiencies which we will address in the following sections — how a function can communicate back to its caller(a function that calls a callee), and how a function can allocate separate memory with a less constrained lifetime.

####Reference Parameters

Passing by value (copying) does not allow the callee(a function that was called by the caller) to communicate back to its caller and has the usual disadvantages of making copies. Pass by reference uses pointers to avoid copying the value of interest and allow the callee to communicate back to the caller.

For pass by reference, there is only one copy of the value of interest, and pointers to that one copy are passed. So if the value of interest is an int, its reference parameter is an int* . If the value of interest is a struct fraction* , its reference parameters is a struct fraction** .Functions use the dereference operator(*) on the reference parameter to see or change the value of interest

Program to swap two integers using call by reference

#include

int swapNumbers(int*, int*);

int main(void) {
	int a, b;
	
	printf("Enter two numbers to be swapped\n");
	scanf("%d\n%d\n", &a, &b);
	
	printf("The numbers before swapping are a = %d,b = %d\n", a, b);
	
	swapNumbers(&a, &b);
	
	printf("The numbers after swapping are a = %d,b = %d\n", a, b);
	
	return 0;
}

int swapNumbers(int* x, int* y){
	int temp;
	temp = *x;
	*x = *y;
	*y = temp;
}

####Heap Memory

Heap memory provides greater control for the programmer — the blocks of memory can be requested in any size, and they remain allocated until they are deallocated explicitly. Heap memory can be passed back to the caller since it is not deallocated on exit, and it can be used to build linked structures such as linked lists and binary trees. The disadvantage of heap memory is that the program must make explicit allocation and deallocate calls to manage the heap memory. The heap memory does not operate automatically and conveniently the way local memory does.

Introduction: malloc(), calloc(), free(), stdlib.h

source

Reference Lecture

⚠️ **GitHub.com Fallback** ⚠️