473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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 Tclass 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<Thead;
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?
Jan 16 '08 #1
2 1423
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 Tclass 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<Thead;
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
Jan 16 '08 #2
Hmm, thanks.
I rather got too used to GC patterns, if I still point at it, it
should exist ;)
Jan 16 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Rasti | last post by:
Hello, I am trying to translate a C++ script to Java. Are there any differences between the C++ this-pointer and the Java this-pointer? Thank you
1
by: Christoph | last post by:
newFieldElement = document.createElement( 'INPUT' ); newFieldElement.onblur = new Function( "calculatePremiumOptionTotal( this );" ); In my (dynamically generated) javascript, I've never had a...
5
by: Dan | last post by:
Hi, I am getting a SEG V at line 12 and just can't figure out why? Can you help please? This is a simple program to replace spaces with '_' in string 1 #include <stdio.h> 2 3 int main() 4 { 5 ...
8
by: Martin Jørgensen | last post by:
Hi, "C primer plus" p.382: Suppose we have this declaration: int (*pa); int ar1; int ar2; int **p2;
1
aprilmae36
by: aprilmae36 | last post by:
Nice day to all... I'm new in java programming and I'm having some problem with this program. The problem is this: Write a program that will read an unspecified number of non-zero integers and...
6
by: Geevi | last post by:
Hi all, can u explain about this pointer with example... how to use and where to use? how can we return objects with this pointer?
3
by: john | last post by:
Hey, I know we use the pointer this to obtain a class object or class member data. I don't follow the reason for example this code. I'am quite confused assingment operator const B...
10
by: Angel Tsankov | last post by:
Hello! Is the following code illformed or does it yield undefined behaviour: class a {}; class b {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.