-4

This is my code (C++).

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <sstream>
using namespace std;

void rotaryxorencrypt(int dat[],int len){
------------------------------
}
void encrypt(int dat[],int len){
    rotaryxorencrypt(dat,len);
}

void decrypt(int dat[],int len){
}

int main() {
    fstream file;
    fstream *fileptr=&file;
    file.open("tmp",ios::in);
    string line;
    string *lineptr=&line;
    int x;
    int *xptr=&x;
    int cont=0;
    int *contptr=&cont;
    int len;
    int *lenptr=&len;
    stringstream ss;
    stringstream *ssptr=&ss;
    string cryption;
    string *cryptionptr=&cryption;
    getline(*fileptr,*lineptr);
    try{
        if(*lineptr=="encryption"){
            *cryptionptr="encrypt";
        }else if(*lineptr=="decrypt"){
            *cryptionptr="decryption";
        } else {
            cout<<"Unknown Cryptography Type - "<<*lineptr<<endl;
            throw 0;
        }
        getline(*fileptr,*lineptr);
        *ssptr<<*lineptr;
        *ssptr>>*xptr;
        ss.str("");
        ss.clear();
        *lenptr=*xptr;
        int *dataptr;
        dataptr=new int[*lenptr];
        cout<<"Loading Formatted Data"<<endl;
        while ( getline (*fileptr, *lineptr) ) {
            *ssptr<<*lineptr;
            *ssptr>>*xptr;
            ss.str("");
            ss.clear();
            dataptr[cont]=*xptr;
            cont++;
        }
        file.close();
            delete lineptr;
        delete xptr;
        delete contptr;
        delete ssptr;
        delete fileptr;
        ------------------
        if(*cryptionptr=="encrypt"){
            cout<<"Beginning Encryption Process."<<endl;
            cout<<dataptr[0]<<endl;
            encrypt(dataptr,len);
            cout<<dataptr[0]<<endl;
            cout<<"Outputting Encrypted Data."<<endl;
        }else if(*cryptionptr=="decrypt"){
            cout<<"Beginning Decryption Process."<<endl;
            decrypt(dataptr,len);
            cout<<"Outputting Decrypted Data."<<endl;
        }
        cout<<"Clearing Cache."<<endl;
        delete []dataptr;
        delete cryptionptr;
        delete lenptr;
    }catch(int x){
        if(x==0){
            cout<<"Clearing Cache."<<endl;
            delete fileptr;
            delete lineptr;
            delete xptr;
            delete contptr;
            delete ssptr;
            delete fileptr;
        }else{
            cout<<"Unknown Error - Cannot Clear The Cache."<<endl;
        }
    }
    cout<<"here"<<endl;
    return 0;
    cout<<"here"<<endl;
}

Note the cout<<"here"<<endl; before and after the return 0;. Without them I have the same issue, so they are not the issue, but it will execute the first cout<<"here"<<endl; but will crash before the second. If I remove the second, it does the same thing, and if I remove the first it just crashes, therefore it is crashing on the return 0;. WHY IS THIS HAPPENING. (P.S. This is part to another project in encryption (possibly sensitive parts [not crash points or code errors] were turned into "-----------------" (this is not the actual code).

Justin
  • 121
  • 1
  • 4
  • 3
    Please make a [mcve]. It doesn't help to omit code, when you've dumped 100 lines otherwise. What is your exact output? – Tas Apr 15 '16 at 01:58
  • What you describe doesn't sound like crashing. You're surprised that **return**ing from your main routine ends your program? – Disillusioned Apr 15 '16 at 02:09
  • My exact output is (\n means that there is an enter which I can't type): Loading Formatted data\nBeginning Encryption Process.\n140\n140\nOputputting Encrypted Data.\nClearing Cache.\nhere\n(this is where it crashes but the rest is printed after telling windows (7) to 'close program' as it is being run in debug mode) \nProcess returned 255 (0xFF) execution time : 13.743 s\nPress any key to continue. (end output). For giving you the rest of the code any code that I omitted worked before the crash started so it isn't the issue and it also is sensitive (this is part to an encryption program) – Justin Apr 15 '16 at 02:10
  • For the returning, Windows displays a window that says "Encryption.exe has crashed." for the title (it is a .exe file because it is compiled (my compiler is CNU GCC Compiler) and for the rest of it it says "Checking for a solution to the problem." so no, it is not returning, it is indeed crashing – Justin Apr 15 '16 at 02:12
  • 2
    It is crashing because you are using delete on pointers that you did not allocate with new. – drescherjm Apr 15 '16 at 02:14
  • 5
    @Justin Why do we have to dig this information out of you? – Disillusioned Apr 15 '16 at 02:15

1 Answers1

3

Get rid of all the pointers and all the deletes. Nothing here was created with new so there's nothing to delete. Okay, there's dataptr which should be deleted. But that's awfully hard to find in all the dereferencing noise.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • While I don't want to delete all the pointers because using pointers is faster and one of the key principles is that speed is key, removing all of the dereferencing except for that for the `dataptr` worked. Thank you so much. – Justin Apr 15 '16 at 02:17
  • An apology is in order, I am experienced with python and batch, but this is my second day using anything harder than those. – Justin Apr 15 '16 at 02:17
  • 4
    @Justin Actually "faster and one of the key principles" is **not** a key principle, understandability by the next developer is **the** key principle. As for speed consider the words of Donald Knuth: "The real problem is that programmers have spent far too much time worrying about efficiency in the wrong places and at the wrong times; premature optimization is the root of all evil (or at least most of it) in programming." – zaph Apr 15 '16 at 04:16
  • 3
    @Justin - it's not the dereferencing that caused the problems, it's the `delete`s of things that weren't created with `new`. That said, code that creates `auto` objects then immediately creates pointers to them is noisy and pointless. There is no speed difference between using `file` and using `*file_ptr`. – Pete Becker Apr 15 '16 at 12:03