Jump to content

Talk:Comparison of Object Pascal and C

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by 107.33.42.66 (talk) at 06:20, 15 August 2013 (A mistake...: new section). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
WikiProject iconComputing Redirect‑class
WikiProject iconThis redirect is within the scope of WikiProject Computing, a collaborative effort to improve the coverage of computers, computing, and information technology on Wikipedia. If you would like to participate, please visit the project page, where you can join the discussion and see a list of open tasks.
RedirectThis redirect does not require a rating on Wikipedia's content assessment scale.
WikiProject iconC/C++ NA‑class
WikiProject iconThis redirect is within the scope of WikiProject C/C++, a collaborative effort to improve the coverage of C and C++ topics on Wikipedia. If you would like to participate, please visit the project page, where you can join the discussion and see a list of open tasks.
NAThis redirect does not require a rating on Wikipedia's content assessment scale.

A mistake...

quotation
Despite of its treatment as pointer, not all pointer style constructs could be used to array. For example, this code would compile fine but would cause access violation when executed:

void func(int *a) {
// RUNTIME ERROR! a is allocated statically
a = (int*) malloc(sizeof(int) * 10);
}

int main() {
int a[5];
func(a);
}
end of quotation

No, it is not a run time error. Remember that C/C++ passes parameters by value. So, "a" within "func" is a local copy of the pointer.
a = (int*) malloc(sizeof(int) * 10);
allocates the new memory, and assigns the memory address to "a", which is a local variable (as a byval parameter).

It is a memory leak, because the memory is allocated, and not freed. However, by itself, it will not cause an access violation.

If you were to declare "a" as const:
void func(int * const a) {
then you would get a compiler error. 107.33.42.66 (talk) 06:20, 15 August 2013 (UTC)[reply]