Home > Back-end >  The statement
The statement

Time:03-05

# include & lt; Windows. H>
# include & lt; Stdio. H>
# include & lt; Stdlib. H>
# include & lt; String>
# include & lt; Time. H>

using namespace std;
Char * GetCurTimestring ()
{
Char w_content [64].
SYSTEMTIME Tm;
GetLocalTime (& amp; Tm);
Memset (w_content, 0, sizeof (w_content));
Sprintf_s (w_content "% % 4 d/02 d 02/02 % d % d: % 02 d: % 02 d. % 3 d", Tm. WYear, Tm. WMonth,
Tm. WDay, Tm wHour, Tm. WMinute, Tm. WSecond, Tm. WMilliseconds);
//char * strCurTime=w_content;
//char * T=new char [64].
//strcpy_s (T, 64, strCurTime);
//return T;
Return w_content;
}
Int main ()
{
Char * time=GetCurTimestring ();
//the following code is the code
Printf (" % s \ n ", time).
Sleep(1000);
Char * time2=GetCurTimestring ();
Printf (" % s \ n ", time2);
Sleep(1000);
Char * time3=GetCurTimestring ();
Printf (" % s \ n ", time3);
Getchar ();
return 0;
}

Vs compilation, why can only display 8 characters, the others are all gibberish, call the function to comment after is ok

CodePudding user response:

Char w_content [64]. Are comment part is on the stack memory, the memory allocated on the heap, again, your memory is not released

CodePudding user response:

the static char w_content [64].

CodePudding user response:

refer to the second floor 4 teacher zhao response:
the static char w_content [64].
say the principle

CodePudding user response:

reference 1st floor sevancheng response:
char w_content [64]. Is the memory on the stack, the annotation part is again the memory allocated on the heap, note that your memory didn't release oh
release is the release after the call is that char *, or how to release

CodePudding user response:

Internet reference 3 floor and illegal land response:
Quote: refer to the second floor 4 teacher zhao response:
the static char w_content [64].
it is principle to say

Add static let w_content keep in global data rather than the stack,

CodePudding user response:

W_content is local variables, return to the operation of the local variable is undefined, the scope of the local variable is a function of their own, starting from the definition to the end of the function call ended, this is the life cycle,
Therefore, plus the static is to change its life cycle, make its statement cycle procedure over and over (starting from the definition); Using the space on the heap, the reason is that the space has the user (people who write code) to apply for, to decide when to release, that is, life cycle is flexible, but if the program exits, the heap space will be recycling system, release,
  • Related