473,761 Members | 9,480 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1450
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
15941
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
10276
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 problem with attaching a function to an event using the above. However, what's going on with the above is that the reference to the element object (this) isn't being passed to my function. When I do an alert on the argument within the function,...
5
1399
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 char *ptr; 6 ptr="Beginning Linux Programming.pdf"; 7 8 printf("ptr=%s\n", ptr);
8
2237
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
2030
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 determine how many positive and negative values have been read. The program will abort when the user input a zero. I've already made a program but I still can't get it right. I deeply appreciate if you will help me. Here's the program I made: ...
6
1763
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
2969
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 &operator=(const B& x){ if (&x != this)
10
2741
by: Angel Tsankov | last post by:
Hello! Is the following code illformed or does it yield undefined behaviour: class a {}; class b {
0
9345
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10115
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9905
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9775
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7332
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5229
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.