944,120 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 633
  • C++ RSS
Oct 19th, 2009
0

Array holds more data than specified

Expand Post »
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?

C++ Syntax (Toggle Plain Text)
  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===============
C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Fenrir190 is offline Offline
11 posts
since Oct 2009
Oct 19th, 2009
0
Re: Array holds more data than specified
Click to Expand / Collapse  Quote originally posted by Fenrir190 ...
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:
C++ Syntax (Toggle Plain Text)
  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.)
Reputation Points: 23
Solved Threads: 18
Junior Poster
r.stiltskin is offline Offline
105 posts
since Feb 2009
Oct 19th, 2009
0
Re: Array holds more data than specified
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:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Fenrir190 is offline Offline
11 posts
since Oct 2009
Oct 20th, 2009
0
Re: Array holds more data than specified
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
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 23
Solved Threads: 18
Junior Poster
r.stiltskin is offline Offline
105 posts
since Feb 2009
Oct 20th, 2009
0
Re: Array holds more data than specified
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
C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Fenrir190 is offline Offline
11 posts
since Oct 2009
Oct 20th, 2009
0
Re: Array holds more data than specified
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.
Reputation Points: 23
Solved Threads: 18
Junior Poster
r.stiltskin is offline Offline
105 posts
since Feb 2009
Oct 20th, 2009
0
Re: Array holds more data than specified
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.
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is online now Online
7,747 posts
since May 2006
Oct 24th, 2009
0
Re: Array holds more data than specified
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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Fenrir190 is offline Offline
11 posts
since Oct 2009
Oct 24th, 2009
0

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

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Fenrir190 is offline Offline
11 posts
since Oct 2009
Oct 25th, 2009
0
Re: Array holds more data than specified
Click to Expand / Collapse  Quote originally posted by Fenrir190 ...
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:
C++ Syntax (Toggle Plain Text)
  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;


Click to Expand / Collapse  Quote originally posted by Fenrir190 ...
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.
Reputation Points: 23
Solved Threads: 18
Junior Poster
r.stiltskin is offline Offline
105 posts
since Feb 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Bitsets and Vectors
Next Thread in C++ Forum Timeline: please help me to make this question c/c++ program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC