Click here to Skip to main content
15,904,156 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to build a programme about drone which user can enter either default mode or manual mode .. and in default mode i want the programme to further asking user to input Object,imei,and video frame rates ( fstream method ) and actual frame rate (by pass reference) .When I build it it only stated warning which is "Warning C6001 Using uninitialized memory 'flyLevel' line 54" and still succeed and then I debug it and suddenly those error appeared "check failure#2-Stack around variable 'object ' was corrupted"and"check failure#2-Stack around variable 'id' was corrupted".I have google it and i dont understand why this happens.Can anyone explain to why this happens and how to solve this? thank you so much

C++
#include<iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <string.h>

using namespace std;

void manualMode();
void defaultMode();
void Getdata(double& camrecord, double& projectfps);
void calculate(double& camrecord, double& projectfps, double& actual);
void Displayactualframerate(double& out);



int speed, timeTofly, operation;
int angle = 90;
double camrecord ;
double projectfps ;
double actual, out ;

int main() {
    int mode = 0, part = 3;
    double flyLevel;
    int obstacleSensor = 0, timer = 3, AccelerometerSensor = 0, tempSensor = 25;
    char startPause;
    cout << "Drone Flying Technology" << endl;
    do {
        cout << "Choose Mode: (1)Default, (2)Manual \n";
        cin >> mode;
        if (mode == 1)
            defaultMode();
        if (mode == 2)
            manualMode();
    } while (mode == 0);

    cout << "Choose how to fly: (1)Upper , (2)Lower , (3)Upper and Lower \n";
    cin >> part;
    switch (part) {
    case 1:
    case 2:
        flyLevel = 0.5;
        break;
    case 3:
        flyLevel = 1;
        break;
    }
    if (obstacleSensor == 0) {
        do {
            cout << "Press (S) to Start,and swing the propeller." << endl;
            cin >> startPause;
            cout << "LED is On\n";
            while (flyLevel != AccelerometerSensor) {
                
                AccelerometerSensor++;
            }
            cout << "Flying session Started! Time Left:" << timeTofly << endl;
            cout << "Eagle eye Operation Started!\n";
            timeTofly = timeTofly / 2;
            cout << "Crusing mode initiate Time Left:" << timeTofly << endl;
            timeTofly = timeTofly / 2;
            cout << "Free fly commenced Time Left:" << timeTofly << endl;
            timeTofly = 0;
            startPause = 'P';
        } while ((startPause == 's') || (startPause == 'S'));
    }
    cout << "End!\nLED is Off";
    return 0;
}


void manualMode() {
    cout << "Enter drone speed (knot) \n";
    cin >> speed;
    cout << "Enter Time to fly: \n";
    cin >> timeTofly;
    cout << "Choose Operation: (1)Fully manual, (2)Normal Orientation, (3)Free orientation, (4)FPV racing, (5)All\n";
    cin >> operation;
}
void defaultMode() {
    int howtofly;
    cout << "how to fly: (1)Circle, (2)Altitude Hold, (3)Free orientation\n";
    cin >> howtofly;
    switch (howtofly) {
    case 1:
        speed = 30;
        timeTofly = 3;
        break;
    case 2:
        speed = 40;
        timeTofly = 3;
        break;
    case 3:
        speed = 60;
        timeTofly = 3;
        break;
    }


    char object[5];
    char id[5];
    float m1, m2, m3, m4, m5;

    ofstream outputFile("droneinfo.txt", ios::out);

    cout << "Please enter object you want to record,set your drone id number (5 number) and your 5 preferred video fps:\n";
    

    cin >> id >> object >> m1 >> m2 >> m3 >> m4 >> m5;
    
     outputFile << id << " " << object << " " << m1 << " " << m2 << " " << m3 << " " << m4 << " " << m5<<endl;
    



    Getdata(camrecord, projectfps);
    calculate(camrecord, projectfps, actual);
    Displayactualframerate(actual);



}
void Getdata(double& camrecord, double& projectfps) {

    

    cout << "Please enter cam recording =" << camrecord << "f/s ,\nDesired footage frame rate="
        << projectfps<<"f/s\n";
    cin >> camrecord;
    cin >> projectfps;
}


//-------------------//
void calculate(double& camrecord, double& projectfps, double& actual) {
    actual = (camrecord / projectfps);
}


//-------------------//
void Displayactualframerate(double& out) {

    cout << "\nThe actualframerate is =" << out << " f/s\n";

   
}


What I have tried:

I try to change all "flyLevel" to another string and it doesn't work
Posted
Updated 26-Jan-23 18:06pm
Comments
KarstenK 22-Jun-20 5:23am    
try double flyLevel = 0;

Quote:
When I build it it only stated warning which is "Warning C6001 Using uninitialized memory 'flyLevel' line 54"

This means that if part is not 1, 2 or 3, flylevel do not get initialyzed.
C++
switch (part) {
case 1:
case 2:
    flyLevel = 0.5;
    break;
case 3:
    flyLevel = 1;
    break;
default:
    // if code goes here, it is a problem, add a check
}

Quote:
error appeared "check failure#2-Stack around variable 'object ' was corrupted"and"check failure#2-Stack around variable 'id' was corrupted".

Keyboard input to a char array means that the char array is used as a zero terminated string, it means that for an input of 5 char, you need a char array of size 6.
 
Share this answer
 
Comments
CPallini 22-Jun-20 7:55am    
5.
Patrice T 22-Jun-20 7:56am    
Thank you
We can't tell - we have no idea what your app is designed to do, how you use it, or what you did in terms of inputs to get an error to occur.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.
Look at the stack, see what is happening to the data around your variable. The chances are that you are somewhere passing a bad pointer, or using a hanging reference - but you will only spot that by looking very closely at exactly what is going on while your code is running - and that means the debugger.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900