PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > really stupid problem with "this" pointer
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
really stupid problem with "this" pointer

Réponse
 
LinkBack Outils de la discussion
Vieux 16/01/2008, 16h01   #1
Sergei Shelukhin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut really stupid problem with "this" pointer

Hi. I have barely written any C++ for past 3-4 years, I need to
refresh it in my memory so I decided to do some coding starting with
classic algos and data structures, using Visual Studio 9, unmanaged
code. Everything is humming, but I have the following problem I cannot
figure out. Say we have the following:

template <typename T> class SLL
{
public:
SLL<T>* AddHead(T value);
T value;
SLL<T>* next;
};

....

SLL<int>* sll = new SLL<int>;
sll->next = NULL; //yeah I know I also need a constructor
sll->value = 1;
sll = sll ->AddHead(0);
sll = sll ->AddHead(0);

If I use this implementation of AddHead:
SLL<T>* head = new SLL<T>;
head->value = value;
head->next = this;
return head;
everything is ok;

If I use this,
SLL<T> head;
head.value = value;
head.next = this;
return &head;
the first call is ok; before the second call "sll" points to memory
location X and contains the list of two elements; but inside the
second call "this" points to X that has uninitialized instance in it
with random value and next pointing into nowhere.
How come?
  Réponse avec citation
Vieux 16/01/2008, 16h24   #2
Stuart Redmann
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: really stupid problem with "this" pointer

Sergei Shelukhin wrote:

> Hi. I have barely written any C++ for past 3-4 years, I need to
> refresh it in my memory so I decided to do some coding starting with
> classic algos and data structures, using Visual Studio 9, unmanaged
> code. Everything is humming, but I have the following problem I cannot
> figure out. Say we have the following:
>
> template <typename T> class SLL
> {
> public:
> SLL<T>* AddHead(T value);
> T value;
> SLL<T>* next;
> };
>
> ...
>
> SLL<int>* sll = new SLL<int>;
> sll->next = NULL; //yeah I know I also need a constructor
> sll->value = 1;
> sll = sll ->AddHead(0);
> sll = sll ->AddHead(0);
>
> If I use this implementation of AddHead:
> SLL<T>* head = new SLL<T>;
> head->value = value;
> head->next = this;
> return head;
> everything is ok;
>
> If I use this,
> SLL<T> head;
> head.value = value;
> head.next = this;
> return &head;
> the first call is ok; before the second call "sll" points to memory
> location X and contains the list of two elements; but inside the
> second call "this" points to X that has uninitialized instance in it
> with random value and next pointing into nowhere.
> How come?


You should read the chapter parameter passing in C++ once more. You are
returning a pointer to a local variable (the variable head goes out of scope
when AddHead is left), so you cannot dereference this pointer anymore outside
your function or you'll get Undefined Behaviour. Surely your compiler will give
you a warning (at least VC6.0 does).

Regards,
Stuart
  Réponse avec citation
Vieux 16/01/2008, 22h01   #3
Sergei Shelukhin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: really stupid problem with "this" pointer

Hmm, thanks.
I rather got too used to GC patterns, if I still point at it, it
should exist
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 13h12.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10868 seconds with 11 queries