Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Does this function have memory problem?

Reply
Thread Tools

Does this function have memory problem?

 
 
xz
Guest
Posts: n/a
 
      04-04-2008
Does this function have memory problem?

void pushSomthingIntoVector(vector<T> & v) { //T is a class
v.assign(0, T(100)) //T(int a) is a constructor of class T
v.push_back(T(50))
}

void anotherFunction() {
vector<T> v;
pushSomethingIntoVector(v);
cout << v[0] << endl;
cout << v[1] << endl;
}

T(100) and T(50) are created locally within the function
pushSomthingIntoVector(vector<T> & v), does it exsit outside of the
function
? say, in the two lines of "cout" ?

Should I change the code as follows:

void pushSomthingIntoVector(vector<T> & v) { //T is a class
v.assign(0, *(new T(100)) ) //T(int a) is a constructor of class T
v.push_back(*(new T(50)))
}
 
Reply With Quote
 
 
 
 
Sharad
Guest
Posts: n/a
 
      04-04-2008

"xz" <> wrote in message
> Does this function have memory problem?
>
> void pushSomthingIntoVector(vector<T> & v) { //T is a class
> v.assign(0, T(100)) //T(int a) is a constructor of class T
> v.push_back(T(50))
> }
>
> void anotherFunction() {
> vector<T> v;
> pushSomethingIntoVector(v);
> cout << v[0] << endl;
> cout << v[1] << endl;
> }
>
> T(100) and T(50) are created locally within the function
> pushSomthingIntoVector(vector<T> & v), does it exsit outside of the
> function
> ? say, in the two lines of "cout" ?


Yes, they do exist outside the function. You pass vector by reference, hence
the changes to vector are seen outside the function too. Internally copies
of the temporary objects (that you create in pushSomthingIntoVector) are
stored inside the vector. They exist even when the function returns.

> Should I change the code as follows:
>
> void pushSomthingIntoVector(vector<T> & v) { //T is a class
> v.assign(0, *(new T(100)) ) //T(int a) is a constructor of class T
> v.push_back(*(new T(50)))
> }


No, let std::vector do the memory management for you.

Sharad
P.S. - Check the documentation of vector's assign member function.


 
Reply With Quote
 
 
 
 
asterisc
Guest
Posts: n/a
 
      04-05-2008
On Apr 5, 1:33 am, xz <zhang.xi...@gmail.com> wrote:
> Does this function have memory problem?
>
> void pushSomthingIntoVector(vector<T> & v) { //T is a class
> v.assign(0, T(100)) //T(int a) is a constructor of class T
> v.push_back(T(50))
>
> }
>
> void anotherFunction() {
> vector<T> v;
> pushSomethingIntoVector(v);
> cout << v[0] << endl;
> cout << v[1] << endl;
>
> }
>
> T(100) and T(50) are created locally within the function
> pushSomthingIntoVector(vector<T> & v), does it exsit outside of the
> function
> ? say, in the two lines of "cout" ?
>
> Should I change the code as follows:
>
> void pushSomthingIntoVector(vector<T> & v) { //T is a class
> v.assign(0, *(new T(100)) ) //T(int a) is a constructor of class T
> v.push_back(*(new T(50)))
>
> }


Let std::vector to do his job (manage memory allocations/
deallocations).
One advice though,instead of:

void anotherFunction() {
> vector<T> v;
> pushSomethingIntoVector(v);
> cout << v[0] << endl;
> cout << v[1] << endl;
>


try to use
for( vector<T>::const_iterator it = v.begin(); it != v.end(); ++it )
cout << *it << endl;

to be sure that your previous function push something there.

Retrieving elements from a vector won't do any bounds check and won't
throw any exception.
 
Reply With Quote
 
Obnoxious User
Guest
Posts: n/a
 
      04-05-2008
On Fri, 04 Apr 2008 17:30:48 -0700, asterisc wrote:
>
> Retrieving elements from a vector won't do any bounds check and won't
> throw any exception.


std::vector<>::at() throw(std:ut_of_range);

http://www.cplusplus.com/reference/stl/vector/at.html

--
OU
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-05-2008
On 5 avr, 02:30, asterisc <Rares....@ni.com> wrote:

> Retrieving elements from a vector won't do any bounds check
> and won't throw any exception.


Using the [] operator with an out of bounds value is undefined
behavior. In any modern implementation, I would expect an
assertion failure, or something similar. (It's the case with
g++ and VC++, at least.)

And of course, if you have an unforseen out of bounds condition,
you don't want an exception; you want the program to crash.

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-08-2008
On Apr 7, 5:04 pm, Ron Natalie <r...@spamcop.net> wrote:
> James Kanze wrote:
> > Using the [] operator with an out of bounds value is undefined
> > behavior. In any modern implementation, I would expect an
> > assertion failure, or something similar. (It's the case with
> > g++ and VC++, at least.)


> Provided you are in a mode with such assertions turned on.


> VC++ won't do it in release mode.


Now Ron, you know better. The compiler does what you tell it to
do. And VC++ (like every other compiler) offers tons of
options, not just one or two modes. The code I deliver has the
assertions turned on unless there is a real performance
problems.

It is true that typically, code compiled with them on and code
compiled with them off cannot be linked together. (Worse: at
least with g++, it can be linked without problems; it just core
dumps when you run it.) Which means that one tight loop, and
you have to recompile the entire application and all libraries
with it turned off everywhere.

This also means that if I'm delivering a library, I have to
deliver it in two versions, since I can't know in advance
whether the client code will have a performance problem
elsewhere which will require turning the assertions off.

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      04-11-2008
In article <06a70357-8f00-4673-b305-
>,
says...
> Does this function have memory problem?
>
> void pushSomthingIntoVector(vector<T> & v) { //T is a class
> v.assign(0, T(100)) //T(int a) is a constructor of class T
> v.push_back(T(50))
> }


This doesn't seem to make any sense. The vector starts out empty, so you
could just as well use:

void pushSomethingIntoVector(vector<T> &v) {
v.push_back(T(100));
v.push_back(T(50));
}

> void anotherFunction() {
> vector<T> v;
> pushSomethingIntoVector(v);
> cout << v[0] << endl;
> cout << v[1] << endl;
> }


Here you're taking for granted that the modification succeeds. I'd do
something like:

copy(v.begin(), v.end(), ostream_iterator<T>(std::cout, "\n"));

This will only copy out the real contents of the vector, not what you
_think_ you put there.

[ ... ]

> Should I change the code as follows:
>
> void pushSomthingIntoVector(vector<T> & v) { //T is a class
> v.assign(0, *(new T(100)) ) //T(int a) is a constructor of class T
> v.push_back(*(new T(50)))
> }


No, you should not. You're using new to allocate objects, but you're NOT
keeping a pointer to them afterwards. That means you have no way of
deleting those objects, so the memory is inevitably leaked.

--
Later,
Jerry.

The universe is a figment of its own imagination.
 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      04-11-2008
In article <c50ffbd8-2a5d-4179-a3ae-
>, says...

[ ... ]

> try to use
> for( vector<T>::const_iterator it = v.begin(); it != v.end(); ++it )
> cout << *it << endl;


While I agree with using begin() and end() to find the current size of
the vector, I do NOT agree that a for-loop is the way to go here. There
are things that get more complex when you try to use standard
algorithms, but this is NOT one of them. I'd advise using std::copy
instead.

--
Later,
Jerry.

The universe is a figment of its own imagination.
 
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
Does ModelSim or any simulator software have a function similar tothe standard function any logic analizer has? Weng Tianxiang VHDL 7 09-11-2009 04:47 PM
Does a resource have to have all phrases in it? =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?= ASP .Net 4 02-03-2006 08:58 AM
If your Tv has progresive Scan does your DVD have to have it to work ? Alyssa DVD Video 5 01-06-2006 11:10 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
I have the memory but the PC don't have the will Jack B Computer Support 7 10-11-2003 04:33 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57