C dynamic memory allocation and sizeof()

前端 未结 7 1582
感情败类
感情败类 2020-12-12 02:42

I\'m having some trouble understanding the difference between these two code segments: I allocate space for an array of integers dynamically within my code with the followin

7条回答
  •  抹茶落季
    2020-12-12 03:19

    If you have int arr1[8] the type of arr1 (as far as the compiler is concerned) is an array ints of size 8.

    In the example int * arr2 the type of arr2 is pointer to an integer.

    sizeof(arr1) is the size of an int array
    sizeof(arr2) is the size of an int pointer (4 bytes on a 32 bit system, 8 bytes on a 64 bit system)

    So, the only difference is the type which the compiler thinks that variable is.

提交回复
热议问题