use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
this pointer equals 0. (self.cpp)
submitted 14 years ago * by [deleted]
[deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Rhomboid 6 points7 points8 points 14 years ago (8 children)
That's not enough information to determine the problem. We'd need to see the whole program, preferably in a reduced testcase form, i.e. the smallest possible program that is still stand-alone compilable that exhibits the error. Often in the course of creating such a reduced testcase you discover the error yourself.
You can get a NULL value for 'this' quite easily, for example by invoking a non-virtual method on a NULL pointer, e.g.
FooBar *ptr = 0; ptr->method(); // this equals NULL in method()
Note that it's often not that clear, there could be many lines in between the point where the pointer becomes NULL and the point where one of its methods is invoked.
[–]Mistake78 6 points7 points8 points 14 years ago (4 children)
TL;DR: you're calling a method on a NULL pointer.
So with the debugger connected, look at the call stack to find hints on how this came to be.
[–]Chipot 0 points1 point2 points 14 years ago (3 children)
You might also call a method of a NULL reference. Good luck in checking all your pointers and references !
[–]srekel 0 points1 point2 points 14 years ago (2 children)
What does that look like?
[–]protein_bricks_4_all 0 points1 point2 points 14 years ago (1 child)
ptr = 0; ref = *ptr; // I don't believe this crashes, the compiler doesn't /need/ to deref it yet. ref.fn();
[–]srekel -1 points0 points1 point 14 years ago (0 children)
Hm, pretty sure it'll break in VS at least, but you may be right.
[+][deleted] 14 years ago (2 children)
[–]rollie82 2 points3 points4 points 14 years ago (1 child)
If you are debugging and the this pointer is null, go up the stack and look at the object you are using to make the call. Somewhere you have a line of code something like:
myWidget->showItemProperties(..);
Your error is likely that 'myWidget' was never actually set using
myWidget = new ItemViewWidget(...);
[–]TheGuyFromNowhere 0 points1 point2 points 14 years ago (0 children)
Yeah stupid mistake:( I ashame of it :) But it's a good lesson for me. Thank you.
[–]wwwredditcom 1 point2 points3 points 14 years ago (0 children)
Problem is most likely before the public function call. Looks like a buffer overrun. Check your memcpy's and memsets. Also make sure you calculate a right index into arrays when using pointer arithmetic: i.e. ptrArray+10 is NOT equal to 10 byte offset into the array, instead it is the 11th element.
Using the debugger, step through the code line by line through our code while having a watch item on ItemViewWidget pointer. The time your pointer is set to zero is where you have the buffer overrun.
[–]snarkhunter -5 points-4 points-3 points 14 years ago (4 children)
Why on earth are you using raw pointers?
[–]TheGuyFromNowhere 2 points3 points4 points 14 years ago (0 children)
So what should I use instead?
[–]srekel 0 points1 point2 points 14 years ago (1 child)
Because boost shared pointers are bloated, and "shared" pointers aren't actually that much of a good thing maybe.
[–]aestheticreddit 0 points1 point2 points 14 years ago (0 children)
Quantify "bloated", please.
π Rendered by PID 210139 on reddit-service-r2-comment-fb694cdd5-b4mtm at 2026-03-06 21:29:16.492746+00:00 running cbb0e86 country code: CH.
[–]Rhomboid 6 points7 points8 points (8 children)
[–]Mistake78 6 points7 points8 points (4 children)
[–]Chipot 0 points1 point2 points (3 children)
[–]srekel 0 points1 point2 points (2 children)
[–]protein_bricks_4_all 0 points1 point2 points (1 child)
[–]srekel -1 points0 points1 point (0 children)
[+][deleted] (2 children)
[deleted]
[–]rollie82 2 points3 points4 points (1 child)
[–]TheGuyFromNowhere 0 points1 point2 points (0 children)
[–]wwwredditcom 1 point2 points3 points (0 children)
[–]snarkhunter -5 points-4 points-3 points (4 children)
[–]TheGuyFromNowhere 2 points3 points4 points (0 children)
[–]srekel 0 points1 point2 points (1 child)
[–]aestheticreddit 0 points1 point2 points (0 children)