Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Resolving memory leaks

[es] :: C/C++ programiranje :: Resolving memory leaks

[ Pregleda: 4819 | Odgovora: 13 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

Pretender

Član broj: 12407
Poruke: 100
212.124.182.*



Profil

icon Resolving memory leaks16.09.2003. u 23:26 - pre 249 meseci
Code:

1:     // Listing 9.14
2:      // Resolving memory leaks
3:      #include <iostream.h>
4:
5:      class SimpleCat
6:      {
7:      public:
8:              SimpleCat (int age, int weight);
9:             ~SimpleCat() {}
10:            int GetAge() { return itsAge; }
11:            int GetWeight() { return itsWeight; }
12:
13      private:
14:           int itsAge;
15:           int itsWeight;
16:      };
17:
18:      SimpleCat::SimpleCat(int age, int weight):
19:      itsAge(age), itsWeight(weight) {}
20:
21:      SimpleCat & TheFunction();
22:
23:      int main()
24:      {
25:           SimpleCat & rCat = TheFunction();
26:           int age = rCat.GetAge();
27:           cout << "rCat is " << age << " years old!\n";
28:           cout << "&rCat: " << &rCat << endl;
29:           // How do you get rid of that memory?
30:           SimpleCat * pCat = &rCat;
31:           delete pCat;
32:           // Uh oh, rCat now refers to ??
33:     return 0;
34:      }
35:
36:      SimpleCat &TheFunction()
37:      {
38:           SimpleCat * pFrisky = new SimpleCat(5,9);
39:           cout << "pFrisky: " << pFrisky << endl;
40:           return *pFrisky;
41: }

Output: pFrisky:  0x2bf4
rCat is 5 years old!
&rCat: 0x2bf4


Citat:
So far, so good. But how will that memory be freed? You can't call delete on the reference. One clever solution is to create another pointer and initialize it with the address obtained from rCat. This does delete the memory, and plugs the memory leak. One small problem, though: What is rCat referring to after line 31? As stated earlier, a reference must always alias an actual object; if it references a null object (as this does now), the program is invalid.

Citat:

There are actually three solutions to this problem. The first is to declare a SimpleCat object on line 25, and to return that cat from TheFunction by value. The second is to go ahead and declare the SimpleCat on the free store in TheFunction(), but have TheFunction() return a pointer to that memory. Then the calling function can delete the pointer when it is done.
The third workable solution, and the right one, is to declare the object in the calling function and then to pass it to TheFunction() by reference.


Moze li mi neko od specijalista ispisati to 3. i pravo resenje ?


Hvala
 
Odgovor na temu

boccio
Boris Krstović
Spoonlabs.com
nbgd

Član broj: 7594
Poruke: 2458
*.vdial.verat.net

Sajt: bocc.io


+771 Profil

icon Re: Resolving memory leaks20.09.2003. u 10:37 - pre 249 meseci
ako se ne varam, ovo su primeri iz knjige jesse liberty-ja, zar ne?
Jeff, one day you’ll understand that it’s harder to be kind than clever.
 
Odgovor na temu

Pretender

Član broj: 12407
Poruke: 100
212.124.182.*



Profil

icon Re: Resolving memory leaks20.09.2003. u 16:59 - pre 249 meseci
Da (ako ce to nesto pomoci).
 
Odgovor na temu

Dragi Tata
Malo ispod Kanade

Član broj: 1958
Poruke: 3906
..ndg-pm4-2.dialup.nethere.net



+6 Profil

icon Re: Resolving memory leaks20.09.2003. u 19:11 - pre 249 meseci
Ajde, danas sam dobre volje - doterali su mi mašinu za veš, ali u principu ti savetujem da nađeš neku ljudsku knjigu. Kažu da je za početnike najbolja ova:

http://www.acceleratedcpp.com/

Uglavnom, evo koda:

Code:

#include <iostream>
using namespace std;

class SimpleCat
    {
    public:
        SimpleCat (int age, int weight):
            itsAge(age), itsWeight(weight) {}
        ~SimpleCat() {}
        int GetAge() { return itsAge; }
        int GetWeight() { return itsWeight; }

    private:
        int itsAge;
        int itsWeight;
    };


void TheFunction(SimpleCat&);

int main()
    {
    SimpleCat rCat(0,0);       
    TheFunction(rCat);
    int age = rCat.GetAge();
    cout << "rCat is " << age << " years old!\n";
    cout << "&rCat: " << &rCat << endl;
    }

void TheFunction(SimpleCat& cat)
    {
    SimpleCat Frisky(5,9);
    cout << "Frisky: " << &Frisky << endl;

 //Ovo radi jer su itsAge i itsWeight value tipovi
 //da su pointeri bilo bi belaja
    cat = Frisky; 
    }

 
Odgovor na temu

leka
Dejan Lekić
senior software engineer, 3Developers
Ltd.
London, UK

Član broj: 234
Poruke: 2534
*.telia.com

Sajt: dejan.lekic.org


+2 Profil

icon Re: Resolving memory leaks20.09.2003. u 19:29 - pre 249 meseci
Ja ne volim Get/Set metode. Ne kapiram zasto ljudi vise vole da imaju dva metoda umesto jednog, overloadovanog. GetNesto je isto sto i Nesto() , a SetNesto() je isto sto i (recimo) Nesto(int arg) ...
Dejan Lekic
software engineer, MySQL/PgSQL DBA, sysadmin
 
Odgovor na temu

Dragi Tata
Malo ispod Kanade

Član broj: 1958
Poruke: 3906
..ndg-pm4-2.dialup.nethere.net



+6 Profil

icon Re: Resolving memory leaks20.09.2003. u 20:16 - pre 249 meseci
Hehe, to znači da tebi više pasuje C# (properties) nego Java (get - set). Mislim da je to jedna od onih stvari koje jesu stvar ukusa. Lično mislim da Get/Set metode ponekad previše otkrivaju unutrašnjost klase i tako štete enkapsulaciji.
 
Odgovor na temu

-zombie-
Tomica Jovanovic
freelance programmer
ni.ac.yu

Član broj: 4128
Poruke: 3448
*.verat.net

Sajt: localhost


+5 Profil

icon Re: Resolving memory leaks20.09.2003. u 20:54 - pre 249 meseci
ne, njemu još više leži Delphi (iz koga su c# propertiji ;).

Citat:
Dragi Tata:
Lično mislim da Get/Set metode ponekad previše otkrivaju unutrašnjost klase i tako štete enkapsulaciji.


da li bi hteo da ovo malo elaboriraš? ovo verovatno isto važi i za propertije (koji su isto to, samo druga sintaxa).

ne razumem kako to otkriva unutrašnjost? pa get/set funkcija uopšte ne mora da čita/piše neku privatnu promenjivu objekta, već može da poziva druge metode...
 
Odgovor na temu

Dragi Tata
Malo ispod Kanade

Član broj: 1958
Poruke: 3906
..ndg-pm4-2.dialup.nethere.net



+6 Profil

icon Re: Resolving memory leaks20.09.2003. u 22:39 - pre 249 meseci
Citat:
-zombie-:
ne razumem kako to otkriva unutrašnjost? pa get/set funkcija uopšte ne mora da čita/piše neku privatnu promenjivu objekta, već može da poziva druge metode...


Pa zato sam ja rekao "ponekad", a ti "može". U gornjem primeru je manje-više sve u redu jer Get funkcije vraćaju kopije unutrašnjih promenljivih. A šta bi bilo da vraćaju pointere/reference na te promenljive? To mu dođe potpuno isto kao da su stavili promenljive u public deo.
 
Odgovor na temu

Pretender

Član broj: 12407
Poruke: 100
212.124.182.*



Profil

icon Re: Resolving memory leaks21.09.2003. u 22:54 - pre 249 meseci

Hvala, Dragi Tata. [Nadam se da ce vesmasina raditi dobro]
 
Odgovor na temu

Pretender

Član broj: 12407
Poruke: 100
212.124.182.*



Profil

icon Re: Resolving memory leaks21.09.2003. u 23:18 - pre 249 meseci
Vidim da je knjiga koju si preporucio dosta hvaljena, ali nisam uspeo da nadjem e-verziju, iako sam u poslednje vreme pronasao i skinuo nekoliko slicnih knjiga (Thinking in C++, The C++ Primer(hvala leki) i `moju` knjigu(hvala srkiju)).
 
Odgovor na temu

dust
Dušan Stefanović

Član broj: 9827
Poruke: 33
*.rcub.bg.ac.yu



Profil

icon Re: Resolving memory leaks24.09.2003. u 11:25 - pre 249 meseci
Jel može link za C++ Primer?
 
Odgovor na temu

Pretender

Član broj: 12407
Poruke: 100
*.beotel.net



Profil

icon Re: Resolving memory leaks24.09.2003. u 16:25 - pre 249 meseci
http://www-math.cudenver.edu/~jwilson/cpp_primer/CPP_PRIMER/
 
Odgovor na temu

filmil
Filip Miletić
Oce Technologies B.V., inženjer
hardvera
Arcen, NL

Član broj: 243
Poruke: 2114
*.adsl.zonnet.nl

Jabber: filmil@jabber.org
ICQ: 36601391


+3 Profil

icon Re: Resolving memory leaks24.09.2003. u 19:16 - pre 249 meseci
Gledam u prevod, gledam u ovaj tvoj link, pa mi se sve čini da to nije to.

f
 
Odgovor na temu

leka
Dejan Lekić
senior software engineer, 3Developers
Ltd.
London, UK

Član broj: 234
Poruke: 2534
*.racasse.se

Sajt: dejan.lekic.org


+2 Profil

icon Re: Resolving memory leaks25.09.2003. u 08:28 - pre 249 meseci
Evo linka koji je "povezan" sa knjigom - http://www.amazon.com/exec/obi...ref=sr_2_1/104-7090890-1727116 . Inace knjiga je odavno prevedena na nas jezik, moze se kupiti na http://www.knjizara.co.yu/pls/sasa/knjizara.knjiga?k_id=14207
Dejan Lekic
software engineer, MySQL/PgSQL DBA, sysadmin
 
Odgovor na temu

[es] :: C/C++ programiranje :: Resolving memory leaks

[ Pregleda: 4819 | Odgovora: 13 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.