[SOLVED] C++ class member pointer changes after function returns
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
The pointer to 'display' seems to change its value when the function returns, and the value it changes to each time is different which suggests to me it's referencing invalid memory. Certainly, if I try to use it in any functions that draw to it then it throws a segfault. These problems don't occur if I use the exact same code inside the createWindow() function.
Can anyone point me to a resource on pointers which explains what's going on here? (no pun intended...) Thank you
Last edited by Snark1994; 03-30-2011 at 04:20 PM.
Reason: Spotted a pun
Oh boy, that code looks more C-like than C++. Anyhow, the issue is that you re-declared "display" in your createWindow() function. Obviously you meant to use the class member data variable.
Cheers I knew it would be something stupid like that... Should have read it more carefully.
And yeah, it kinda is. I'm new to C++ (and, well, pretty new to C) but I started using it for the classes. Should I try to make changes to my coding style for C++, or is this more of a "whatever works" type thing?
Do you want to excel in C++, or just do the minimum to get by?
99% of software development has to do with style, and unfortunately it's not yours, but that of your peers. If you adhere to the design and style expected by your peers (or company or teacher), then you will be fine.
You are at the beginning of your project (obviously not much there), so it is hard to comment on your design (you have one, right??).
As for C++, one should allocate memory using "new", not "malloc"... and never allocate one measly byte! If you require a string, use the STL string class object. But in the end, you do what you want. After all, it is your project.
Ah, myChar was just to show that the 'char' pointer didn't change when I popped out of the function
In terms of code, I'm just having fun really. So I don't really have an "expected" design or style. However, thanks for the heads-up on "new". And... unless I'm misunderstanding you, I don't really have a design I'm just putting together some functions for other programmes to call, so I can re-use my graphics and window creation code.
As for C++, one should allocate memory using "new", not "malloc"... and never allocate one measly byte! If you require a string, use the STL string class object. But in the end, you do what you want. After all, it is your project.
Well, really you shouldn't use "new" either.. except in a constructor, and then make sure to do the "delete" in the destructor. RAII is a very important resource management C++ idiom.
Personally, I almost always let SDL handle opening the window context. There's some limitations to that and I don't know if you need more capability, but like I said, I almost never need more.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.