| | |
Array holds more data than specified
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 9
Reputation:
Solved Threads: 0
I did some searching and I think it might be a buffer overflow but I'm not sure. Could someone tell me what's wrong with this?
======================class header===============
For some reason when I output upperCase it contains the values stored in it as well as the values stored in lowerCase. The same thing for shiftedUpper. What's going on here?
P.S My professor says it has something to do with the memory locations. For some reason in the class putting shift in between two char arrays remedies the problem.
Additional Details
When I added the null character the lowerCase char array won't display anything.
C++ Syntax (Toggle Plain Text)
SafeGuard:: SafeGuard(int agent) { for(int index = 0; index < 26; index++) { upperCase[index] = 'A' + index; lowerCase[index] = 'a' + index; } for(int alter = 0; alter < 26; alter++) { shiftedUpper[alter] = upperCase[(1 + agent) * 9 )% 26]; shiftedLower[alter] = lowerCase[(1 + agent) * 9 )% 26]; } cout<<"Upper Case: "<<upperCase<<endl; cout<<"Lower Case: "<<lowerCase<<endl; cout<<"Shifted upper case: "<<shiftedUpper<<endl; cout<<"Shifted lower Case: "<<shiftedLower<<endl; }
C++ Syntax (Toggle Plain Text)
#ifndef CLASS_SafeGuard_ #define CLASS_SafeGuard_ #include <iostream> #include <string> using namespace std; class SafeGuard { private: char upperCase[26]; char lowerCase[26]; int shift; char shiftedUpper[26]; char shiftedLower[26]; char message[100]; public: SafeGuard::SafeGuard(int shift); char *encrptMessage(char message[]); char *decryptMessage(char message[]); char *getMessage(); int getShift(); }; #endif
For some reason when I output upperCase it contains the values stored in it as well as the values stored in lowerCase. The same thing for shiftedUpper. What's going on here?
P.S My professor says it has something to do with the memory locations. For some reason in the class putting shift in between two char arrays remedies the problem.
Additional Details
When I added the null character the lowerCase char array won't display anything.
Last edited by Fenrir190; Oct 19th, 2009 at 10:31 pm.
•
•
Join Date: Feb 2009
Posts: 71
Reputation:
Solved Threads: 9
0
#2 Oct 19th, 2009
•
•
•
•
For some reason when I output upperCase it contains the values stored in it as well as the values stored in lowerCase...
Also, have another look at this:
C++ Syntax (Toggle Plain Text)
for(int alter = 0; alter < 26; alter++) { shiftedUpper[alter] = upperCase[(1 + agent) * 9 )% 26]; shiftedLower[alter] = lowerCase[(1 + agent) * 9 )% 26]; }
•
•
Join Date: Oct 2009
Posts: 9
Reputation:
Solved Threads: 0
0
#3 Oct 19th, 2009
•
•
•
•
I think you will have to show how you are outputting the arrays for anyone to tell what is going wrong with that.
Also, have another look at this:If, for example, agent=0, shiftedUpper will be an array of 26 A's. Is that really what you want? (Similarly for shiftedLower.)C++ Syntax (Toggle Plain Text)
for(int alter = 0; alter < 26; alter++) { shiftedUpper[alter] = upperCase[(1 + agent) * 9 )% 26]; shiftedLower[alter] = lowerCase[(1 + agent) * 9 )% 26]; }
char[]
int
char[]
For some reason that fixed it. As for how I'm outputting it, I'm not quite sure what you by that but, In the default constructor I'm outputting the base addresses of the char arrays to that I can determine if the variables were initialized correctly.
Last edited by Fenrir190; Oct 19th, 2009 at 11:29 pm.
•
•
Join Date: Feb 2009
Posts: 71
Reputation:
Solved Threads: 9
0
#4 Oct 20th, 2009
Sorry, I wasn't paying attention to the rest of the constructor. The problem there is that if you give a char array to cout, it will print everything starting at the beginning of the array and continuing until it finds a null char '\0' (or simply a zero byte). So if you want to do the last byte of upperCase should be a '\0'. Otherwise you have to print it by looping through the array one element at a time.
Your char[], int, char[] solution probably worked because the value of the int happened (luckily) to be 0.
My other comment was about how you calculate the values for shiftedUpper and shiftedLower, not about the output.
C++ Syntax (Toggle Plain Text)
cout << upperCase;
Your char[], int, char[] solution probably worked because the value of the int happened (luckily) to be 0.
My other comment was about how you calculate the values for shiftedUpper and shiftedLower, not about the output.
Last edited by r.stiltskin; Oct 20th, 2009 at 12:19 am.
•
•
Join Date: Oct 2009
Posts: 9
Reputation:
Solved Threads: 0
0
#5 Oct 20th, 2009
•
•
•
•
Sorry, I wasn't paying attention to the rest of the constructor. The problem there is that if you give a char array to cout, it will print everything starting at the beginning of the array and continuing until it finds a null char '\0' (or simply a zero byte). So if you want to dothe last byte of upperCase should be a '\0'. Otherwise you have to print it by looping through the array one element at a time.C++ Syntax (Toggle Plain Text)
cout << upperCase;
Your char[], int, char[] solution probably worked because the value of the int happened (luckily) to be 0.
My other comment was about how you calculate the values for shiftedUpper and shiftedLower, not about the output.
0
#7 Oct 20th, 2009
Since you are using
If you want to use them as a string, you need to remember that as string always ends with \0, so they need to 27 characters long.
upperCase and lowerCase as character arrays rather than strings, you need to output each value individually.If you want to use them as a string, you need to remember that as string always ends with \0, so they need to 27 characters long.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Oct 2009
Posts: 9
Reputation:
Solved Threads: 0
0
#8 33 Days Ago
Ok so me and my professor went into this in a little more detail. From what he saying in each function allocated memory for each identifier is kept in the same space (if that's the correct way to say it.)
ex I have an int, and 2 char[]
int x = 4
char a1[26] = 'A' - 'Z' (assigned using a for loop)
char a2[26] = the shifted characters.
Ex.)
|00|00|00|04|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z
|D|E|F|.......|D|
The \0 character that supposed to be included is overwritten and the array doesn't know when the end comes. Also when I manually insert a null character I lose the shifted char array. SO with this being said I have some questions.
1.) When memory is allocated for identifiers, do the identifiers declared in the same block share the same space?
2.) How do I get the compiler to allocate identifiers in separate memory spaces?
ex I have an int, and 2 char[]
int x = 4
char a1[26] = 'A' - 'Z' (assigned using a for loop)
char a2[26] = the shifted characters.
Ex.)
|00|00|00|04|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z
|D|E|F|.......|D|
The \0 character that supposed to be included is overwritten and the array doesn't know when the end comes. Also when I manually insert a null character I lose the shifted char array. SO with this being said I have some questions.
1.) When memory is allocated for identifiers, do the identifiers declared in the same block share the same space?
2.) How do I get the compiler to allocate identifiers in separate memory spaces?
•
•
Join Date: Oct 2009
Posts: 9
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
for(int i = 0; i != 27; i++) { if(i < 26) letters[i] = 'A' + i; if(i == 26) letters[i] = '\0'; } for(int s = 0; s < 27; s++) { if(s < 26) shifted[s] = letters[(s + shift) % 26]; if(s == 26) shifted[s] = '\0'; }
Using this method I'm able to stay safely in my arrays. Using the previous method the null character wasn't given a chance to be inserted into the array. I'll post my findings so that anyone else having this problem will know what to do and how to prevent this.
•
•
Join Date: Feb 2009
Posts: 71
Reputation:
Solved Threads: 9
0
#10 33 Days Ago
•
•
•
•
The \0 character that supposed to be included is overwritten and the array doesn't know when the end comes. Also when I manually insert a null character I lose the shifted char array. SO with this being said I have some questions.
cout << letters << endl; , you should do this: C++ Syntax (Toggle Plain Text)
char letters[27]; int i; for(i = 0; i < 26; i++) { letters[i] = 'A' + i; } letters[i] = '\0'; // here, i==26 cout << letters << endl;
•
•
•
•
1.) When memory is allocated for identifiers, do the identifiers declared in the same block share the same space?
2.) How do I get the compiler to allocate identifiers in separate memory spaces?
As to the "solution" you posted in Post #9, it is incorrect. You declared each of the arrays to be 26 chars, so, for example, the upperCase array runs from upperCase[0]-upperCase[25]. If you write a '\0' to upperCase[26] you are writing in memory that doesn't belong to that array. You are writing it in memory space that belongs to the NEXT variable that you declared. So if the next variable is the lowerCase array, then the space that you are calling upperCase[26] is REALLY lowerCase[0]; when you assign letters to lowerCase, the 'a' will overwrite the '\0' and you have the same problem as before. Or if you switch things around and declare
int shift; between the two char arrays, then either the value assigned to shift will overwrite the '\0' or the '\0' will overwrite shift, depending on which you do last. Instead, the solution is simply to allocate 27 bytes to each array instead of 26, using the last byte of each array to hold the '\0', as in my example above. ![]() |
Similar Threads
- C# Test Score Data (C#)
- Using different values in loop (C++)
- How do I code a multidimensional array? (VB.NET)
- 2D Dynamic Array (C++)
- c++ array of struct type problems (C++)
- help...one-dimensional array averaging program (C++)
- Having problems saving objects into an array, then accessing the data later for repo (Java)
- Please help with data file and array (C++)
- transfer double array from asp to javascript (ASP.NET)
- How do I create a program using an Array ? (C++)
Other Threads in the C++ Forum
- Previous Thread: Bitsets and Vectors
- Next Thread: please help me to make this question c/c++ program
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






