I have been thinking, that does anybody know what is the difference in
machine code between normal array memory allocation:
int some_array1[10];
compared to dyanamical allocation:
int* some_array2 = new int[10];
I mean, what does the compiler do to know that in the first case the memory
cannot be deleted using "delete" and in the second one delete can be done?
Compiler will happily allow you to write "delete some_array;" (or
"delete [] some_array"), but doing so is undefined behavior. IOW...
This is completely out of compiler's hands.
You should also note that :
perator delete (global one) receives a
void* and is consequently rather underwhelming.
When you call operator delete yourself, you need to pass it a pointer
to some type. You can't ever pass it "an array", because AFAIK, in
order for an array to go into any function/operator, it's first
converted to a pointer to it's first element.
When you call operator delete, it calls destructor of the object, then
deallocates storage (this is why :
perator delete is less
interesting: it can't know what destructor to call. When you call
operator[] delete, it calls as many destructors as objects allocated
by the corresponding call to operator new[]. Manner used to allocate
storage in new must match the manner to deallocate storage in delete
(important when you overload new/delete).
In the first case the memony is allocated "permanently"...Is there just
somekind of "flag" by which the probram defines that the some_array1 is a
"fixed" memory? What is the dirrennce between compiling some_array1and
some_array2 (in machine code level)?
There's no flag. It's the duty of the code not to mix things up;
dragons await if it does mix them. That said... There are three
"storage classes" in C and C++:
static (objects that exist for the whole program duration; you declare
them outside any function, or as static class members; their lifetime
is in hands of the compiler)
automatic (objects that exist in a scope; you declare them inside a
code block, {here}; perhaps class members count as automatic, I am not
sure; their lifetime is in hands of the compiler, too)
dynamic (objects allocated/deallocated with new/delete or free/malloc
for C; these are placed into free store, or heap, and their lifetime
is in hands of the code - they are destroyed when code calls operator
delete on a corresponding pointer).
Finally, there's function-static objects, as in void f() { ... static
int i; ...} I don't know how they are classified.
Goran.