C++ pointers to memory - why arent they changing

Learning C++. I am on pointers to memory.
Have a text file that is read by my program. It decomposes each word to individual characters and places it into memory along with position in a word. The program is supposed to return memory address of each <character> <position> combination. It returns the same memory address for each combination. WHY? I thought it would return a unique memory address for each combination.
Here is the text file:
1
2
3
4
5
BRAIN
CORTEX
AMYGDALIA
HYPOTALAMUS
CEREBELLUM


Here is the program:

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
#include <iostream>
#include <string>
#include <algorithm>
#include <string>
#include <fstream>

using namespace std;
     
int main () 
{

 string word; 

 ifstream myReadFile;
 myReadFile.open("text.txt");
 char output[1000000];
 if (myReadFile.is_open()) {
 while (!myReadFile.eof()) {
 
    myReadFile >> word;
 
    cout<<output;

for( int k=0; k<word.length(); k++ ) 
 cout<< k << "  " << word[k] 
 << "   MemAddrK " << &k
 << "   MemAddrL " << &word 
 << "   MemAddrK+Letter " << &word[k] << endl;

 cout << "...................." << endl;        
          }
 }
 
myReadFile.close();

    system("pause");
    return 0;
}


Here is the output:

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
0  B   MemAddrK 0x19bbdc   MemAddrL 0x28ff20   MemAddrK+Letter BRAIN
1  R   MemAddrK 0x19bbdc   MemAddrL 0x28ff20   MemAddrK+Letter RAIN
2  A   MemAddrK 0x19bbdc   MemAddrL 0x28ff20   MemAddrK+Letter AIN
3  I   MemAddrK 0x19bbdc   MemAddrL 0x28ff20   MemAddrK+Letter IN
4  N   MemAddrK 0x19bbdc   MemAddrL 0x28ff20   MemAddrK+Letter N
....................
0  C   MemAddrK 0x19bbdc   MemAddrL 0x28ff20   MemAddrK+Letter CORTEX
1  O   MemAddrK 0x19bbdc   MemAddrL 0x28ff20   MemAddrK+Letter ORTEX
2  R   MemAddrK 0x19bbdc   MemAddrL 0x28ff20   MemAddrK+Letter RTEX
3  T   MemAddrK 0x19bbdc   MemAddrL 0x28ff20   MemAddrK+Letter TEX
4  E   MemAddrK 0x19bbdc   MemAddrL 0x28ff20   MemAddrK+Letter EX
5  X   MemAddrK 0x19bbdc   MemAddrL 0x28ff20   MemAddrK+Letter X
....................
0  A   MemAddrK 0x19bbdc   MemAddrL 0x28ff20   MemAddrK+Letter AMYGDALIA
1  M   MemAddrK 0x19bbdc   MemAddrL 0x28ff20   MemAddrK+Letter MYGDALIA
2  Y   MemAddrK 0x19bbdc   MemAddrL 0x28ff20   MemAddrK+Letter YGDALIA
3  G   MemAddrK 0x19bbdc   MemAddrL 0x28ff20   MemAddrK+Letter GDALIA
4  D   MemAddrK 0x19bbdc   MemAddrL 0x28ff20   MemAddrK+Letter DALIA
5  A   MemAddrK 0x19bbdc   MemAddrL 0x28ff20   MemAddrK+Letter ALIA
6  L   MemAddrK 0x19bbdc   MemAddrL 0x28ff20   MemAddrK+Letter LIA
7  I   MemAddrK 0x19bbdc   MemAddrL 0x28ff20   MemAddrK+Letter IA
8  A   MemAddrK 0x19bbdc   MemAddrL 0x28ff20   MemAddrK+Letter A
....................
0  H   MemAddrK 0x19bbdc   MemAddrL 0x28ff20   MemAddrK+Letter HYPOTALAMUS
1  Y   MemAddrK 0x19bbdc   MemAddrL 0x28ff20   MemAddrK+Letter YPOTALAMUS
2  P   MemAddrK 0x19bbdc   MemAddrL 0x28ff20   MemAddrK+Letter POTALAMUS
3  O   MemAddrK 0x19bbdc   MemAddrL 0x28ff20   MemAddrK+Letter OTALAMUS
4  T   MemAddrK 0x19bbdc   MemAddrL 0x28ff20   MemAddrK+Letter TALAMUS
5  A   MemAddrK 0x19bbdc   MemAddrL 0x28ff20   MemAddrK+Letter ALAMUS
6  L   MemAddrK 0x19bbdc   MemAddrL 0x28ff20   MemAddrK+Letter LAMUS
7  A   MemAddrK 0x19bbdc   MemAddrL 0x28ff20   MemAddrK+Letter AMUS
8  M   MemAddrK 0x19bbdc   MemAddrL 0x28ff20   MemAddrK+Letter MUS
9  U   MemAddrK 0x19bbdc   MemAddrL 0x28ff20   MemAddrK+Letter US
10  S   MemAddrK 0x19bbdc   MemAddrL 0x28ff20   MemAddrK+Letter S
....................


What I expected (and I would like to know why this assumption is wrong) is some thing like:

1
2
3
4
5
0  B   MemAddrK 0x19bbdc   MemAddrL 0x28ff20   MemAddrK+Letter BRAIN
1  R   MemAddrK 0x87utdo   MemAddrL 0xkfyrr0   MemAddrK+Letter RAIN
2  A   MemAddrK 0xiuetrt   MemAddrL 0xifu550   MemAddrK+Letter AIN
3  I   MemAddrK 0x98fhhf   MemAddrL 0x2nftr7   MemAddrK+Letter IN
4  N   MemAddrK 0x1jfdhf   MemAddrL 0x2h544h   MemAddrK+Letter N


Why arent the memory addresses changing as different <position> <letter> are being passed into memory? I am assuming this is because after each line memory contents are cycled (cleared), and new values are assigned the same slot.
If, so how do I retain them and assign a new memory slot?
Code examples will be appreciated. Thanks.
I am assuming this is because after each line memory contents are cycled (cleared), and new values are assigned the same slot.

This is true for k because it keeps getting destroyed and recreated every time the loop is entered (at least formally).
But word is always the same object. You just keep changing it, but it's never destroyed (and objects never change addresses after they've been created).
Edit: and of course, there's no way either would change while iterating over the string.
Are you sure you understood the concept of automatic objects (a.k.a stack variables)?

If, so how do I retain them and assign a new memory slot?

What do you really want to do? If you want to store all words, you'll want a vector (a dynamic array).
Last edited on
1
2
3
4
5
6
7
8
9
10
for( int k=0; k<word.length(); k++ ) 
 cout<< k << "  " << word[k] 
// added code here ,,,,
 << "   MemAddrK " << &k // this should be constant, k will never move.
 << "   MemAddrL " << &word // this should be &word + k * sizeof(char) assuming char is the default of the string.
 << "   MemAddrK+Letter " << &word[k] << endl;

 cout << "...................." << endl;        
          }


I hope the comments in code point you in a direction....
@Azagaros
The comment for line 2 belongs to line 3. And even so, it would be &word[0] + k (although the standard doesn't explicitly guarantee that the characters have to be adjacent to each other).
Line 3 should have a cast to void*, otherwise it's treated as a C string, which isn't correct here.
@Athor I agree with you. I wasn't sure how I might get the info he wanted without relying on an string::iterator and using something like &(*pIterator). I didn't know if this was for a class or how advanced of a programmer I was dealing with.

Athar and Azagaros, thank you so very much for you replies.
Now I understand that &k is a placeholder for a value, not a value itself so it would not change.
So, with every new line from text file it will hold a new character with the same memory address.
My next task is to put Gigabytes of text file into memory, which of course will fail since this PC doesnt have that much RAM.
I need to do the following:
1) Read first line from text file. Each line contains 35 characters maximum. Make it persist in memory (not recycled).
2) Read second line from file. If characters sequence at least partially exists in memory, link remaining difference to already existing sequence from previous line. Like first line is ABBAT, next line is ABBITURENT, the ITURENT from second line will be linked to ABB from first line. Now you understand WHY I tried to take each line apart to its individual sequence of characters with their positions.
3) And so on for millions of rows.
I want to see how much of the file can be fit into memory and if level of compression in memory (by linking to already existing partial strings) will be significant.
The file will contain 30%-40% of repeating strings, but repeating only partially. Each line still will be unique.
I know this is quite an undertaking for a newbie like me (I am still learning how to spell C++!) but I still wonder if you can provide some pointers or code examples.
Very much appreciate your help.
Ok, Helios(sp) has posted something of the basic code example of what you are trying to do, which is an lz compression algorithm. This problem has been asked several times on this board in the last 2 weeks. Almost like a mid-term complex project for a computer science class.

The post Helios has posted is in the archived posts and he has mentioned it in similar posts to yours. I don't have the link handy without searching, so happy hunting.
Topic archived. No new replies allowed.