LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-30-2011, 04:19 PM   #1
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
C++ class member pointer changes after function returns


I'm venturing into the world of OpenGL, but in the process of writing my graphics library I ran into a problem... My code is:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>

class myGraphics {
	private:
		Display *display;
		char* myChar;
	public:
		int createWindow(int,int);
		void printMahPointer(void);
};

int myGraphics::createWindow(int width, int height){
	Display *display = XOpenDisplay(0);
	myChar = (char*) malloc(1);
	printf("%p %p\n",(void*) display,myChar);
	return 0;
}

void myGraphics::printMahPointer(void){
	printf("%p %p\n",(void*) display,myChar);
}
 
int main(void){
	myGraphics graphics;
	graphics.createWindow(100,100);
	graphics.printMahPointer();
	return 0;
}
I then compiled this with
Code:
g++ graphics.cpp -o graphics -lX11 -Wall
and got output:



Code:
0x601010 0x602680
0x7fff7f3f3fa0 0x602680
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
 
Old 03-30-2011, 04:26 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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.
 
1 members found this post helpful.
Old 03-30-2011, 04:34 PM   #3
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632

Original Poster
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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?
 
Old 03-30-2011, 04:39 PM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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.
 
Old 03-30-2011, 04:44 PM   #5
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632

Original Poster
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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.
 
Old 03-30-2011, 06:46 PM   #6
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Quote:
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++: How to put a pointer to a function, in a class template, without typedef'ing it? Aquarius_Girl Programming 3 03-05-2011 07:52 PM
passing a class member function to pthread_create. dmail Programming 1 07-29-2006 11:15 AM
C++ pointer to downcast member function Wondre Programming 12 02-21-2003 02:56 AM
c++ Pointer to Member Function Wondre Programming 0 02-15-2003 06:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration