[英]pointer and which is pointed by the pointer
Update : Sorry, just a big mistake. 更新:抱歉,这是一个很大的错误。 It is meaningless to write
int *a = 3;
写
int *a = 3;
是没有意义的int *a = 3;
But please just think the analogy to the case like TCHAR *a = TEXT("text")
; 但请想一想像
TCHAR *a = TEXT("text")
; (I edited my question, so some answers and comments are strange, since they are for my original question which is not suitable) (我编辑了我的问题,所以一些答案和评论很奇怪,因为它们是我原来的问题,不合适)
In main function, suppose I have a pointer TCHAR *a = TEXT("text");
在main函数中,假设我有一个指针
TCHAR *a = TEXT("text");
Then it excutes the following code: 然后它会执行以下代码:
int i;
for (i = 0; i < 1000; i++) {
a = test(i);
}
with the function TCHAR* test(int par)
defined by: 使用函数
TCHAR* test(int par)
定义:
TCHAR* test(int par)
{
TCHAR *b = TEXT("aaa");
return b;
}
My question is, after executing the above code, but before the program ends, in the memory: 我的问题是,在执行上面的代码之后,但在程序结束之前,在内存中:
1. the pointer `a` remains?
2. The 1000 pointers `b` are deleted each time the function test(...) exits ?
3. But there are still 1000 memory blocks there?
In fact, my question is motivated from the following code, which shows a tooltip when mouse is over a tab item in a tab control with the style TCS_TOOLTIPS: 事实上,我的问题来自以下代码,该代码显示了当鼠标位于带有TCS_TOOLTIPS样式的选项卡控件中的选项卡项时的工具提示:
case WM_NOTIFY
if (lpnmhdr->code == TTN_GETDISPINFO) {
LPNMTTDISPINFO lpnmtdi;
lpnmtdi = (LPNMTTDISPINFO)lParam;
int tabIndex = (int) wParam; // wParam is the index of the tab item.
lpnmtdi->lpszText = SetTabToolTipText(panel->gWin.At(tabIndex));
break;
}
I am thinking if the memory usage increases each time it calls 我在想每次调用时内存使用量是否增加
SetTabToolTipText(panel->gWin.At(tabIndex))
, which manipulates with TCHAR
and TCHAR*
and return a value of type LPTSTR
. SetTabToolTipText(panel->gWin.At(tabIndex))
,它使用TCHAR
和TCHAR*
并返回LPTSTR
类型的值。
You dont allocate any memory so you don't have to worry about memory being freed. 你没有分配任何内存,所以你不必担心内存被释放。 When your vaiables go out of scope they will be freed automatically.
当您的vaiables超出范围时,它们将自动释放。 In this function
在这个功能
int test(int par)
{
int *b = par;
}
you don't have a return value even though the function says that is will return an int
, so you should probably do so as in this line 你没有返回值,即使函数说它会返回一个
int
,所以你应该这样做
for (i = 0; i < 1000; i++) {
a = test(i);
}
you assign to a
the value that is returned by test()
. 分配给
a
由返回的值test()
Also 也
int* a = 3;
int* b = par;
are asking for trouble. 正在惹麻烦。 You are assigning integer values to a pointer variable.
您正在为指针变量分配整数值。 You should probably rethink your above code.
您应该重新考虑上面的代码。
If SetTabToolTipText
allocates a string inside using some memory management facilities new/malloc or some os-specific, you should do an additional cleanup. 如果
SetTabToolTipText
使用一些内存管理工具new / malloc或某些特定于操作系统的内部分配字符串,则应该进行额外的清理。 Otherwise there'll be a memory leak. 否则会有内存泄漏。
If nothing like this happens inside (it's not mentioned in the documentation or comments etc), it's most likely returning the pointer to some internal buffer which you typically use as readonly. 如果内部没有发生这种情况(在文档或注释等中没有提到),则很可能将指针返回到您通常用作只读的内部缓冲区。 In this case, there should be no concerns about a memory consumption increase.
在这种情况下,不应该担心内存消耗增加。
指针应该包含地址...所以int* a = 3
是没有意义的......并且在函数中你没有为int分配内存(仅用于par变量,然后在函数结束时销毁),你分配内存用于存储在int* b
,当函数结束时,这个内存也是空闲的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.