Author Topic: Allocations/deallocations - tools  (Read 3781 times)

Offline Léa Massiot

  • Multiple posting newcomer
  • *
  • Posts: 39
Allocations/deallocations - tools
« on: October 30, 2011, 05:59:11 pm »
Hello,

Thank you for reading my post.
I am new to C++ and CodeBlocks.
I have three related questions.

1)  C++ question
When I declare a new "ifstream" object a new "string" object like this:
Code
void AClass::aMethod()
{
      std::ifstream inputStream;
      std::string s_wholeFileName = "";
}
How are the "ifstream" and the "string" objects destroyed?
Do I have to do it myself and if yes, how?
(I'm used to the Java garbage collector...)

2) Is there a tool inside CodeBlocks (or outside of it) that I could use to see exactly how memory is managed: memory allocations and deallocations.  A tool which would tell me after each instruction what has happened as far has memory is concerned. (I'm running CodeBlocks with the Debian Squeeze OS).

3) And as a supplementary tool to the previous one, could you advise me a tool inside of CodeBlocks (or outside of it) which would tell me if all my variables have been properly created and destroyed.

This is simply said but it would really help.
Best regards,
--
Léa

zabzonk

  • Guest
Re: Allocations/deallocations - tools
« Reply #1 on: October 30, 2011, 06:06:53 pm »
Objects like strings and streams are destroyed for you by the compiler, provided you create them like this:

     string s =  "foo";

if you create them with "new":

     string * s = new string( "foo" );

then you have to delete them yourself - so don't create them like that!

However, questions like this are off-topic here - you should ask them at sites like http://www.reddit.com/r/learnprogramming

Offline Léa Massiot

  • Multiple posting newcomer
  • *
  • Posts: 39
Re: Allocations/deallocations - tools
« Reply #2 on: October 30, 2011, 06:14:25 pm »
@Neil :
Thank you for your answer.
I admit my first question was a bit off-topic here and I thank you answering it after all.

I think the two other questions are not off-topic... so anyone on these?

Best regards,
--
Léa

Offline Alpha

  • Developer
  • Lives here!
  • *****
  • Posts: 1513
Re: Allocations/deallocations - tools
« Reply #3 on: October 30, 2011, 06:28:13 pm »
3) And as a supplementary tool to the previous one, could you advise me a tool inside of CodeBlocks (or outside of it) which would tell me if all my variables have been properly created and destroyed.
Try using the Valgrind plugin (with Valgrind of course).  I think this might be what you are looking for, but as I am on Windows, I have not used it (so I could be wrong).

Offline Léa Massiot

  • Multiple posting newcomer
  • *
  • Posts: 39
Re: Allocations/deallocations - tools
« Reply #4 on: October 30, 2011, 07:25:19 pm »
@Alpha :
Thank you for your answer.
I've installed "Valgrind", it looks promising. Thanks!
Best regards,
--
Léa