I am new to c++ and just read some online tutorial about it. I am pretty curious about my experiment result in pointer. Here is my code:
int *p_value;
sizeof(*p_value) <--- I expected 0 but it shows 4
p_value = new int;
sizeof(*p_value) <--- it shows 8, make sense
delete(p_value)
sizeof(*p_value) <--- I expect 0, but it shows 4 again...
More importantly, I don't understand why does p_value
point to the same memory address after delete operation. Is it necessary to be true?
Thanks for pointing out my misunderstanding over the operator "sizeof". But here is something I didn't state it too clear in my question.
here is another code: int *p_value = new int; cout << p_value <-- it prints the address of the new created int, say 0x000A; delete p_value; cout << p_value <-- it shows the same address before the "delete" called
Can i consider the "delete" just mark the 0x000A is not occupied in Map, I guess. But it won't change the content of the p_value?
Your results cannot be true. All your cases output size
of type int
in bytes
(and sizeof(int)
cannot be changed due program run).
I think the key thing your missing is that sizeof
is not a function. It doesn't look at the value of its argument at run time like a function does, just its type at compile time.
int *p_value;
sizeof(*p_value) <--- I expected 0 but it shows 4
Since p_value
is a pointer to an integer, *p_value
is an integer. Integer are four bytes on your platform.
p_value = new int;
sizeof(*p_value) <--- it shows 8, make sense
That's odd. Since p_value
is a pointer to an integer, *p_value
is an integer. So apparently, integers are 8 bytes on your platform.
delete(p_value)
sizeof(*p_value) <--- I expect 0, but it shows 4 again...
Why do you expect 0? Since p_value
is a pointer to an integer, p_value
is an integer. So its size is 4 (or however many bytes an integer occupies on your platform). How else would p_value = malloc (sizeof (*p_value));
work?
more importantly, I don't understand why p_value point to the same memory address after delete operation. Is it necessary to be true?
Passing the value of a variable to a function doesn't change the value of that variable. So delete (p_value)
doesn't change the value of p_value
, except it now points to garbage.
All of the results returned by the above sizeof(* ptr)
give you information about the size of the type it points to, specific to your platform , specifically the size of int
, ie 4
means size: 4 bytes or 32 bits, thus the int
can hold up to 2 32 - 1-sign-bit different numbers.
You could think of the returned results as results from: sizeof(int)
, which won't change regardless of the location* of your int
of whether you free dynamically allocated memory, with delete
or not, fact that, as @ForEveR, already said, makes your results dubious**.
*Static, stack or heap memory.
**Checked on two different platforms and the result is 4
, not 8
.
int *p_value;
sizeof(*p_value) <--- I expected 0 but it shows 4
*p_value
is an integer, the size is typically 4 bytes, thus it shows 4. This does depends on the compiler too (C++ standard only mentioned that it should be at least 4 bytes). My test returns 8 though.
p_value = new int;
sizeof(*p_value) <--- it shows 8, make sense
If the above shows 4, then this one should shows 4 as well (unless you're using different compiler/target arch). The sizeof operator shows the number of bytes taken by the data type. The value itself is irrevalent.
delete(p_value)
sizeof(*p_value) <--- I expect 0, but it shows 4 again...
The same as above.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.