Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Using static equivalant ?

Reply
Thread Tools

Using static equivalant ?

 
 
joseph cook
Guest
Posts: n/a
 
      03-02-2007
Is the following code:

void foo()
{
static std::vector<int> obj (500);
obj.clear();

}

mostly equivalant in operation to the following:

void foo()
{
static bool firstTimeCalled = true;
std::vector<int> obj;
if(firstTimeCalled)
{
obj.resize(500);
firstTimeCalled = false;
}
obj.clear();
}

With regards that I am only allocating memory for the 500 ints once,
no matter how many times this function is called?

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-02-2007
joseph cook wrote:
> Is the following code:
>
> void foo()
> {
> static std::vector<int> obj (500);
> obj.clear();
>
> }
>
> mostly equivalant in operation to the following:
>
> void foo()
> {
> static bool firstTimeCalled = true;
> std::vector<int> obj;
> if(firstTimeCalled)
> {
> obj.resize(500);
> firstTimeCalled = false;
> }
> obj.clear();
> }


Yes, looks like it.

> With regards that I am only allocating memory for the 500 ints once,
> no matter how many times this function is called?


It's guaranteed that 'clear' doesn't shrink the allocated vector
storage, so, yes, it should be keeping the 500 reserved.

You might want to use 'reserve' instead, maybe:

static std::vector<int> obj;
static int dummy = (obj.reserve(500), 42);

although for 'int' it probably doesn't matter. You still need to
'clean' it at some point, right?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Any sense in using private static final for non-static classes? Ricardo Palomares Martinez Java 2 04-28-2007 10:45 PM
PIX public/24 ip static mapping means 256 times interfaces static maps? Nieuws Xs4all Cisco 2 05-26-2005 06:25 PM
PIX public/24 ip static mapping means 256 times interfaces static maps? Nieuws Xs4all Cisco 0 05-26-2005 11:07 AM
What is the WEB equivalant for Console.WriteLine() Chuck Insight ASP .Net Web Controls 14 03-05-2005 04:03 AM
Static vs. non-static connection Natan ASP .Net 8 05-26-2004 08:21 AM



Advertisments