Issue With Encryption Function Not Linking To Main

微笑、不失礼 提交于 2019-12-12 04:18:57

问题


For the purpose of education, I'm writing a small pair of functions which open a file as a binary file, read each character one at a time, change the value of said character and write to a new, encrypted file. (Note, this isn't true encryption per se.)

For some reason, I'm getting this error message in my debugger at the while-loop condition:

Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)

I suspect the issue is with understanding pointer. Here is my program in it's entirety. Thanks ahead of time.

//  This program reads the contents of a file, encrypts it, and write the contents into a separate file.

#include <iostream>
#include <fstream>
#include <cctype>
#include <cstring>
#include <cmath>

using namespace std;

// Global Constants
const int POSITIVE_INT_LIMIT = 10;
const int NEGATIVE_INT_LIMIT = -10;
const int SLOPE_NEGATIVE = -1;
const int SLOPE_POSITIVE = 1;
const int STEP = 1;

// Function Prototypes
void encrypt(fstream *unEncryptedFile);
fstream *fileOpener();

int main() {

    encrypt(fileOpener());

    return 0;
}

/****************************************************
 *                  fileOpener()                    *
 *   Opens file with error checking and displays    *
 *   message if error, then exiting gracefully.     *
 ****************************************************/
fstream *fileOpener(){

    fstream inFile; // Create fstream object
    inFile.open("crypty.txt", ios::in | ios::binary); // Open file


    if (!inFile) {
        cout << "Error opening file." << endl;
        return NULL;

    } else {

        fstream *filePtr; // create a pointer to the file.

        filePtr = &inFile; // Pass the address of inFile to the pointer.

        return filePtr;
    }


}


/***************************************************
 *                    encrypt()                    *
 *    Encrypts a file with a simlpe algorithm.     *
 ***************************************************/ 
void encrypt(fstream &unEncryptedFile){

    char ch, cy;
    char *chPtr = &cy; // Initialize and assign char pointer.

    fstream cryptFile;
    cryptFile.open("decrypy.txt", ios::out | ios::binary);

    int amplitude = 0, slope = SLOPE_NEGATIVE; //Set initial amplitude and slope.

    int streamSize = 1;

/*      Encryption pattern inside while-loop.

 limit>     10                             *
            9                             * *
            8                            *   *
            7                           *     *
            6                          *       *
            5                         *         *
            4                        *           *
            3                       *             *
            2                      *               *
            1                     *                 *
 start>     0*2345678901234567890*2345678901234567890* -- < one hertz (cycle)
           -1 *                 *
           -2  *               *   (Number line: each integer represents a single while loop cycle.)
           -3   *             *
           -4    *           *
           -5     *         *
           -6      *       *
           -7       *     *
           -8        *   *
           -9         * *
 limit>    -10         *

 */
/*************************************************************
 The pattern above depictes a single character
 being read, and then the value of amplitude is added to it.

 *************************************************************/

    while (!unEncryptedFile.fail()) { // <--Code reports bug at this statement.

        ch = unEncryptedFile.get(); // Get the next character in the file.


        if (amplitude > NEGATIVE_INT_LIMIT && slope == SLOPE_NEGATIVE) {
            amplitude -= STEP;
            cy = ch + amplitude;
            cryptFile.write(chPtr, streamSize); //Adjusted character value, cy, is written to file.

        } else if (amplitude <= NEGATIVE_INT_LIMIT){
            slope = SLOPE_POSITIVE;
            amplitude = NEGATIVE_INT_LIMIT;
            cy = ch + amplitude;
            cryptFile.write(chPtr, streamSize); //Adjusted character value, cy, is written to file.


        } else if (amplitude < POSITIVE_INT_LIMIT && SLOPE_POSITIVE){
            amplitude += STEP;
            cy = ch + amplitude;
            cryptFile.write(chPtr, streamSize); //Adjusted character value, cy, is written to file.

        } else if (amplitude >= POSITIVE_INT_LIMIT){
            slope = SLOPE_NEGATIVE;
            amplitude = POSITIVE_INT_LIMIT;
            cy = ch + amplitude;
            cryptFile.write(chPtr, streamSize); //Adjusted character value, cy, is written to file.
        }

    }

//Files are closed.
unEncryptedFile.close();
cryptFile.close();

}

回答1:


I suspect the issue is with understanding pointer.

Probably, almost you right, but with understanding of pointers to local variables.

When you define local variables like this:

fstream inFile; // Create fstream object

inFile is allocated on the stack.

With following:

filePtr = &inFile

... you'll get pointer to some place on the stack in filePtr.

And when you exit that function:

return filePtr;

... stack portion, associated with function call will be returned to heap and filePtr will point to unused memory address, which will be overriden by next called function/executed code.

So, why not just return inFile itself?



来源:https://stackoverflow.com/questions/34034426/issue-with-encryption-function-not-linking-to-main

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!