What Does "New Int(100)" Do

What does new int(100) do?

The first line allocates a single int and initializes it to 100. Think of the int(100) as a constructor call.

Since this is a scalar allocation, trying to access arr[1] or to free the memory using delete[] would lead to undefined behaviour.

int *array = new int[n]; what is this function actually doing?

new allocates an amount of memory needed to store the object/array that you request. In this case n numbers of int.

The pointer will then store the address to this block of memory.

But be careful, this allocated block of memory will not be freed until you tell it so by writing

delete [] array;

Delete or delete[] over new int[][]

Always delete[] when the object allocated is an array (including an array of arrays, i.e., a multidimensional array). Using delete when you are supposed to use delete[], or vice versa, is undefined behaviour. It may appear to work but there is no guarantee.

How to initialize an array in Kotlin with values?

val numbers: IntArray = intArrayOf(10, 20, 30, 40, 50)

See Kotlin - Basic Types for details.

You can also provide an initializer function as a second parameter:

val numbers = IntArray(5) { 10 * (it + 1) }
// [10, 20, 30, 40, 50]

What's the difference between the types - int * and int *[100] in C?

  1. This is a pointer to an int:

    int *p;
    ┌────┐
    │int*│
    └────┘

    It should point at an int, something like this:

    ┌────┐
    │int*│
    └─┃──┘

    ┌───┐
    │int│
    └───┘
  2. This is an array of 100 pointers to int:

    int *p[100];

    That is, it gives you 100 pointers.

    ┌────┬────┬────┬────┬────┬────┬┄
    │int*│int*│int*│int*│int*│int*│
    └────┴────┴────┴────┴────┴────┴┄

    Each pointer should point an int, perhaps like this:

    ┌────┬────┬────┬────┬────┬────┬┄
    │int*│int*│int*│int*│int*│int*│
    └─┃──┴─┃──┴─┃──┴─┃──┴─┃──┴─┃──┴┄
    ▼ ▼ ▼ ▼ ▼ ▼
    ┌───┐┌───┐┌───┐┌───┐┌───┐┌───┐┌┄
    │int││int││int││int││int││int││
    └───┘└───┘└───┘└───┘└───┘└───┘└┄

    Of course, there's no reason they can't all point at the same int, or whatever.

    You may want to use an array of pointers if you want many pointers that you can easily
    iterate over. You may, for example, dynamically allocate objects and have each pointer point at a different object:

    p[0] = new int(0);
    p[1] = new int(0);
    // ...

    Perhaps dynamically allocating ints isn't the best example, but I think the point is clear.

  3. This is a pointer to an array of 100 int:

    int (*p)[100];

    That is, it gives you just 1 pointer:

    ┌───────────┐
    │int(*)[100]│
    └───────────┘

    It should point at an array that contains 100 ints:

    ┌───────────┐
    │int(*)[100]│
    └─┃─────────┘

    ┌───┬───┬───┬───┬───┬───┬┄
    │int│int│int│int│int│int│
    └───┴───┴───┴───┴───┴───┴┄

    You will get a pointer to an array when you use the address-of operator (&) on the name of an array. For example:

    int arr[100] = { /* some initial values */ };
    int (*p)[100] = &arr;

    Here, I've taken the address of the arr array, which gives me a pointer to that array. If you then want to access an element of the array, you have to dereference the pointer first: (*p)[3] will access element 3.



Side note:

Always remember that arrays are not pointers. As we have just seen, we can take the address of an array to get a pointer to it, just like any other (non-temporary) object in C++. The only special connection between arrays and pointers is that the name of an array can be implicitly converted to a pointer to the array's first element. That means the following is valid:

int arr[100] = { /* some initial values */ };
int* p = arr;

The pointer p will point at the first element in arr. Note that p is not a pointer to the array, but a pointer to an element of the array.

(Also note that there is no such thing as an array type function argument. If you write something like int p[] as a function argument, it is transformed by the compiler to be a int*.)



Related Topics



Leave a reply



Submit