1
1
123qwe2015-10-21 21:24:59
C++ / C#
123qwe, 2015-10-21 21:24:59

What kind of memory are we talking about in this piece of code?

void f(){
    int* x = malloc(10 * sizeof(int));
    x[10] = 0;
}

The teacher says that 10 is not in the range
of memory. You can specify a number in square brackets from 0 to 9. What memory is he talking about?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg Tsilyurik, 2015-10-21
@Yonghwa

The teacher says that 10 is not in the memory range. You can enter a number in square brackets from 0 to 9.

Correctly says ;-)
- allocates an array of 10 elements, x[10] ...
- the elements of an array of 10 elements are indexed like this (starting from 0): x[0], x[1], ... x[8], x[9] (10 pieces)
- indexing x[10] - outside the memory allocated to the array, the 11th element, a very gross mistake.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question