-3

I allocate 1 byte with malloc but i can put even more than 1 integer in this array which ptr pointing array's first element.How is this possible?

    int main(){
    int *ptr = (int*)malloc(sizeof(int));
    ptr[1] = 5;
    ptr[10] = 12;

    printf("%d %d\n",ptr[1], ptr[10]);
    }

output: 5 12

2 Answers2

0

C is a low-level language which trusts you to ensure that the code you write is safe. Unlike other higher-level languages, there are no checks at run time or compile time.

Bearing that in mind, in this case you have allocated memory for one integer (which is likely to be four bytes and not 1 byte.) Malloc gives you back a pointer which does not contain any information about the size of the block you have allocated.

ptr[0] = 5

is equivalent to writing

*ptr = 5

Note that in C the first element of an array is at 0 not 1.

There are no checks that ptr points to a valid block of memory.

Similarly when you write

ptr[10] = 5

that is equivalent to writing

*(ptr+10) = 5

This is undefined behaviour in this context and the compiler and runtime are permitted to do anything at all including produce the result you intend!

As to how this has worked, it is likely that the heap from which the integer was allocated contains enough space past the allocated space that you can write to without causing an access violation.

In a real program that space could be completely unused so there are no apparent ill effects (some of the time) or it could be in use by other code in the program or by the runtime to track allocations in the heap.

The lack of run time checks makes C very powerful and efficient but it also makes it easy to write code that is erratic or unsafe.

-1

I allocate 1 byte with malloc

You didn't allocate 1 byte. You allocate 1 int.

but i can put even more than 1 integer in this array

ptr[1] = 5;  
ptr[10] = 12;

It's called undefined behaviour.

Dereferencing a pointer that has not yet been definitely initialized

You can check this answer which has the list of the most common undefined behaviour in C++, some of which (including your case) apply to C as well.

https://stackoverflow.com/a/367662/4975822

artm
  • 17,291
  • 6
  • 38
  • 54
  • Where do you get the "Performing pointer arithmetic ..." stuff from? It's wrong. – EOF Sep 20 '20 at 12:27
  • @EOF yeah it's dereferencing – artm Sep 20 '20 at 12:29
  • Oh, even pointer arithmetic *can* be undefined (`ptr[10]` is undefined even without the dereferencing), but pointing *just after* the object is allowed, so that part of your answer was incorrect. – EOF Sep 20 '20 at 12:32
  • `is undefined even without the dereferencing` - is that right? if you do just `ptr + 10` ie just taking the address is is not UB, is it? – artm Sep 20 '20 at 12:38
  • or else please point to the source saying it so – artm Sep 20 '20 at 12:38
  • Yes it is undefined. C11 draft standard n1570: *6.5.6 Additive operators 8 When an expression that has integer type is added to or subtracted from a pointer [...] If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined[...]* – EOF Sep 20 '20 at 12:39