Pages

Monday, April 27, 2015

Understanding pointer in C programming language

**dynamic memory allocation

manage memory.

get block of memory of any size and control using pointer.

pointer = location in memory.



ex:

#include <stdio.h>

main(){
int angka;
int* pointer;



angka = 5;


pointer = &angka;  //  & = alamat memory dari.


  printf( "angka:  %i\n", angka ); // 5
  printf( "alamat memory dr angka", &angka ); // 0xbfffc5c
  printf( "alamat pointer", pointer ); // 0xbfffc5c

  printf( "value pointer: %i\n", *pointer ); // 5
}



======
decimal vs hexadecimal


Decimal has 10 possible values for each digit:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Hexidecimal has 16 possible values for each digit:                
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f


0xbfffc5c

0x -> prefix / menandai sebuah bilangan hexadecimal.  bffc5c -> angkanya
======

int* pointer  -> pointer declaration

*pointer ->  get pointer value.



======

** dynamic memory.



bisa memesan block memory lebih besar tp nanti harus di free sendiri. klo ga terjadi memory leaks.

void * memoryBlock = malloc (4);  // reserve 4 bytes of memory


void = typenya undefined / belum diketahui. bisa dimasukin char int etc.

free (memoryBlock); // cara buat ngefreein memory;



void * memoryBlock  = malloc (sizeof(int)* 20);  //allocate 20 int values

void * memoryBlock = calloc (20, sizeof(int)); // clear memory before return to pointer




======

No comments:

Post a Comment