Memory - UtilityAcc/C-Notes GitHub Wiki

GLOBALS = GLOBAL DATA, VARIABLES.

HEAP = FREE MEMORY

STACK = ALLOCATED MEMORY

& - gets an address of variable in memory

a = 5 :a variable is equals to value 5, if you go to address a you will get 5 value

&a = address of variable a , it will be like 0x587756 HEX, this address stores value 5

*shows value, stored in address

a=5 declare varialbe with value 5

int* p declare pointer

p = &a pointer p points location in memory, here p is equal to address of a variable, for example 0x123 and contains value 5

*p unpacks the value stored in address p and in current case it equals to 5

POINTER is a variable which stores a HEX address of another variable(memory location), has a memory address datatype only data, that contains address can be assigned to it. Pointer points on the first element of data series like array of chars or integers.

Pointer has an datatype designator like int, char, float If pointer has a INT designator it points on first 8 bytes of data, if char then first byte.

Pointer can be moved by incerement and decrement by some int number like p + 1 or p - 3 Pointer itself has 8 BYTE SIZE

#include <stdio.h>

int main(void)

{

int n = 50;

int *p = &n;

printf("%p\n", p);

}

POINTER INCREMENT DECREMENT EXAMPLE

#include <stdio.h>

int main(void)

{ char *s = "HI!";

printf("%c\n", *s);

printf("%c\n", *(s+1));

printf("%c\n", *(s+2));

} ` If you initialize an array is automatically becomes a POINTER. Arrays and pointers are part of one datatype.

int main(void)

{ int numbers[] = {4, 6, 8, 2, 7, 5, 0};

`printf("%i\n", *numbers);`
`printf("%i\n", *(numbers + 1));`
`printf("%i\n", *(numbers + 2));`
`printf("%i\n", *(numbers + 3));`
`printf("%i\n", *(numbers + 4));`
`printf("%i\n", *(numbers + 5));`
`printf("%i\n", *(numbers + 6));`

}

MALLOC = memory allocation. Requests the specific amount of memory for storing the data:

This code copys string from one array to another, but first memory allocation is taking place with malloc function. ` #include <cs50.h> #include <ctype.h> #include <stdio.h> #include <stdlib.h> #include <string.h>

int main(void)

{ char *s ="sweetpussy";

char *t = malloc(strlen(s) + 1); \\ request quantity of bites whith equals the size of `s` array plus 1 char for \0 in the end of the string

for (int i = 0, n = strlen(s) + 1; i < n; i++)
{

    t[i] = s[i];

}

t[0] = toupper(t[0]);

printf("s: %s\n", s);
printf("t: %s\n", t);

free(t); \ clear memory allocation } s:sweetpussy t:Sweetpussy `

IF you declare an empty pointers vithout variable, you have to allocate memory to it int main(void) {
int *x;
int *y;

x = malloc(sizeof(int)); \\GOOOD                   

*x = 42;
***y = 13; \\\CAN CAUSE SEGMENTATION FAULT   **

y = x;        

*y = 13;   

}`

PASS POINTERS TO SWAP FUNCTION FOR SWAPPING VARS FROM MAIN SCOPE

`#include <stdio.h>

void swap(int *a, int *b);

int main(void)

{

int x = 1; int y = 2;

printf("x is %i, y is %i\n", x, y);

swap(&x, &y);

printf("x is %i, y is %i\n", x, y);

}

void swap(int *a, int *b)

{ int tmp = *a;

*a = *b;

*b = tmp;

}`

LINKS

https://www.geeksforgeeks.org/memory-layout-of-c-program/

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