c - sizeof dynamic array is not correct -


this question has answer here:

in array there 4 element size should 4bit*4 = 16. (an int data type take 4 bit in system store value.) when ran code got 8 bit size of dynamicarray.

#include <stdio.h> #include <stdlib.h>  int main(void) {      //dynamic arrays save memory creating pointer stores     //the beginning of array     int *dynamicarray = malloc(20 * sizeof(int));     *dynamicarray = 10;     printf("address %x stores value %d\n", dynamicarray, *dynamicarray);      dynamicarray[1] = 20;     printf("dynamicarray[1] stores value %d\n", dynamicarray[1]);     dynamicarray[2] = 45;     printf("dynamicarray[2] stores value %d\n", dynamicarray[2]);     dynamicarray[3] = 34;     printf("dynamicarray[3] stores value %d\n", dynamicarray[3]);     printf("the size of dynamicarray %d\n", sizeof(dynamicarray));      // release unused memory:     free(dynamicarray);      return exit_success; } 

here image of output.

enter image description here

also suggest me website c check in-built function properties or know them more. thank you.

  • you don’t have array; have pointer.

  • the size of pointer measured in bytes, not bits.

  • sizeof evaluated @ compile time , constant given expression or type. not depend on number of “filled” elements in array (or pointer space holds elements, matter).

your expression equivalent sizeof(int*), , pointers 8 bytes in environment.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -