Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7182487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:46:52+00:00 2026-05-28T17:46:52+00:00

I have an application that appears to be executing all the code Ive given

  • 0

I have an application that appears to be executing all the code Ive given it, but still prompting an error “Run Time Check Error 2 – stack around ‘val’ was corrupted.” I have read this could be because it is getting an assignment out of its bounds, but I checked all inputs and they are all valid.

Im curious on what else could cause this?

I feel the culprit could be in a number of places. ExtractVals method is where ive started, and Ive added a printf to test the indexes and all seems to check out. No out of bounds calls. This method does reassign values to each element of the array depending on the string inputted to the method (Its a tolkenizer method). Maybe this could be causing the error?

Any tips would be great.

#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);
}

Outputs for Vals

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:46:53+00:00Added an answer on May 28, 2026 at 5:46 pm

    This is Visual Studio helping you out 🙂

    You did something that trashed the stack.

    commandCpy[] and cmdTok[] in display() are two of the first places I’d start looking.

    An unterminated string in cmd[], which is passed in to ExtractVals(), is also an excellent place to look 🙂

    Step through the code in the MSVC debugger – that should take you directly to the problem!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a production web application that appears to be having a thread stuck
I have application that makes different queries with different results so the caching in
I have application that is connecting to the DB and if I enter incorrect
I have application that is up more than 3 days. I can see in
I have application that brings response via Ajax and creates 5-20 new jQuery click
I have an application that sends messages to an external web service. I build
I have an application that displays an image inside of a Windows Forms PictureBox
I have an application that reads a CSV file with piles of data rows.
I have an application that uses NHibernate as its ORM and sometimes it experiences
I have an application that I would like to embed inside our companies CMS.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.