C++ pointers πρόβλημα με new

Συζητήσεις για γλώσσες προγραμματισμού και θέματα σχετικά με προγραμματισμό.
Post Reply
User avatar
Zifnab
Venus Former Team Member
Posts: 7581
Joined: Tue Nov 15, 2005 2:42 am
Academic status: MSc
Gender:
Location: Connecticut
Contact:

C++ pointers πρόβλημα με new

Post by Zifnab » Tue Nov 21, 2006 9:57 pm

Έχω το εξής πρόβλημα. Όταν περνάω τον δείκτη ενός string σε μία μέθοδο (έχοντας κάνει new) μου αλλάζει την διεύθυνση, κάτι που δεν εξηγείται... :roll: Τι δεν έχω καταλάβει? Το output από τον πρώτο κώδικα το καταλαβαίνω, αλλά το δεύτερο θέλω παρακαλώ κάποιος να μου το εξηγήσει-γιατί κάνοντας απλή εκτύπωση της διεύθυνσης που δείχνει o pointer, έπρεπε να βγάζει την ίδια δεύθυνση με αυτό που έδειχνε ο δείκτης που πέρασε σαν attribute... :roll:

Code: Select all

#include <iostream>
#include <vector>
using namespace std;
void address(string* s) {
     cout<<"entered "<<s<<endl;
}
     

int main() {
    string a="text";
    cout<<"before entering method "<<&a<<endl;
    address(&a);
    cout<<"after entering method "<<&a<<endl;
    system("pause");
}

Output:

before entering method 0x22ff50
entered 0x22ff50
after entering method 0x22ff50
Press any key to continue . . .

Code: Select all

#include <iostream>
#include <vector>
using namespace std;
void address(string* s) {
     cout<<"entered "<<s<<endl;
}
     

int main() {
    string* a=new string("text");
    cout<<"before entering method "<<&a<<endl;
    address(a);
    cout<<"after entering method "<<&a<<endl;
    system("pause");
}

Output:

before entering method 0x22ff5c
entered 0x3d3b88
after entering method 0x22ff5c
Press any key to continue . . .


User avatar
sofia_bonny
Kilobyte level
Kilobyte level
Posts: 433
Joined: Thu Sep 29, 2005 12:00 am
Academic status: 4th year
Gender:

Re: C++ pointers πρόβλημα με new

Post by sofia_bonny » Tue Nov 21, 2006 11:07 pm

Zifnab wrote:

Code: Select all

#include <iostream>
#include <vector>
using namespace std;
void address(string* s) {
     cout<<"entered "<<s<<endl;
}
     

int main() {
    string* a=new string("text");
    cout<<"before entering method "<<&a<<endl;
    address(a);
    cout<<"after entering method "<<&a<<endl;
    system("pause");
}

Output:

before entering method 0x22ff5c
entered 0x3d3b88
after entering method 0x22ff5c
Press any key to continue . . .


Νομίζω ότι μέσα στην μέθοδο ζητάς να σου εκτυπώσει την τιμή την οποία περιέχει ο δείκτης s που ουσιαστικά είναι η θέση μνήμης του string ενώ στη main ζητάς να σου εκτυπώσει την διεύθυνση του δείκτη... Οπότε είναι λογικό να σου βγάζει άλλες διευθύνσεις... :roll:
Image
What raging fire shall flood the soul?
What rich desire unlocks its door?
What sweet seduction lies before us . . .?
User avatar
Zifnab
Venus Former Team Member
Posts: 7581
Joined: Tue Nov 15, 2005 2:42 am
Academic status: MSc
Gender:
Location: Connecticut
Contact:

Post by Zifnab » Tue Nov 21, 2006 11:31 pm

sofia, ευχαριστώ για την απάντηση...έχεις δίκιο... ;)
Post Reply

Return to “Προγραμματισμός”