-6

In array there are four element so it size should be 4bit*4 = 16. (An int data type take 4 bit in my system to store the value.) But when i ran this code i only got 8 bit as the size of dynamicArray.

#include <stdio.h>
#include <stdlib.h>

int main(void) {

    //Dynamic arrays save memory by creating a pointer that stores
    //the beginning of the 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 is %d\n", sizeof(dynamicArray));

    // Release unused memory:
    free(dynamicArray);

    return EXIT_SUCCESS;
}

Here is the image of output.

enter image description here

Also suggest me website for C to check the in-built function properties or to know about them more. Thank you.

Hemant Parihar
  • 732
  • 1
  • 6
  • 19
  • "*suggest me website for C to check the in-built function properties or to know about them more*" -- Sorry, but that's off-topic. Just google the function to get its details. – Spikatrix Oct 10 '15 at 05:54
  • And use `%p` to print addresses, not `%x`. – Spikatrix Oct 10 '15 at 06:00

2 Answers2

6
  • You don’t have an array; you have a pointer.

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

  • sizeof is evaluated at compile time and is constant for any given expression or type. It does not depend on the number of “filled” elements in an array (or pointer to some space that holds those elements, for that matter).

Your expression is equivalent to sizeof(int*), and pointers are 8 bytes in your environment.

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • Actually i want to check is 'malloc' free the unused memory. I initialized the dynamicArray pointer to point to the first element. As i'm only using 4 element out of 20 it should free the rest of the space. So how can i check this. As you said if i do sizeof(dynamicArray) it will return the size of pointer. – Hemant Parihar Oct 10 '15 at 12:28
  • @HemantParihar: *“As i'm only using 4 element out of 20 it should free the rest of the space.”* There’s no reason it should do that. – Ry- Oct 10 '15 at 17:42
  • I red somewhere that by using dynamic memory allocation we can free the unused memory. How we can achieve that. I'm new to C. Can you suggest some link about dynamic memory management and malloc. – Hemant Parihar Oct 10 '15 at 17:55
  • @HemantParihar: You can use `realloc` to resize memory. – Ry- Oct 10 '15 at 18:12
  • Can I free the unused memory? – Hemant Parihar Oct 10 '15 at 18:15
  • @HemantParihar: Yes, using `realloc`. Look it up. – Ry- Oct 10 '15 at 18:17
1

I ran your code on my 32-bit computer and the value of sizeof(dynamicArray) does report 4. I bet your computer is 64-bits which is why the value is 8 instead.

Take a look at: http://www.viva64.com/en/a/0004/ and look for the table titled "Table N2. 32-bit and 64-bit data models.". That would help explain why some systems report 4 and some report 8 for the value for sizeof(dynamicArray).

Mike -- No longer here
  • 2,064
  • 1
  • 15
  • 37