Please fix my program

I tried this out and I had no build/compile errors but when I run it a message appears that says "test.exe has stopped working" and when I close that the program returns 0xC0000005

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <cstdlib>
#include "funcs.h"

using namespace std;

char** encrypt(char raw[]);

char** decrypt(char enc[]);

int main()
{
    char hi[] = "hello world!";

    char** res = encrypt(hi);

    cout<<*(*res+1);

    return 0;
}

char** encrypt(char raw[1])
{
    int ran1, ran2, ran3, ran4;

    ran1 = rand() % 100;
    ran2 = rand() % ran1;
    ran3 = rand() % 1 + ran2;
    ran4 = ran1 + (ran3*2);

    size_t si_of = sizeof(raw);

    char** enc = NULL;

    for(size_t i = 0; i<si_of; i++)
    {
        *(*(enc+0)+i) = up(ran4, raw[i]);
    }

    char buffenc[4] = {(char)ran1, (char)ran2, (char)ran3, (char)ran4};

    *(*(enc+1)) = *buffenc;

    return enc;
}
You need to allocate memory for your pointers before you can use them.
Dynamic allocation? If so which ones? I did this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
char** encrypt(char raw[1])
{
    int ran1, ran2, ran3, ran4;

    ran1 = rand() % 100;
    ran2 = rand() % ran1;
    ran3 = rand() % 1 + ran2;
    ran4 = ran1 + (ran3*2);

    size_t si_of = sizeof(raw);

    char** enc;
    enc = new char*[si_of*2];

    for(size_t i = 0; i<si_of; i++)
    {
        *(*(enc+0)+i) = up(ran4, raw[i]);
    }

    char* buffenc;

    buffenc = new char[5];

    *buffenc = (char)ran1, (char)ran2, (char)ran3, (char)ran4;

    *(*(enc+1)) = *buffenc;

    delete[] buffenc;

    return enc;

    delete[] enc;
}


and my build messages have:

||=== Build: Debug in Apoxx (compiler: GNU GCC Compiler) ===|
C:\Users\Bob\OneDrive\Code\Apoxx\main.cpp||In function 'char** encrypt(char*)':|
C:\Users\Bob\OneDrive\Code\Apoxx\main.cpp|50|warning: right operand of comma operator has no effect [-Wunused-value]|
C:\Users\Bob\OneDrive\Code\Apoxx\main.cpp|50|warning: right operand of comma operator has no effect [-Wunused-value]|
C:\Users\Bob\OneDrive\Code\Apoxx\main.cpp|50|warning: right operand of comma operator has no effect [-Wunused-value]|
||=== Build finished: 0 error(s), 3 warning(s) (0 minute(s), 1 second(s)) ===|
||=== Run: Debug in Apoxx (compiler: GNU GCC Compiler) ===|

line 50 is
*buffenc = (char)ran1, (char)ran2, (char)ran3, (char)ran4;
Last edited on
Did you know about the comma operator?
http://www.tutorialspoint.com/cplusplus/cpp_comma_operator.htm

Also line 32 is unreachable and is also the wrong method of deallocating memory for a type pointer to pointer
I patched it up a bit. Now it's the same as the beginning (no errors but crashes when run with return value 0xC0000005)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
char** encrypt(char raw[1])
{
    int ran1, ran2, ran3, ran4;

    ran1 = rand() % 100;
    ran2 = rand() % ran1;
    ran3 = rand() % 1 + ran2;
    ran4 = ran1 + (ran3*2);

    size_t si_of = sizeof(raw);

    char** enc;
    enc = new char*[si_of*2];

    for(size_t i = 0; i<si_of; i++)
    {
        *(*(enc+0)+i) = up(ran4, raw[i]);
    }

    char* buffenc;

    buffenc = new char[5];

    *(buffenc+0) = (char)ran1;
    *(buffenc+1) = (char)ran2;
    *(buffenc+2) = (char)ran3;
    *(buffenc+3) = (char)ran4;

    *(*(enc+1)) = *buffenc;

    delete[] buffenc;

    return enc;

    for (size_t d = 0; d<sizeof(enc); d++)
    {
    delete[] enc[d];
    }
}


Using cout I narrowed the crash is caused by memory allocation on line 22. I used a try/catch block but the program still crashed.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
char** encrypt(char raw[1]) {
    int ran1, ran2, ran3, ran4;

    ran1 = rand() % 100;
    ran2 = rand() % ran1;
    ran3 = rand() % 1 + ran2;
    ran4 = ran1 + (ran3*2);

    size_t si_of = sizeof(raw); // This is always 1, why bother?

    char** enc;
    enc = new char*[si_of*2];

    for(size_t i = 0; i<si_of; i++) {

        // Need to allocate memory first
        --> enc[i] = new char[1/* A number should go here*/];

        *(*(enc+0)+i) = up(ran4, raw[i]); // enc[0][i] = up(ran4, raw[i]); // This is where your crash happens
    }

    char* buffenc;

    buffenc = new char[5];

    *(buffenc+0) = (char)ran1;
    *(buffenc+1) = (char)ran2;
    *(buffenc+2) = (char)ran3;
    *(buffenc+3) = (char)ran4;

    *(*(enc+1)) = *buffenc;

    delete[] buffenc;

    return enc;

    // After the above line executes, nothing else down here is reached
    for (size_t d = 0; d<sizeof(enc); d++) {
        // Note that ^ sizeof(enc) will not give you 2 so even more segfault for you

        // Why do you even want to do this? This will cause another segfault somewhere else if executed
        delete[] enc[d];
    }
}
Last edited on
Thank you very much. I'm a beginner so I did not know any of that. :|
Topic archived. No new replies allowed.