Array holds more data than specified

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2009
Posts: 9
Reputation: Fenrir190 is an unknown quantity at this point 
Solved Threads: 0
Fenrir190 Fenrir190 is offline Offline
Newbie Poster

Array holds more data than specified

 
0
  #1
Oct 19th, 2009
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?

  1. SafeGuard:: SafeGuard(int agent)
  2. {
  3. for(int index = 0; index < 26; index++)
  4. {
  5. upperCase[index] = 'A' + index;
  6. lowerCase[index] = 'a' + index;
  7. }
  8.  
  9. for(int alter = 0; alter < 26; alter++)
  10. {
  11. shiftedUpper[alter] = upperCase[(1 + agent) * 9 )% 26];
  12. shiftedLower[alter] = lowerCase[(1 + agent) * 9 )% 26];
  13. }
  14. cout<<"Upper Case: "<<upperCase<<endl;
  15. cout<<"Lower Case: "<<lowerCase<<endl;
  16. cout<<"Shifted upper case: "<<shiftedUpper<<endl;
  17. cout<<"Shifted lower Case: "<<shiftedLower<<endl;
  18. }
======================class header===============
  1. #ifndef CLASS_SafeGuard_
  2. #define CLASS_SafeGuard_
  3. #include <iostream>
  4. #include <string>
  5. using namespace std;
  6.  
  7. class SafeGuard
  8. {
  9. private:
  10. char upperCase[26];
  11. char lowerCase[26];
  12. int shift;
  13. char shiftedUpper[26];
  14. char shiftedLower[26];
  15. char message[100];
  16. public:
  17. SafeGuard::SafeGuard(int shift);
  18. char *encrptMessage(char message[]);
  19. char *decryptMessage(char message[]);
  20. char *getMessage();
  21. int getShift();
  22. };
  23. #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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 71
Reputation: r.stiltskin is an unknown quantity at this point 
Solved Threads: 9
r.stiltskin r.stiltskin is offline Offline
Junior Poster in Training
 
0
  #2
Oct 19th, 2009
Originally Posted by Fenrir190 View Post
For some reason when I output upperCase it contains the values stored in it as well as the values stored in lowerCase...
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:
  1. for(int alter = 0; alter < 26; alter++)
  2. {
  3. shiftedUpper[alter] = upperCase[(1 + agent) * 9 )% 26];
  4. shiftedLower[alter] = lowerCase[(1 + agent) * 9 )% 26];
  5. }
If, for example, agent=0, shiftedUpper will be an array of 26 A's. Is that really what you want? (Similarly for shiftedLower.)
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 9
Reputation: Fenrir190 is an unknown quantity at this point 
Solved Threads: 0
Fenrir190 Fenrir190 is offline Offline
Newbie Poster
 
0
  #3
Oct 19th, 2009
Originally Posted by r.stiltskin View Post
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:
  1. for(int alter = 0; alter < 26; alter++)
  2. {
  3. shiftedUpper[alter] = upperCase[(1 + agent) * 9 )% 26];
  4. shiftedLower[alter] = lowerCase[(1 + agent) * 9 )% 26];
  5. }
If, for example, agent=0, shiftedUpper will be an array of 26 A's. Is that really what you want? (Similarly for shiftedLower.)
Ok I did this before but the way I fixed it was declaring the variables like this
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 71
Reputation: r.stiltskin is an unknown quantity at this point 
Solved Threads: 9
r.stiltskin r.stiltskin is offline Offline
Junior Poster in Training
 
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
  1. cout << upperCase;
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.
Last edited by r.stiltskin; Oct 20th, 2009 at 12:19 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 9
Reputation: Fenrir190 is an unknown quantity at this point 
Solved Threads: 0
Fenrir190 Fenrir190 is offline Offline
Newbie Poster
 
0
  #5
Oct 20th, 2009
Originally Posted by r.stiltskin View Post
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
  1. cout << upperCase;
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.
So there isn't an actual problem? So long as I'm careful this won't effect my use of the char arrays right?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 71
Reputation: r.stiltskin is an unknown quantity at this point 
Solved Threads: 9
r.stiltskin r.stiltskin is offline Offline
Junior Poster in Training
 
0
  #6
Oct 20th, 2009
Your printing problem is not because there's anything wrong with the arrays. It's because of the way you are printing.

So I guess that's a "yes" to your last question.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei
 
0
  #7
Oct 20th, 2009
Since you are using 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
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 9
Reputation: Fenrir190 is an unknown quantity at this point 
Solved Threads: 0
Fenrir190 Fenrir190 is offline Offline
Newbie Poster
 
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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 9
Reputation: Fenrir190 is an unknown quantity at this point 
Solved Threads: 0
Fenrir190 Fenrir190 is offline Offline
Newbie Poster

I've found a solution to this problem (well another one)

 
0
  #9
33 Days Ago
  1. for(int i = 0; i != 27; i++)
  2. {
  3. if(i < 26)
  4. letters[i] = 'A' + i;
  5.  
  6. if(i == 26)
  7. letters[i] = '\0';
  8. }
  9.  
  10. for(int s = 0; s < 27; s++)
  11. {
  12. if(s < 26)
  13. shifted[s] = letters[(s + shift) % 26];
  14.  
  15. if(s == 26)
  16. shifted[s] = '\0';
  17. }

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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 71
Reputation: r.stiltskin is an unknown quantity at this point 
Solved Threads: 9
r.stiltskin r.stiltskin is offline Offline
Junior Poster in Training
 
0
  #10
33 Days Ago
Originally Posted by Fenrir190 View Post
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.
You are confused. The null (\0) character doesn't get put there magically by the compiler. If you want to be able to treat that char array as a string, YOU have to allow space for the null character, and YOU have to put it there. If you want to be able to print it just by writing cout << letters << endl; , you should do this:
  1. char letters[27];
  2. int i;
  3. for(i = 0; i < 26; i++) {
  4. letters[i] = 'A' + i;
  5. }
  6. letters[i] = '\0'; // here, i==26
  7. cout << letters << endl;


Originally Posted by Fenrir190 View Post
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?
You are confused here as well. Your professor probably said something to the effect that the identifiers declared within a particular block of code are only accessible while that block of code is executing. Even your example shows that they don't share the same space -- if they did, the second array would have completely overwritten the first one. Instead, as your example shows, the second array follows immediately after the first one -- as you would logically expect. So you don't have to do anything special -- each identifier gets its own memory space automatically. But you have to be sure that you are allocating ENOUGH space.

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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC