0

I have a question about the following code:

int age = 20;
void * pointer;
pointer = alloc(sizeof(int), 0)

pointer = (void*) age;

How does it work?

What is the value of pointer?

What happens with this piece of code in terms of the line :

pointer = (void*) age;

4

1 回答 1

7

This code accomplishes exactly nothing.

First, you allocated a pointer for a size of int, using non-standard allocation methods.

Then, you assign that pointer to point to the address 0x14, which probably doesn't contain any valid information, and would give you a SEGFAULT if you attempted to dereference it.

Third, you leak the initial memory you alloc'd for pointer, which is never a good thing.

Overall, a VERY bad design pattern.

于 2012-08-20T13:16:09.940 回答