Home > Back-end >  VS2019 use strlen () function, why much more behind the code
VS2019 use strlen () function, why much more behind the code

Time:11-12

In use today mallco () and strlen (), the distribution of space is always greater than the need space, causes the output always garbled,

If the strlen () to sizeof (), displays not loaded WNTDLL. The PBD or VS don't respond,

 
Int main ()
{
/* const char * flag;
Char * outflag;
Flag="ZmxhZ3tzaG91a2FrdX0=";
Outflag=(char *) malloc (BASE64_DECODE_OUT_SIZE (strlen (flag)));
Base64_decode (flag, strlen (flag), outflag);
Puts (outflag);
Free (outflag);
return 0; */
Char * flag;
Flag="ZmxhZ3t9CgoK";
Char * outflag=(char *) malloc (strlen (flag));
for (int i=0; I & lt; The strlen (flag); I++)
{
Outflag [I]=flag [I] ^ 1;
}
Puts (flag);
Puts (outflag);
Free (outflag);
return 0;
}

Want to ask you, how to solve this problem

CodePudding user response:

 # include & lt; stdio.h> 
#include
#include

Int main ()
{
/* const char * flag;
Char * outflag;
Flag="ZmxhZ3tzaG91a2FrdX0=";
Outflag=(char *) malloc (BASE64_DECODE_OUT_SIZE (strlen (flag)));
Base64_decode (flag, strlen (flag), outflag);
Puts (outflag);
Free (outflag);
return 0; */
int i;
Char * flag;
Flag="ZmxhZ3t9CgoK";
Char * outflag=(char *) malloc (strlen (flag) + 1);
For (I=0; I & lt; The strlen (flag); I++)
{
Outflag [I]=flag [I] ^ 1;
}
Outflag [I]=0;//add '\ 0'
Puts (flag);
Puts (outflag);
Free (outflag);
return 0;
}


For your reference ~

Pay attention to '\ 0', garbled words, for the string, mostly because '\ 0' cause (not at the end of the string corresponding plus' \ 0 'cause)

CodePudding user response:

String must have the terminator '\ 0', otherwise it would have been input into memory information string terminator
So
Puts (flag);
Outflag [strlen (flag)]='\ 0';//this additional set string terminator
Puts (outflag);

In addition, the flag [I] ^ 1 could lead to a result is display character, thus outflag display content does not necessarily can see






  • Related