Dear all
I have the following code which is written in C and compiled
under gcc in both Linux and Solaris. However, it run differently
from the two platform.
In Linux, the gcc is 2.96 and the result is
Length = 8, value = YYYYMMDD
Length = 4, value = YYYY
Length = 4, value = MMDD
Length = 2, value = MM
In Solaris, the gcc verison is unknown to me and the result is
Length = 8, value = YYYYMMDD
Length = 4, value = YYYY
Length = 4, value = MMDD
Length = 4, value = MM
In Solaris, the code also seems to have memory overrun.
Can anyone help ?
#include <string.h>
#include <stdlib.h>
main(int argc, char **argv)
{
char *strTmp_1;
char *strTmp_2;
char *strTmp_3;
char strTmp[9] = "YYYYMMDD";
printf("%s%d%s%s\n", "Length = ", strlen(strTmp),
", value = ", strTmp);
strTmp_1 = (char*) malloc(strlen("YYYYY"));
strTmp_1 = strncpy(strTmp_1, strTmp, strlen("YYYY"));
printf("%s%d%s%s\n", "Length = ", strlen(strTmp_1),
", value = ", strTmp_1);
strcpy(strTmp, strTmp + strlen("YYYY"));
printf("%s%d%s%s\n", "Length = ", strlen(strTmp),
", value = ", strTmp);
strTmp_2 = (char*) malloc(strlen("MM") + strlen("\0"));
strTmp_2 = strcat((char *) memcpy(strTmp_2, strTmp,
strlen("MM")), "\0");
printf("%s%d%s%s\n", "Length = ", strlen(strTmp_2),
", value = ", strTmp_2);
}