*Char Variables Overwritten?

Hi everyone, I'm having a problem. I'm trying to get three differents *char in order to check if they math between themselves ( actually just to check two of them).
But when I call the function to get the second and the third password (the *char's are passwords, but I write a new code in order to simplify the problem) the new reads overwrite the previous one.
Here is the code:

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
#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <windows.h>

using namespace std;

HANDLE consol;

char *getPass(void)
{
     COORD pos;
     consol = GetStdHandle(STD_OUTPUT_HANDLE);
     cout<<"[";
     CONSOLE_SCREEN_BUFFER_INFO consolInfo;
     GetConsoleScreenBufferInfo(consol, &consolInfo);
     pos.X = consolInfo.dwCursorPosition.X;
     pos.Y = consolInfo.dwCursorPosition.Y;
     cout<<"__________]";
     SetConsoleCursorPosition(consol, pos);
     char letra,pass[10];
     int i=0;
     do
     {
          letra = getch();
          if (letra!=8 && letra!=13 && i<10)
          {
             pass[i]=letra;
             cout<<"*";
             i++;
          }
          else if (letra==8 && i>0)
          {
               cout<<"\b"<<"_"<<"\b";
               i--;
          }
     }
     while (letra!=13);
     pass[i]='\0';
     static char fpass[10];
     strcpy(fpass,pass);
     return fpass;
}

int main (void)
{
     char *word1,*word2;
     word1 = (char*) malloc(300);
     word2 = (char*) malloc(300);
     cout<<"Write First Word: ";
     word1=getPass();
     cout<<"\nWrite Second Word: ";
     word2=getPass();
     cout<<"\nThe words are: "<<word1<<" and "<<word2;     
     getch();
}


For example, if the input is: MyPass1, and then I write MyPass2, the variable word1 will be overwritten by the new input leaving the variables with the same string.
So I wanted to know why these happens. I'm really new at c++ so this may be a really foolish matter that I just can't realized the reason.

By the way, I apologize for my bad english, since it's not my first language.

Thanks for reading my nosense code :)

Last edited on
It's because on line 40 you return a pointer to fpass, so word1 and word2 point to the same memory location.

To fix this, you can use C++ strings instead of char*

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
#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <windows.h>

using namespace std;


string getPass(void) // use C++ strings instead of char arrays
{
     HANDLE consol; // Avoid global variables when possible
     COORD pos;
     consol = GetStdHandle(STD_OUTPUT_HANDLE);
     cout<<"[";
     CONSOLE_SCREEN_BUFFER_INFO consolInfo;
     GetConsoleScreenBufferInfo(consol, &consolInfo);
     pos.X = consolInfo.dwCursorPosition.X;
     pos.Y = consolInfo.dwCursorPosition.Y;
     cout<<"__________]";
     SetConsoleCursorPosition(consol, pos);
     char letra,pass[10];
     int i=0;
     do
     {
          letra = getch();
          if (letra!=8 && letra!=13 && i<10)
          {
             pass[i]=letra;
             cout<<"*";
             i++;
          }
          else if (letra==8 && i>0)
          {
               cout<<"\b"<<"_"<<"\b";
               i--;
          }
     }
     while (letra!=13);
     pass[i]='\0';
     return pass; // just return pass
}

int main (void)
{
     string word1,word2; // use string here too
     cout<<"Write First Word: ";
     word1=getPass();
     cout<<"\nWrite Second Word: ";
     word2=getPass();
     cout<<"\nThe words are: "<<word1<<" and "<<word2;
     getch();
}

}

Thank You!! I didn't know that the pointer fpass remains with the same memory location.
Knowing what the problem was...couldn't be possible to release the pointer after returning it?
In order to avoid overwritting the same memory location. For example, like using:

1
2
3
4
5
6
7
...
     char *fpass;
     fpass = new char [10];
     strcpy(fpass,pass);
     return fpass;
     delete[] fpass;
...


Would I be doing something wrong or not advisable???
Last edited on
Topic archived. No new replies allowed.