简体   繁体   English

运行时检查失败#2-变量周围的堆栈-已损坏

[英]Run-Time Check Failure #2 - Stack around the variable — was corrupted

When I run This program I get the following error: Run-Time Check Failure #2 - Stack around the variable 'out_result' was corrupted. 当我运行该程序时,出现以下错误:运行时检查失败#2-变量'out_result'周围的堆栈已损坏。 I tried to print the length of out_result to check if there was an overrun to the buffer, but the length is always less than CHAR_MAX_LENGTH =512 Any ideas? 我尝试打印out_result的长度,以检查缓冲区是否溢出,但是该长度始终小于CHAR_MAX_LENGTH = 512,有什么想法吗?

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;

}

Change this line 更改此行

sprintf_s(out_result, MAX_CMD_LINE_LENGTH, 
        temp, 
        NULL); 

to become this: 成为这个:

sprintf_s(out_result, CHAR_MAX_LENGTH, 
        "%s"
        temp); 

For details on sprintf_s() please read here: http://msdn.microsoft.com/en-us/library/ce3zzk1k.aspx (there also are examples at the end of the page) 有关sprintf_s()详细信息,请在此处阅读http : //msdn.microsoft.com/zh-cn/library/ce3zzk1k.aspx (页面末尾还有示例)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行时检查失败 #2 - 变量“IDNumber”周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'IDNumber' was corrupted 运行时检查失败#2-变量&#39;indices&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'indices' was corrupted 运行时检查失败#2-变量&#39;result&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'result' was corrupted 运行时检查失败#2-变量&#39;numberchoices&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'numberchoices' was corrupted 运行时检查失败#2-变量&#39;ex&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'ex' was corrupted 运行时检查失败#2-变量&#39;NearID&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'NearID' was corrupted 运行时检查失败-变量周围的堆栈已损坏 - Run-Time Check Failure - Stack around variable was corrupted 运行时检查失败#2-变量&#39;s&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 's' was corrupted 运行时检查失败#2 - 变量&#39;B&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'B' was corrupted 运行时检查失败 #2 - 变量“newRow”周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'newRow' was corrupted
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM