-1

我已经为此挠头太久了。我看到了很多关于如何在 C 循环中处理随机生成的主题,但是当涉及两个循环时我什么也没看到。

下面是我的代码:

typedef struct
{
    char qh_uid[6];
    long int qh_vd;
    long int qh_pd;
    long int qh_id;
    double qh_value;
}quote_history;

int records_size = 20;
int batch_size = 5;

char *rand_str(char *dst, int size)
{
    static const char text[] = "abcdefghijklmnopqrstuvwxyz"
                               "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int i, len = size;
    for ( i = 0; i < len; ++i )
    {
        dst[i] = text[rand() % (sizeof text - 1)];
    }
    dst[i] = '\0';
    return dst;
 }

char *rand_tstp(char *dst, int size)
{
    static const char text[] = "0123456789";
    int i, len = size;
    for ( i = 0; i < len; ++i )
    {
        dst[i] = text[rand() % (sizeof text - 1)];
    }
    dst[i] = '\0';
    return dst;
}

double randfrom(double min, double max)
{
    double range = (max - min);
    double div = RAND_MAX / range;
    return min + (rand() / div);
}

quote_history *feed_batch(quote_history *qh_batch, int batchsize)
{
    int j;
    char mytext[6];
    char mylong[17];
    for (j = 0; j < batchsize; ++j)
    {
        quote_history qh_aux;
        printf("uid : %s - value : %lf\n",rand_str(mytext, sizeof mytext), randfrom(0,100000));
        strcpy(qh_aux.qh_uid, rand_str(mytext, sizeof mytext));
        qh_aux.qh_vd = strtol(rand_tstp(mylong, sizeof mylong), NULL, 10);
        qh_aux.qh_pd = strtol(rand_tstp(mylong, sizeof mylong), NULL, 10);
        qh_aux.qh_id = strtol(rand_tstp(mylong, sizeof mylong), NULL, 10);
        qh_aux.qh_value = randfrom(0,100000);
        qh_batch[j] = qh_aux;
    }
    printf("--------------\n");
    return qh_batch;
}

int
main(int           const argc,
     const char ** const argv) {

    quote_history *subArray;

    srand(time(NULL));
    int k;
    for (k = 0; k < (int)(records_size/batch_size); ++k) {
        subArray= (quote_history*)calloc(batch_size, sizeof(quote_history));
        subArray = feed_batch(subArray, batch_size);
        // Do something with subArray
        free(subArray);
    }
}

好吧,基本上,我想做的是生成 struct quote_history 的批次(作为数组),其中一些值是随机生成的,然后处理该批次并移至另一个批次。

出于某种原因,随机生成在 feed_batch 函数中运行良好,但是当它移动到循环中的下一个项目时,批次始终保持不变。

忘记粘贴代码的结果:

uid : qJfzrJ - value : 64938.995598
uid : LyCadK - value : 23030.096583
uid : dcOicU - value : 26016.211568
uid : BmpSTV - value : 76145.000279
uid : aQvABq - value : 92286.726130
--------------
uid : qJfzrJ - value : 64938.995598
uid : LyCadK - value : 23030.096583
uid : dcOicU - value : 26016.211568
uid : BmpSTV - value : 76145.000279
uid : aQvABq - value : 92286.726130
--------------
uid : qJfzrJ - value : 64938.995598
uid : LyCadK - value : 23030.096583
uid : dcOicU - value : 26016.211568
uid : BmpSTV - value : 76145.000279
uid : aQvABq - value : 92286.726130
--------------
uid : qJfzrJ - value : 64938.995598
uid : LyCadK - value : 23030.096583
uid : dcOicU - value : 26016.211568
uid : BmpSTV - value : 76145.000279
uid : aQvABq - value : 92286.726130
--------------

我尝试了许多组合(例如在循环中循环而不是 feed_batch 函数)但没有成功。

我希望有人能帮助我。

弗洛里安

4

2 回答 2

1

您的 main 分配了一个 quote_history 字段并在 for 循环中释放它。

feed_batch 获取这个指针并像访问数组一样访问它——>您的调试器应该报告内存访问错误。

新的猜测:
我知道为什么你认为它确实有效(printfs 是正确的)但在外面它不起作用。但这只是一个猜测,因为缺少代码:我猜你将 mytext 指针保存在你的结构中。所以最后你的所有数组元素都指向同一个字符串。更糟糕的是,字符串是一个自动变量,在退出 feed_batch 后会失去作用域。

顺便说一句:
为什么需要动态分配?只需声明一个 quote_history 对象并传递它的指针。或声明一个 quote_history 对象数组。这里不需要动态分配。

于 2013-06-18T15:04:09.357 回答
1

如果我理解您的代码,那么您缺少的是将随机生成的字符串复制到结构的数组qh_uid中。事实上你不需要复制,你可以直接在你的结构的缓冲区中生成随机字符串。

quote_history *feed_batch(quote_history *qh_batch, int batchsize)
{
    int j;
    for (j = 0; j < batchsize; ++j)
    {
        rand_str(qh_batch[j].qh_uid, sizeof(qh_batch[j].qh_uid) - 1);
        printf("uid : %s - value : %lf\n", qh_batch[j].qh_uid, randfrom(0,100000));
    }
    printf("--------------\n");
    return qh_batch;
}

Sander De Dycker 在评论中提到的也是真的,你不能写 at qh_uid[6],你可以写的最大值是qh_uid[5],(这样做,除了对齐考虑之外,你实际上覆盖了 的第一个字节qh_vd),记住数组索引从0. 因此-1添加到传递给的大小rand_str

还要考虑 vlad_tepesch 关于分配的答案。

于 2013-06-18T15:29:10.483 回答