- 论坛徽章:
- 0
|
原帖由 baohuaihuai 于 2007-7-3 14:04 发表 ![]()
是unsigned的原因吧.
的确是unsigned的原因,但是我不知道在哪里设置可以解决这个问题。
我写的测试代码
test.c
#include <time.h>
#include <stdio.h>
#define UINT8 unsigned char
void getFormatTime(UINT8 *str_time, time_t time_stamp);
int main()
{
time_t time_stamp = 0;
UINT8 time_str[32] = {0};
UINT8 buf[4] = {0x00,0x00,0x00,0x00};
memset(buf,0,4);
time_stamp = 1179974176;
getFormatTime(time_str, time_stamp);
printf("time_str = %s \n", time_str);
return 0;
}
void getFormatTime(UINT8 *str_time, time_t time_stamp)
{
struct tm tm_time;
if(str_time == NULL)
{
return;
}
if(time_stamp == 0)
{
str_time[0] = '\0';
return;
}
localtime_r(&time_stamp, &tm_time);
sprintf(str_time, "%d-%d-%d %d:%d:%d", (tm_time.tm_year+1900), tm_time.tm_mon+1, tm_time.tm_mday, tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec);
}
[root@EMC prac]# gcc -g -w -Wall -o test test.c
[root@EMC prac]# gdb test
(gdb) b main
Breakpoint 1 at 0x80483c4: file test.c, line 9.
(gdb) r
Starting program: /home/root/prac/test
Breakpoint 1, main () at test.c:9
9 {
(gdb) n
main () at test.c:10
10 time_t time_stamp = 0;
(gdb) n
11 UINT8 time_str[32] = {0};
(gdb) n
12 UINT8 buf[4] = {0x00,0x00,0x00,0x00};
(gdb) n
13 memset(buf,0,4);
(gdb) p buf
$1 = "\000??
(gdb) p time_str
$2 = "\003綷000@", '\0' <repeats 27 times>
(gdb) n
16 time_stamp = 1179974176;
(gdb) p buf
$3 = "\000??
(gdb)
可以看出unsigned char数组,初始化无效,memset也无效。
同样的代码在另外一台机子上是可以,两台电脑是用同一张光盘装得(redhat),只是出问题的电脑前一阵好像编译过什么冬冬,不知道为什么就坏了。
下面是在另外一台电脑上gdb结果
(gdb) n
9 unsigned char time_str[32] = {0};
(gdb) n
10 unsigned char buf[4] = {0x00,0x00,0x00,0x00};
(gdb) n
11 memset(buf,0,4);
(gdb) p time_str
$1 = '\0' <repeats 31 times>
(gdb) p buf
$2 = "\000\000\000"
(gdb) n
14 time_stamp = 1179974176;
(gdb) p buf
$3 = "\000\000\000"
(gdb)
[ 本帖最后由 ChinaPeter 于 2007-7-4 09:40 编辑 ] |
|