【问题标题】:Error reading variable (cannot access memory at address) encountered when srand() called调用 srand() 时遇到读取变量错误(无法访问地址处的内存)
【发布时间】:2016-03-07 12:48:14
【问题描述】:

我正在使用带有 gcc 的 Codeblocks IDE 在 C 中进行并发编程试验。当我运行我的程序时,我没有收到任何输出。不过,有趣的是,当我在程序的某个点设置断点时,程序将执行该点之前的所有指令(包括向控制台输出值)。但是,之后,如果我尝试执行指令srand(time(NULL)),我正在观看的所有变量都会立即显示“读取变量时出错,无法访问地址 X 处的内存”并且调试过程会停止。

这是我的 main() 函数

/*main function*/
int main()
{
int k = 3;
int m = 100;
int n = 10;
int t = 10;

/*First, let's create the threads*/
pthread_t thread[numOfThreads];

/*Second, create the data struct*/
struct programData *data = malloc(sizeof(struct programData));

/*Initialize data struct*/
data->kPumps=k;
data->mVisitors=m;
data->nCars=n;
data->tTime=t; /*They'll drive visitor around for 10 units of time.*/

/*Now let's create the different threads*/
pthread_create(&thread[0], NULL, visitorThread, (void*)data);
pthread_create(&thread[1], NULL, carThread, (void*)data);
pthread_create(&thread[2], NULL, pumpThread, (void*)data);
pthread_create(&thread[3], NULL, gasTruck, (void*)data);

return 0;
}

还有我的访客线程

void *visitorThread(void *arg){
int arrayIndex;
int i;
/*Let's create mVisitors*/
struct programData *data;
data = (struct programData*)arg;
int numOfVisitors;
numOfVisitors = data->mVisitors;
/*create an array of visitors*/
struct visitor v[numOfVisitors];

/*Initialize values*/
for(i = 0; i < numOfVisitors; i++){
    v[i].id = i+1;
    v[i].isInCar = false;
    v[i].isInQueue = false;
}

printf("There are %d visitors at the San Diego Zoo \n", numOfVisitors);
printf("At first the visitors wait at the snake exhibit \n");
//sleep(5);
printf("Now some of them are getting bored, and want to get into a car to be shown the rest of the zoo \n");

/*After a random amount of time, some people line up to take cars*/
/*create queue*/
struct visitor* queue[numOfVisitors];
/*Initialize the array*/
for(i = 0; i < numOfVisitors; i++){
    queue[i] = NULL;
}

arrayIndex = 0;

/*While there are people in the snake exhibit*/
while(numOfVisitors >=1){
    /*After a random period of time, no more than 5 seconds...*/
//srand
    srand(time(NULL));
    int timeBeforePersonLeaves = rand()%5+1;
    //fflush(stdout);
    //sleep(timeBeforePersonLeaves);
    /*...a random person will get bored and enter the array line to be picked up by a car*/
    srand(time(NULL));
    int personIndex = rand() % numOfVisitors;
    queue[arrayIndex] = &v[personIndex];
    v[personIndex].isInQueue = true;
    printf("Visitor %d is now in queue spot %d \n", personIndex, arrayIndex);
    arrayIndex++;
}
return;
}

问题似乎存在于 while 循环中。如果我在 while 循环末尾的括号中放置一个断点,它将输出每个值。但是,如果将断点放在 while 循环中的任何位置,一旦它到达 srand 调用,就会导致同样的问题。对此的任何帮助将不胜感激,并提前致谢。

【问题讨论】:

  • while(numOfVisitors &gt;=1)。你有一个无限循环。导致arrayIndex 不断增长并溢出queue。另外,请阅读man page for srand。你只需要调用一次而不是连续调用。
  • 调用 srand 一次似乎已经解决了这个问题。谢谢!
  • 不幸的是,我怀疑这会解决您的潜在问题。它可能已经移动了问题,因此它不会在您当前的测试运行中触发(多线程应用程序经常发生)。但除非你解决了潜在的问题(例如无限循环),否则问题随时会再次困扰你。
  • 我应该注意到我添加了减少 numOfVisitors 的代码,这会停止 while 循环。
  • 好的,这比 srand 更改更有可能修复了您的 seg 错误。

标签: c concurrency pthreads srand


【解决方案1】:

while(numOfVisitors &gt;=1)。你有一个无限循环。导致arrayIndex 不断增长并溢出queue。另外,请阅读 srand 的手册页。您只需要调用一次而不是连续调用。 – 凯勒姆

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-12
    • 2017-08-28
    • 1970-01-01
    • 2019-03-26
    • 1970-01-01
    • 2018-09-08
    • 2013-02-18
    • 1970-01-01
    相关资源
    最近更新 更多