fbpx

Struktur Data : Alokasi Memori Dinamis – sizeof()

๐Ÿ“‹ Daftar Isi

Dynamic Memory Allocation adalah suatu prosedur dimana struktur data (seperti array) berubah selama program dieksekusi (run time) terdapat 4 fungsi dari header <stdlib.h> untuk menggunkan alokasi memori :

Alokasi

malloc()
calloc()
realloc()

Dealokasi

free()

Fasilitas ini memungkinkan user untuk membuat struktur data dengan ukuran dan panjang berapapun yang diseesuaikan dengan kebutuhan di dalam program.


Fungsi Sizeof()

Output dari sizeof() adalah jumlah memori yang dialokasikan untuk tipe data tersebut (dalam byte)

#include <stdio.h>
struct employee
{
  char name[40];
  int id;
};

int main()
{
  int myInt = 16;
  struct employee stats;
  int arr[] = {1, 2, 3, 4, 7};
  printf("size of variable myInt : %d\n", sizeof(myInt));
  printf("size of variable stats : %d\n", sizeof(myInt));
  printf("size of variable arr : %d\n", sizeof(myInt));
  printf("size of int data type : %d\n", sizeof(myInt));
  printf("size of char data type : %d\n", sizeof(myInt));
  printf("size of float data type : %d\n", sizeof(myInt));
  printf("size of double data type : %d\n", sizeof(myInt));
  
  return 0;
}
 

Output yang dihasilkan dimungkinkan akan berbeda-beda tergantung prosesor dan compiler yang digunakan.

size of variable myInt : 4
size of variable stats : 44
size of variable arr : 20
size of int data type : 4
size of char data type : 1
size of float data type : 4
size of double data type : 8

Materi Lengkap

Silakan baca juga beberapa artikel menarik kami tentang Struktur dan Pointer, daftar lengkapnya adalah sebagai berikut.


Tonton juga video pilihan dari kami berikut ini

Bagikan ke teman-teman Anda

Contact Us

How to whitelist website on AdBlocker?

How to whitelist website on AdBlocker?

  1. 1 Click on the AdBlock Plus icon on the top right corner of your browser
  2. 2 Click on "Enabled on this site" from the AdBlock Plus option
  3. 3 Refresh the page and start browsing the site
error: Content is protected !!