You are here:

C++/How many variables (foo) of the Foo type is created?

Advertisement


Question
Hi,

It should be like this,

for(int i=0;i<10;i++) {
Foo* foo = new Foo();
}

How many pointers are created, 1 or 10 ? how to print out them?

Thanks,
lzzzz

-------------------------
Followup To
Question -
Hi,

Given,

Foo foo = new Foo();

How many variables (foo) of the Foo type is created? and how to print out each of those variables (foo)?

Thanks,
lzzzz
Answer -
Hi,
  If Foo is a class then the above syntax is wrong....

You would get compilation errors. You here foo is not a pointer :-))

Regards,
RaiD

Answer
for(int i=0;i<10;i++) {
Foo* foo = new Foo(); // local variable foo
}

In this you get it 10 times, 10 different as each time foo would be different.After every loop you lose the handle to the memory allocated as the variable is local. It gives memory leak also as you are not freeing up the memory before going to the next loop.

Regards,
RaiD

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Dharmender Rai

Expertise

I can answer general and system level C/C++ questions.

Experience

I have 5 years of experience in C++.

©2016 About.com. All rights reserved.