使用睡眠功能后内存出错

问题描述 投票:0回答:2

有一个char**数组来存储structs,里面有ID,action和current time。因此,每一秒之后,都会有一个新客户。但是当我迭代 char** 数组时,时间输出将与上一次相同。

#include <stdio.h>
#include <time.h>

char* get_current_time(){
    time_t current_time;
    struct tm* time_info;
    static char time_string[9];

    /*Get the current time. */
    current_time = time(NULL);
    
    /*Convert the local time to local time. */
    time_info = localtime(&current_time);
    
    /*Format the time string. */
    strftime(time_string, sizeof(time_string), "%H:%M:%S", time_info);
    
    return time_string;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "getTime.h"

typedef struct cusDetails
{
    int cusId;
    char cusAct;
    char* cusTime;
}cusDetails;


int main(int argc, char *argv[])
{
    int numCus = atoi(argv[1]);
    int i;
    char* cusActList = "ABCDEFGHI";
    
    cusDetails* customers = malloc(numCus*sizeof(cusDetails));
    
    for (i = 0; i <numCus; i++)
    {
        customers[i].cusId = i+1;
        customers[i].cusAct = cusActList[i];
        customers[i].cusTime = get_current_time();
        printf("The cutomer in time %s\n", customers[i].cusTime);

        sleep(1);
    }
    
    for (i = 0; i <numCus; i++)
    {
        printf("Customer: %d, for %c, at %s\n", customers[i].cusId, customers[i].cusAct, customers[i].cusTime);
    }

    return 0;
}

输入“./a.out 5”,输出为: 及时的客户 09:20:58 及时的客户 09:20:59 客户及时 09:21:00 及时的客户 09:21:01 及时的客户 09:21:02

Customer: 1, for A, at 09:21:02 客户:2,B,时间为 09:21:02 客户:3,C,09:21:02 客户:4,D,09:21:02 客户:5,代表 E,时间为 09:21:02

请帮忙告诉我那里有什么帮助?以及如何防止它发生

c for-loop time malloc sleep
2个回答
1
投票

customers[i].cusTime = get_current_time();
复制一个指针,每次都是同一个指针。
printf("... %s\n", ..., customers[i].cusTime);
然后总是打印由同一指针引用的内容。

相反,复制 string.

typedef struct cusDetails {
    int cusId;
    char cusAct;
    // char* cusTime;
    char cusTime[9];
}cusDetails;

// customers[i].cusTime = get_current_time();
strcpy(customers[i].cusTime, get_current_time());

0
投票

由于您的 time_string 是固定长度,我建议您将

char *cusTime
更改为
char cusTime[TIME_LEN]
以反映这一点。
static
变量有点代码味道,因为它不是线程安全的。这就是为什么我只是将字符串传递给
get_current_time()
而不是要求调用者复制它,正如您发现的那样,这是一个错误的修剪设计。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#define TIME_LEN 9

typedef struct cusDetails {
    int cusId;
    char cusAct;
    char cusTime[TIME_LEN];
} cusDetails;

void get_current_time(char time_string[TIME_LEN]){
    time_t current_time;
    struct tm* time_info;

    /*Get the current time. */
    current_time = time(NULL);

    /*Convert the local time to local time. */
    time_info = localtime(&current_time);

    /*Format the time string. */
    strftime(time_string, TIME_LEN, "%H:%M:%S", time_info);
}

int main(int argc, char *argv[]) {
    if(argc != 2) {
        printf("argument required\n");
        return 1;
    }
    int numCus = atoi(argv[1]);
    char* cusActList = "ABCDEFGHI";

    cusDetails* customers = malloc(numCus*sizeof(cusDetails));
    for (int i = 0; i <numCus; i++) {
        customers[i].cusId = i+1;
        customers[i].cusAct = cusActList[i];
        get_current_time(customers[i].cusTime);
        printf("The cutomer in time %s\n", customers[i].cusTime);
        sleep(1);
    }
    for (int i = 0; i <numCus; i++)
        printf("Customer: %d, for %c, at %s\n", customers[i].cusId, customers[i].cusAct, customers[i].cusTime);
    return 0;
}

还添加了对

argc
的检查,这样如果您忘记指定所需的数字参数,您的程序就不会出现段错误。顺便说一句,更喜欢
strtol()
或者写你自己的
atoi()
默默地将错误转换为 0.

倾向于最小化变量的范围。我在使用它的两个循环中都制作了

int i
本地。这也避免了这两个循环是否通过
i
进行通信的问题(有时您使用它来计算元素的数量)。

不固定,但更喜欢将变量而不是类型传递给

sizeof
。它通常较短,无论类型如何,它们都遵循相同的模式:

struct type *t = malloc(sizeof *t * count);
© www.soinside.com 2019 - 2024. All rights reserved.