In the following program
在以下程序中
int main(){
char * time;
struct tm timeptr;
t.tm_sec = 10;
t.tm_min = 10;
t.tm_hour = 6;
t.tm_mday = 25;
t.tm_mon = 2;
t.tm_year = 89;
t.tm_wday = 6;
time = asctime(&timeptr);
puts(time);
}
Where the memory to string returned by asctime() is allocated? I mean is it allocated in heap or caller/called function's stack or where else? if its in heap then do I need to free it? if its in the asctime's stack how it is accessible to me even if the function has returned the control to me? if its in caller function's stack how did it not corrupt any data of the callers stack?All I understood is it has to be allocated in user space.
在哪里分配了asctime()返回的内存字符串?我的意思是它分配在堆或调用者/被调用函数的堆栈中还是在其他地方?如果它在堆中那么我需要释放它吗?如果它在asctime的堆栈中,即使函数已将控件返回给我,我也可以访问它?如果它在调用函数的堆栈中它是如何不破坏调用者堆栈的任何数据的?我理解的是它必须在用户空间中分配。
3 个解决方案
#1
0
The returned value is calculated from the static area; it's not Heap. hence, subsequent calls to asctime would modify the string pointed by an earlier returned pointer. No need to free the returned pointer.
返回值是从静态区域计算出来的;这不是堆。因此,对asctime的后续调用将修改先前返回的指针指向的字符串。无需释放返回的指针。
#2
0
In the asctime
documentation at cppreference says:
在cppreference的asctime文档中说:
Return value pointer to a static null-terminated character string holding the textual representation of date and time. The string may be shared between
asctime
andctime
, and may be overwritten on each invocation of any of those functions.返回值指向静态以null结尾的字符串,该字符串包含日期和时间的文本表示形式。字符串可以在asctime和ctime之间共享,并且可以在每次调用任何这些函数时被覆盖。
#3
0
asctime
returns a pointer to a string.
asctime返回指向字符串的指针。
There are two possible questions that can be asked.
可以提出两个可能的问题。
- Where the pointer is allocated?
- Where is the pointed-to string is allocated?
指针分配的位置?
指向字符串的位置在哪里?
The answers are:
答案是:
- Where the compiler writer decides to allocate it (normally in a register, but for larger return types it could be on the stack).
- Where the function writer decides to allocate it. It could reasonably be either a static array or a heap-allocated array. In order to know, you need to read the function documentation. For
asctime
it's a static arrray. For other functions it may be something else.
编译器编写者决定分配它的地方(通常在寄存器中,但对于较大的返回类型,它可能在堆栈中)。
函数编写者决定分配它的位置。它可以合理地是静态数组或堆分配数组。要知道,您需要阅读功能文档。对于asctime来说,它是一个静态的arrray。对于其他功能,它可能是其他功能。
#1
0
The returned value is calculated from the static area; it's not Heap. hence, subsequent calls to asctime would modify the string pointed by an earlier returned pointer. No need to free the returned pointer.
返回值是从静态区域计算出来的;这不是堆。因此,对asctime的后续调用将修改先前返回的指针指向的字符串。无需释放返回的指针。
#2
0
In the asctime
documentation at cppreference says:
在cppreference的asctime文档中说:
Return value pointer to a static null-terminated character string holding the textual representation of date and time. The string may be shared between
asctime
andctime
, and may be overwritten on each invocation of any of those functions.返回值指向静态以null结尾的字符串,该字符串包含日期和时间的文本表示形式。字符串可以在asctime和ctime之间共享,并且可以在每次调用任何这些函数时被覆盖。
#3
0
asctime
returns a pointer to a string.
asctime返回指向字符串的指针。
There are two possible questions that can be asked.
可以提出两个可能的问题。
- Where the pointer is allocated?
- Where is the pointed-to string is allocated?
指针分配的位置?
指向字符串的位置在哪里?
The answers are:
答案是:
- Where the compiler writer decides to allocate it (normally in a register, but for larger return types it could be on the stack).
- Where the function writer decides to allocate it. It could reasonably be either a static array or a heap-allocated array. In order to know, you need to read the function documentation. For
asctime
it's a static arrray. For other functions it may be something else.
编译器编写者决定分配它的地方(通常在寄存器中,但对于较大的返回类型,它可能在堆栈中)。
函数编写者决定分配它的位置。它可以合理地是静态数组或堆分配数组。要知道,您需要阅读功能文档。对于asctime来说,它是一个静态的arrray。对于其他功能,它可能是其他功能。