Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 07-06-2004, 01:29 PM   #1
Senior Member
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Rep: Reputation: 55
Numerous C++ questions

Hey peeps,

I have a couple of (fairly simple) C++ questions I'd like answered, if you'd be so kind:

1. If I have a method within a class that creates a pointer to an object using the new keyword, can it return a pointer to that object or is it lost on procedure exit?
For example:
// Incomplete code...
SDL_Surface *temp;
SDL_Surface* getFrame(){
    temp = SDL_CreateRGBSurface(...etc....);
    return temp;
Is that valid or is temp lost?

2. Does EVERYTHING I create using new need a corresponding delete statement? Until recently I believed that it was only things of indeterminate size (for example created using malloc) that needed freeing or deleting, but looking around it seems this is not the case, and now in every class's destructor I have several delete statements to cater for the numerous objects created within the class. Is this right?

That's all folks! I'm not new to programming, I've been a Java programmer for years, but C++ is throwing up some interesting new challenges for me.


Old 07-06-2004, 01:38 PM   #2
Registered: Jul 2003
Location: Malaysia
Distribution: Red Hat, Slackware 9.1
Posts: 76

Rep: Reputation: 15
The code you have written seems correct althought you should be very careful how the pointer returned by getFrame() is treated. If delete is called upon it and somewhere else in the program you try to operate on temp, you might get a segmentation fault error. Also, remember that if later on in the program another method from your class (I'm assuming temp is a variable in your class) accesses temp, the memory its pointing to might have been changed as you are allowing functions outside your class access its memory location by parsing the pointer.

As to your enquiry about a new being "accompanied" by a delete, the answer is YES. Anytime you allocate an object(s) with new you SHOULD deallocate it when you're done with the object(s) or it could lead to memory leaks.
Old 07-06-2004, 01:42 PM   #3
Senior Member
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Original Poster
Rep: Reputation: 55
Thanks, yes temp is a class variable. As everything gets deleted at once, there should be no problem in this instance of other classes accessing it after deletion.

p.s. I notice how you answered my thread and I answered yours....I just hope my answer is what you're looking for!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Numerous Newbie Problems DDRfreak2 Mandriva 9 08-12-2005 01:52 PM
Numerous Questions matt_com911 Linux - Newbie 6 12-11-2004 06:22 PM
numerous ssh attempts archdev Linux - Security 14 08-10-2004 04:14 PM
Numerous sigsegv crashes Odeusp Mandriva 0 07-20-2004 01:06 PM
Numerous newbie questions. strange Linux - Newbie 6 06-17-2003 12:01 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:36 AM.

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