bad ptr array cannot be evaluated

Hey guys,

So im running into problems when trying to execute the encrypt function of my program.

The error im getting is Unhandled exception at 0x00bb185e in Assignment 2.exe: 0xC0000005: Access violation reading location 0x00000001.

what i think the problem is, is that the function can't access the cypherbet array for some reason.

I tried to make it static thinking that it was because the array got thrown away when the function ended aswell as making it dynamic but neither of these fixed it.

im also getting CXX0030: Error: expression cannot be evaluated and bad ptr errors in the debugger when i break and check the locals.

so im thinking there may be something wrong with how im creating the array or something.

i've included my entire program incase i messed up somewhere earlier and thats messing this part up.

but the array is made in the makeCypher function then passed to the encrypt function.

oh the all the arrays work previously as they all print info out at some point its just in the encrypt function they mess up.

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
using namespace std;

int getLength(char* formattedArray)
{
	int arraySize =0;
	for(int f = 0; f < formattedArray[f] && formattedArray[f] != '/0'; f++)
	{
		arraySize++;
	}
	cout << arraySize;
	return arraySize;
}

void makeCypher(int shiftVal)
{

	char alphabet[27] = "abcdefghijklmnopqrstuvwxyz";
	static char *cypherBet = NULL;
	cypherBet = new char[27];

	int loopNum;
	cout << "The plain text alphabet is: \n";
	for(int g = 0; g < 26; g++)
	{
		cout << alphabet[g];
	}
	cout << endl;
	cout << "This is the Cypertext alphabet: \n";
	for (int h=0; alphabet[h]; h++)
	{
		if((alphabet[h] +shiftVal) > 'z')
		{
			loopNum = ((alphabet[h]+shiftVal)-'z');
			cypherBet[h] = alphabet[loopNum-1];
		}
		else
		{
			cypherBet[h] = alphabet[h] + shiftVal;
		}
		cout << cypherBet[h];
	}
	cout<<endl;
}

void encrypt(char *formattedArray, char *cypherBet,int groupCount)
{
	for (int i=0; i < formattedArray[i] && formattedArray[i] !='/0'; i++)
	{
		formattedArray[i] = formattedArray[i]+(cypherBet[i]-formattedArray[i]);
		cout << formattedArray[i];
	}
	
}

int main (char *cypherBet)
{

	const int maxSize = 256;
	//char input [maxSize];
	char *input =NULL;
	int d = 0;
	int shiftVal, arraySize,groupCount;
	//char formattedArray[maxSize];
	char *formattedArray = NULL;
	input = new char[maxSize];
	formattedArray = new char[maxSize];
	
	//Takes the input from the user and stores it into the input array
	cout << "Please enter your text to be encrypted \n";
	cin.getline(input, maxSize);

	//loops through input array and stops at a null terminator
	for(int b = 0; b < input[b] && input[b] !='/0'; b++)
	{
		//this checks if the current char is upper case and
		//converts to lowercase if it is
		if((input[b] >= 'A' ) && (input[b] <= 'Z'))
		{
			input[b] = input[b] +32;
		}
		//this checks if the current char is a space and
		//adds it to a new array if it isn't
		if(input[b] != 32 && input[b] != '/0')
		{
			formattedArray[d] = input[b];
			d++;
		}
	}

	cout << "Here is your input as plain text: \n";
	for(int e= 0;e < formattedArray[e] && formattedArray[e] != '/0'; e++)
	{
		cout<<formattedArray[e];
	}

	cout << endl;


	cout << "Here is the length of your array: \n";
	arraySize = getLength(formattedArray);
	cout << endl;

	cout << "Please enter your shift value \n";
	cin >> shiftVal;

	makeCypher(shiftVal);
	
	cout << "Please enter your group count \n";
	cin >> groupCount;
	encrypt(formattedArray, cypherBet, groupCount);
	//formattedArray = encrypt(formattedArray, cypherBet, groupCount);

	cin.ignore(2);

}
Last edited on
Line 56: You can't pass cypherbet as an argument to main. Arguments to main are always int main (int argc, const char* argv[]); where argc is the number of command line arguments and argv list an array of pointers to those command line arguments.

Line 19/44: You don't return the pointer to cypherbet to the caller. This also creates a memory leak because you don't delete cypherbet anywhere.
1
2
3
const char * makeCypher(int shiftVal)
...
return cypherbet;


Line 58:
const char * cypherbet;

Line 107:
cypherbet = makeCypher(shiftVal);








yeh the passing to main was because i was trying to get cypherbet to be able to be viewed by main so i could call the encrypt function from it, as it comes up as cypherbet is undefined otherwise.

yeh i havn't done the return functions yet as i don't fully understand how to return an entire array yet but the function needed to return something.

Topic archived. No new replies allowed.