0
當我運行該程序時,出現以下錯誤: 運行時檢查失敗#2 - 變量out_result周圍的堆棧已損壞。 我試着打印out_result的長度來檢查緩衝區是否有溢出,但長度總是小於CHAR_MAX_LENGTH = 512 有沒有想法?運行時檢查失敗#2 - 變量周圍的堆棧已損壞
void GetCPUOutResult(FILE* cpu_out_file_pointer,char* out_result)
{
char temp[CHAR_MAX_LENGTH+1] = {0};
int char_index =fgetc(cpu_out_file_pointer) ,index=0;
while (((char)char_index) != '\n')
{
temp[index]=(char)char_index;
char_index =fgetc(cpu_out_file_pointer);
index++;
}
temp[index]='\0';
sprintf_s( out_result,
MAX_CMD_LINE_LENGTH,
temp,
NULL);
}
void WorkerThreadFunction(THREAD_PARAMETERS* parameters)//LPVOID lpParam
{
//THREAD_PARAMETERS* parameters = (THREAD_PARAMETERS *) lpParam;
char *mission;
char cpu_file_name_in[CHAR_MAX_LENGTH];
char cpu_file_name_out[CHAR_MAX_LENGTH];
char out_result[CHAR_MAX_LENGTH+1]={0};
FILE *cpu_in_file_pointer, *cpu_out_file_pointer;
HANDLE mutexes_handles[NUM_MUTEXES];
int thread_id=parameters->thread_id;
char *log_file_name=parameters->log_file_name;
HANDLE youHaveWorkToDo=parameters->youHaveWorkToDoSemaphore[thread_id];
HANDLE readyToAcceptNewWork=parameters->readyToAcceptNewWorkSemaphore[thread_id];
//open file cpu<threadID>.in for writing
cpu_file_name_in[ 0 ] = '\0';
GenerateCPUFileName(cpu_file_name_in,"in",thread_id);
cpu_in_file_pointer = _fsopen(cpu_file_name_in, "w", _SH_DENYNO);
//open file cpu<id>.out for reading
cpu_file_name_out[ 0 ] = '\0';
GenerateCPUFileName(cpu_file_name_out,"out",thread_id);
cpu_out_file_pointer = _fsopen(cpu_file_name_out, "r", _SH_DENYNO);
while (TRUE)
{
printf("Worker Thread: %d is waiting for work from dispatcher\n",thread_id);
WaitForDispatcherToGiveMeWorkToDo(youHaveWorkToDo);
THREADS_STATUS_ARRAY[thread_id]=0;/////////////////////////////////////////////////////////////////
printf("Worker Thread: %d GOT work from dispatcher\n",thread_id);
mission= WORKER_THREADS_BUFFER[thread_id];
printf("Worker Thread: %d is executing mission %s\n",thread_id,mission);
//pre mission execution writing to log file with lock
mutexes_handles[0] = AquireSingleInstanceLock(FIRST_INSTANCE_MUTEX_NAME);//Set LOCK
printf("Worker Thread: %d is trying to witre to INPUT LOGFILE\n",thread_id);
WriteToLogFile(log_file_name,
thread_id,
" started running mission ",
mission);
printf("Worker Thread: %d wrote successfully into INPUT LOGFILE\n",thread_id);
ReleaseMutex(mutexes_handles[0]);//free LOCK
//Write mission from worker thread buffer into cpu<id>.in
sprintf(mission,"%s\n",mission);
fwrite(mission, sizeof(char), strlen(mission), cpu_in_file_pointer);
fflush(cpu_in_file_pointer);//flush data from accoiated buffer to the file
printf("Worker Thread: %d wrote successfully in cpu%d.in core\n",thread_id,thread_id);
//get result from cpu<id>.out
printf("Worker Thread: %d is trying to get result from CPU<Id>.OUT\n",thread_id);
Sleep(50);
GetCPUOutResult(cpu_out_file_pointer,out_result);
printf("Worker Thread: %d got result:%s from CPU<Id>.OUT\n result length is:%d\n",thread_id,out_result,strlen(out_result));
///post mission execution writing to log file with lock
printf("Worker Thread: %d trying to write result:%s into OUT LOG FILE\n",thread_id,out_result);
mutexes_handles[1] = AquireSingleInstanceLock(SECOND_INSTANCE_MUTEX_NAME);//Set LOCK
WriteToLogFile(log_file_name,thread_id," is done! Mission result is ",out_result);
ReleaseMutex(mutexes_handles[0]);//free LOCK
printf("Worker Thread: %d successfully wrote result:%s into OUT LOG FILE\n",thread_id,out_result);
printf("Worker Thread: %d successfully executed mission:%s \n",thread_id,mission);
if (strcmp(mission,"quit\n") == 0 )
{
break;
}
else
{
InformDispatcherThreadIsReadyToAcceptWork(readyToAcceptNewWork,
(LPLONG)&THREADS_STATUS_ARRAY[thread_id]);
THREADS_STATUS_ARRAY[thread_id]=1;/////////////////////////////////////////////////////////////////
}
}
return;
}
什麼行錯誤occ你在嗎? –
哪種語言?請選擇一個。 –
文件的最後一行是否在最後有一個\ n?爲防萬一,請檢查EOF。 – doctorlove