473,385 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Using static equivalant ?

Is the following code:

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

}

mostly equivalant in operation to the following:

void foo()
{
static bool firstTimeCalled = true;
std::vector<intobj;
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?

Mar 2 '07 #1
1 1637
joseph cook wrote:
Is the following code:

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

}

mostly equivalant in operation to the following:

void foo()
{
static bool firstTimeCalled = true;
std::vector<intobj;
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<intobj;
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
Mar 2 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Water Cooler v2 | last post by:
What is the C# equivalant of the VB.NET keyword "shadows"?
3
by: Water Cooler v2 | last post by:
What is the .NET equivalant of the VB function String(Number as Long, Character)? I am forgetting it at the moment.
2
by: Mr Ideas Man | last post by:
Hi all, I am trying to create the following class: My problem is i can't seem to use this static function (VB) The error i am getting is "methods can not be declared static" Can anyone...
6
by: semkaa | last post by:
Can you explain why using ref keyword for passing parameters works slower that passing parameters by values itself. I wrote 2 examples to test it: //using ref static void Main(string args) {...
3
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
1
by: Raja Mohan | last post by:
The following code is used in vb6. 'UPGRADE_ISSUE: Constant vbUnicode was not upgraded. Click for more:...
3
by: Ciara9 | last post by:
I am having problems trying to update a field in a database using a field in a form. I currently have two fields, Today and Tomorrow in a table named Date. The Today field automatically defaults to...
8
by: Scott M. | last post by:
I've tried following the simple instructions to be able to use MyServices within C#...Add a project reference to Microsoft.VisualBasic.dll and add: "using Microsoft.VisualBasic.MyServices;" to the...
5
by: raylopez99 | last post by:
I have a form, Form6, that has a bunch of buttons overlaid on it. I want to be able to click on any arbitrary area of the form, and if that area of the form is overlaid by a button, I want to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.