2012-01-29 422 views
0

我有一個應用程序似乎正在執行所有代碼,我仍然給出它,但仍然提示錯誤「運行時檢查錯誤2 - 堆棧'VAL'已損壞。」我讀過這可能是因爲它正在獲得一個任務,但我檢查了所有的輸入,它們都是有效的。堆棧周圍變量已損壞。這是什麼意思?

IM在好奇什麼其他可能導致此?

我覺得罪魁禍首可能是許多地方。 ExtractVals方法是開始的地方,我添加了一個printf來測試索引,並且所有的東西似乎都被檢出。沒有越界呼叫。此方法根據輸入到該方法的字符串(它是一個tolkenizer方法)爲數組的每個元素重新賦值。也許這可能會導致錯誤?

任何提示將是偉大的。

#include <stdio.h> 
#include <stdlib.h> 
#include <gl/glut.h> 
#include <gl/gl.h> 
#include <gl/glu.h> 
#include <string.h> 
#include <ctype.h> 


void makeLower(char *input); 
void extractVals(char *cmd, float *val); 

FILE *file; 
int g_mainWindow = -1; 
float g_lightPos[] = {1, 1, -1, 0}; 
char commands [50][50]; 
int fileSize = -1; 
int objectdrawn = 0; 

int testint = 0; 

void display() 
{ 
    int i; 
    char cmdTok[10] , *cmd = cmdTok; 
    float val[5]; 
    char commandCpy[10]; 

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glLoadIdentity(); 

    if (objectdrawn == 0){ 

     for(i = 0 ; i <= fileSize ; i++){ 
      strcpy(commandCpy, commands[i]); 
      printf("command = %s\n", commands[i]); 
      cmd = strtok(commandCpy, " \n\0"); 
      printf("command = %s\n", commands[i]); 
      switch(*cmd){ 
      case 'g'://translate object 
       extractVals(cmd , val); 
       glTranslatef(val[0] , val[1] , val[2]); 
       break; 
      case 's'://scales an object 
       extractVals(cmd , val); 
       if (val[4] == 1.){ 
        glScalef(val[0],val[0],val[0]); 
       } 
       else if (val[4] == 3.){ 
        glScalef(val[0] , val[1] , val [2]); 
       } 
       break; 
      case 'r'://rotates an object 
       break; 
      case 'c'://this can call draw cone , cube, or change colors. 
       if(strcmp(cmd , "cone") == 0){ 
        //printf("drawing a cone\n"); 
        glColor3f(1,0,0); 
        glutSolidCone(.5 , 1 , 8, 1); 
       } else if (strcmp(cmd , "cube") == 0){ 
        //glutSolidCube(1); 
       } else if (*cmd == 'c'){ 
        extractVals(cmd , val); 
        glColor3f(val[0] , val[1], val[2]); 
       } 
       break; 
      case 't'://draw a torus or tea pot 
       break; 
      case 'o'://reads a meshfile 
       break; 
      case 'f'://save current frame buffer. 
       break; 
      case 'm': 
       break; 
      } 

     } 

     objectdrawn = 1; 
     i = -1; 
     printf("Loop Done!"); 
    } 



    glFlush(); 
    glutSwapBuffers(); 
} 

void reshape(int w, int h) 
{ 
    float aspect = w/(float)h; 

    glViewport(0,0,w,h); 
    glMatrixMode(GL_PROJECTION_MATRIX); 
    glLoadIdentity(); 
    glOrtho(-aspect, aspect, -1, 1, -1, 1); 
    glMatrixMode(GL_MODELVIEW_MATRIX); 
} 

void idle() 
{ 
    /* parse a command from file */ 
    /* store the data for later draw */ 
    char linebyline [50], *lineStr = linebyline; 
    int i=0; 

    while(!feof(file) && file != NULL){ 
     fgets(lineStr , 50, file); 
     makeLower(lineStr); 
     strcpy(commands[i] , lineStr); 
     printf("lineStr = %s\n", lineStr); 
     printf("command = %s\n", commands[i]); 
     fileSize = i; 
     i++; 

    } 


    glutSetWindow(g_mainWindow); 
    glutPostRedisplay(); 
} 

void makeLower(char *input) 
{ 
    while (*input != '\0') 
    { 
     *input = tolower(*input); 
     input++; 
    } 
} 

/* 
Using a tolenizer this extracts out values needed for other functions to draw. 
*/ 
void extractVals(char *cmd, float *val){ 
    int i=0; 
    cmd = strtok(NULL, " ,"); 
    while(cmd != NULL){ 
     val[i] = atof(cmd); 
     printf("val[%d] is %s\n", i , cmd); 
     cmd = strtok(NULL, " ,"); 
     i++; 
     testint++; 
     printf("Ran this method %d times with a index of %d\n", testint, i); 
    } 

    //printf("Extracted values %f %f %f\n", val[0] , val[1] , val[2]); 

    val[4] = i--; 
} 


int main(int argc, char **argv) 
{ 
    file = fopen(argv[1], "r"); 

    glutInit(&argc, argv); 

    glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH); 
    g_mainWindow = glutCreateWindow("Hello, glut"); 
    glClearColor(0.5, 0.5, 0.5, 0); 
    glEnable(GL_LIGHTING); 
    glEnable(GL_LIGHT0); 
    glEnable(GL_DEPTH_TEST); 
    glLightfv(GL_LIGHT0, GL_POSITION, g_lightPos); 

    glutDisplayFunc(display); 
    glutReshapeFunc(reshape); 
    glutIdleFunc(idle); 

    glutMainLoop(); 
    fclose(file); 
} 

產出瓦爾斯

val[0] is 0 
val[1] is 0.5 
val[2] is 0 

val[0] is 0.25 

val[0] is 1 
val[1] is 1 
val[2] is 1 

val[0] is 4 

val[0] is 0 
val[1] is -0.5 
val[2] is 0 

val[0] is 1 
val[1] is 1 
val[2] is 4 

val[0] is 1 
val[1] is 1 
val[2] is 0 
+1

'extractVals'的打印輸出是什麼?它不包含防止超出允許範圍的安全。 – 2012-01-29 05:21:01

+0

爲ExtractVals添加了輸出 – meriley 2012-01-29 05:23:43

回答

1

這是Visual Studio的幫助你了:)

你做的東西,搗毀了堆棧。

commandCpy []和cmdTok []中顯示()是兩個的第一個地方我開始尋找的。

在CMD [],這是在傳遞給ExtractVals()一個未終止的字符串,也是看的好地方:)

步驟通過在MSVC調試器的代碼 - 這應該帶你直接到問題!

+0

我運行的程序沒有使用來自Argv的文件IO,它給了我更多的信息。這個問題確實是commandCpy – meriley 2012-01-29 19:37:02