循环中的循环&;c语言中的随机生成
我在这件事上挠头太久了。 我看到了许多关于如何在C循环中进行随机生成的主题,但是当涉及两个循环时,我没有看到任何内容 下面是我的代码:循环中的循环&;c语言中的随机生成,c,for-loop,random,srand,C,For Loop,Random,Srand,我在这件事上挠头太久了。 我看到了许多关于如何在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; cha
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);
}
}
我尝试过许多组合(例如,在循环中循环,而不是feed_batch函数),但都没有成功
我希望有人能在这方面帮助我
Florian您的main分配一个quote_history字段,并在for循环中释放它
feed_批处理获取该指针并将其作为数组进行访问-->调试器应报告内存访问错误
新猜测:我有一个想法,为什么你认为它确实有效(printfs是正确的),但在外部它不起作用。但这只是猜测,因为代码缺失: 我猜您将mytext指针保存在您的结构中。因此,在最后,所有数组元素都指向同一个字符串。更糟糕的是,string是一个自动变量,在退出feed\u批处理后会失去作用域 顺便说一句:
为什么需要动态分配? 只需声明quote_history的对象并传递其指针。
或声明quote_历史对象数组。这里不需要动态分配。如果我理解您的代码,您缺少的是将随机生成的字符串复制到结构的数组
qh\u 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;
}
quote_history*feed_batch(quote_history*qh_batch,int batchsize)
{
int j;
对于(j=0;j
Sander De Dycker在评论中提到的也是事实,你不能在qh_-uid[6]
处写入,你可以在qh_-uid[5]
处写入的最大值是qh_-uid[5]
(这样做,除了对齐方面的考虑,实际上你正在覆盖qh_-vd
的第一个字节),记住数组索引从0
开始。因此,将-1
添加到传递给rand_str
的大小中
也考虑VLADHETTEPSH的分配问题。
这不是你的全部代码。例如,没有定义<代码> QHYAUX。这是不编译的。什么是qh_aux?由rand_str
编写的'\0'
终止符将超出调用函数的mytext
数组的范围。小心!这可能与您的问题无关,但在rand_str()
中,您正在使用dst[i]='\0'写入缓冲区的末尾代码>声明。抱歉,我截取了评论行,忘了复制qh_aux的声明。我已经编辑了它为batch\u size
(即5)quote\u history
对象分配空间,不仅仅是1感谢您的输入,您是对的,我将mytext变量保存在我的结构中,正如您现在在代码中看到的那样。首先,我没有在printf中重定向random的结果,我这样做只是为了调试目的。我使用strcpy和strtol重定向我的结构中随机生成的项,正如您现在在代码中看到的那样,为什么要在qh_aux
中工作,然后复制?你为什么不直接在你想要填充的内存上工作呢?嗯,因为我没想到…:-(值得一提的是,将填充直接替换到qh_批处理中也不会有太多工作……我将看看vlad_tepesch所说的无用的动态分配。你的代码对我来说就是这样。你确定你编译了正确的文件吗?在正确的目录中工作吗?或者其他类似的蠢事?
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;
}