Docsity

Understanding Pointers and Memory Concepts in C Language, Study notes of Windows Programming

Toggle sidebar
bg1
Basic C Language Concepts 2
Random access memory (RAM)
RAM (random access memory) is the place in a computer
where the operating system, application programs, and data
in current use are kept so that they can be quickly reached by
the computer's processor. RAM is much faster to read
from and write to than the other kinds of storage in a
computer, i.e. the hard disk, floppy disk, and CD-ROM.
However, the data in RAM stays there only as long as your
computer is running. When you turn the computer off,
RAM loses its data. When you turn your computer on again,
your operating system and other files are once again
loaded into RAM, usually from your hard disk.
RAM is the best known form of computer memory. RAM is considered "random access"
because you can access any memory cell directly if you know the row and column that
intersect at that cell.
Every byte in Ram has an address.
00000000 00000000
00000000 00000001
00000000 00000010
. . .
. . .
. . .
. . .
11111111 11111111
Pointer Definition
Essentially, the computer's memory is made up of bytes. Each byte has a number, an
address, associated with it. “Pointer is a kind of variable whose value is a memory
address, typically of another variable”. Think of it as an address holder, or directions to
get to a variable.
How to assign a value to the pointer?
int *p;
int i = 3;
p = &i;
• read & as "address of"
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Understanding Pointers and Memory Concepts in C Language and more Study notes Windows Programming in PDF only on Docsity!

Random access memory (RAM)

RAM (random access memory) is the place in a computer where the operating system, application programs, and data in current use are kept so that they can be quickly reached by the computer's processor. RAM is much faster to read from and write to than the other kinds of storage in a computer, i.e. the hard disk, floppy disk, and CD-ROM. However, the data in RAM stays there only as long as your computer is running. When you turn the computer off, RAM loses its data. When you turn your computer on again, your operating system and other files are once again loaded into RAM, usually from your hard disk.

RAM is the best known form of computer memory. RAM is considered "random access" because you can access any memory cell directly if you know the row and column that intersect at that cell.

Every byte in Ram has an address.

00000000 00000000 00000000 00000001

00000000 00000010

... ... ... ... 11111111 11111111

Pointer Definition

Essentially, the computer's memory is made up of bytes. Each byte has a number, an address, associated with it. “Pointer is a kind of variable whose value is a memory address, typically of another variable”. Think of it as an address holder, or directions to get to a variable.

How to assign a value to the pointer?

*int p; int i = 3; p = &i;

  • read & as "address of"

In this piece of code, we have taken a pointer to integer denoted by “*p”. In second statement, an integer is declared and initialized by ‘3’. The next step is the most important one. Here we are passing the “Address” of the integer “i” in the pointer. Since pointers hold the variable addresses; so now the pointer “p” contains the address of the integer “i” which has a value of 3.

Pointers Arithmetic

  • Pointer Arithmetic deals with performing addition and subtraction operations on pointer variables.
  • increment a pointer ( ++ )
  • decrement a pointer ( -- )
  • Address in pointer is incremented or decremented by the size of the object it points to (char = 1 byte, int = 2 bytes, ...)

Example: pointers increment and decrement

char x = 'A'; // variable declaration and initialization int y = 32;

char *xPtr = &x; int *yPtr = &y; // pointer declaration and initialization

... xPtr--; //Since char takes 1 byte, and if xPtr has // a value of 108 now it would have a value of // address 107

xPtr++; // pointer would have a value of address 108

Here, we have explained that if we add 1 to a pointer to integer, then that pointer will point to an address two bytes ahead of its current location. Similarly, when we incremented the xPtr, the address it contained is incremented to one value since xPrt is a pointer to integer.

Note:

Value added or subtracted from pointer is first multiplied by size of object it points to

b = p; // ILLEGAL because b is a constant pointer.

A pointer can point to element of an array

**float x[15]; float y = &x[0]; float z = x;

  • y is a pointer to x[0]
  • z is also a pointer to x[0]
  • y+1 is pointer to x[1]
  • thus *(y+1) and x[1] access the same object
  • y[1] is same as *(y+1)
  • integer add, subtract and relational operators are allowed on pointers

Example: Pointer De-referencing

*int ptr; int j = 10;

*ptr = &j; printf ("%d\n", ptr);

**ptr = 15; printf ("%d %d\n", ptr, j);

*if (ptr != 0) { printf ("Pointer ptr points at %d\n", ptr); }

  • *ptr de-references pointer to access object pointed at
  • *ptr can be used on either side of assignment operator
  • if ptr is equal to 0, then pointer is pointing at nothing and is called a null pointer
  • dereferencing a null pointer causes a core dump

Example: Pointer arithmetic.

**double d; double ptr_d; char c; char ptr_c; ptr_d = &d; ptr_c = &c; //This operation will skips 8 bytes in memory because ptr_d is a pointer to double. ptr_d = ptr_d + 1; //This operation will skips 1 byte in memory because ptr_c is a pointer to character.

ptr_c = ptr_c +1;

Example:

float x[5];

Our memory model is

  • x is a pointer to the first element
  • *x and x[0] are the same
  • x and &x[0] are the same
  • elements of an array can be accessed either way
  • x is an array object, not a pointer object

Pointer Advantages

  • This allows a function to "return" more than a single value (we are not really returning more than a single variable, but we are able to directly modify the values that are in main, from within a function).
  • This allows us to refer to larger data structures with just a single pointer. This cuts back on creating multiple copies of a structure, which consumes both memory and time.
  • This also opens the door to dynamic memory allocation.

Pointer Disadvantages

  • The syntax may be confusing initially.
  • Harder to debug, have to follow pointers to make sure they are doing what is expected.
  • More Segmentation faults / Bus errors