由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C++ Interview Question
相关主题
在子函数内开内存,返回主函数指针然后释放空间是不是很糟糕的(转载)这个怎么allocate memory?
关于C/C++里的Static variable的memory allocation/initializaWhat're the three types of memory allocated for C++ variables?
C++一个string的小问题一个 default constructor 的问题
effective C++里的memory pool 一问:问一个C++ set和unordered_set iterator的问题
问个简单的memory allocation 的问题。关于内存泄漏
question about structure initializationa and reference内存分配问题
why do we still use dynamic allocation?a small question about c++ memory allocation
sizeof(string)请教关于allocator member function 的问题
相关话题的讨论汇总
话题: int话题: p1话题: p2话题: question话题: c++
进入Programming版参与讨论
1 (共1页)
s***i
发帖数: 49
1
int * var = new int;
*var=5;
k***r
发帖数: 4260
2
heap vs stack?

【在 s***i 的大作中提到】
: int * var = new int;
: *var=5;

l*****d
发帖数: 359
3
auto variable (life time from definition to end of block) vs. dynamic allocated variable (you control the life time)

【在 s***i 的大作中提到】
: int * var = new int;
: *var=5;

c**m
发帖数: 30
4
depending on where the lines appear:
a:
int *p1 = new int;
int p2 = 5;
int main() {}
Then both p1,p2 are some space reserved in data segment, but p1's address
may have more requirements such as alignment since it's a pointer. p2's
value is stored as 5, however p1's value is inited with an address in heap
by calling alloc during static initialization before main is called.
b:
void foo()
{
int *p1 = new int;
int p2 = 5;
...
}
p1,p2 could be space reserved on stack or simply registers. p2 could b
c**m
发帖数: 30
5
and of course if you don't delete p1, you have memory leak. :)
l*****d
发帖数: 359
6
it's about the memory p1 pointed to and p2, not p1 and p2.

【在 c**m 的大作中提到】
: depending on where the lines appear:
: a:
: int *p1 = new int;
: int p2 = 5;
: int main() {}
: Then both p1,p2 are some space reserved in data segment, but p1's address
: may have more requirements such as alignment since it's a pointer. p2's
: value is stored as 5, however p1's value is inited with an address in heap
: by calling alloc during static initialization before main is called.
: b:

c**m
发帖数: 30
7
that's obvious.
but question asks for difference behind the scene, no mention of anything
specific about memory p1 pointed to and p2 (candidate is supposed to figure
that out). and there're very big differences depending on where they appear
. besides, there's nothing stopping me from making var a pointer to an
integer at location 5:
int var =5;
*((int*)var) = 5;
also, I always thought sizeof(int) == sizeof(void*) but apparently not
because visual studio 64-bit sizeof(int) == 4 and sizeof(voi

【在 l*****d 的大作中提到】
: it's about the memory p1 pointed to and p2, not p1 and p2.
1 (共1页)
进入Programming版参与讨论
相关主题
请教关于allocator member function 的问题问个简单的memory allocation 的问题。
什么是OS Memory management and heap structure?question about structure initializationa and reference
一道 memset in C++的题why do we still use dynamic allocation?
a=(char **)malloc(12*sizeof(char *)) 是什么意思?sizeof(string)
在子函数内开内存,返回主函数指针然后释放空间是不是很糟糕的(转载)这个怎么allocate memory?
关于C/C++里的Static variable的memory allocation/initializaWhat're the three types of memory allocated for C++ variables?
C++一个string的小问题一个 default constructor 的问题
effective C++里的memory pool 一问:问一个C++ set和unordered_set iterator的问题
相关话题的讨论汇总
话题: int话题: p1话题: p2话题: question话题: c++