[C++] how to check how much is allocated?

This topic is 3944 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

consider this code
void check(int& number_)
{
int n;
// n=?
for(int i=0; i<n; i++){
number_= i;
}
}

main()
{
int *number= new int[10];
fill(*number);
}


is there a way to set 'n' to how many elements were allocated in main()?

Share on other sites
Directly, no. Besides, references are not pointers, they do not allow either array indexing or pointer arithmetic.

There is an alternative:
void fill(std::vector& v){    for( int i=0 ; i< v.size() ; i++){        v= i;    }}int main(){    std::vector vec(10);    fill(vec);}

In general, when using C++ one should not use new[] and delete[], one should use std::vector<>.

Share on other sites
Pass it as a parameter?

void check(int* number_, int size){    for(int i=0; i<size; i++){        number_= i;    }}main(){    int *number= new int[10];    fill(number, 10);}

Also, you should really use std::vector instead of arrays.

Share on other sites
ah, ok. i was wondering, because i saw this as an assignment.

another question regarding std::vector.
except for their safety, why should i use them? are they as fast as arrays?
also, including the STL libs makes my executable bloated, doesn't that affect the performance, as well?

ty.

Share on other sites
Quote:
 Original post by Thirtheah, ok. i was wondering, because i saw this as an assignment.another question regarding std::vector.except for their safety, why should i use them? are they as fast as arrays?also, including the STL libs makes my executable bloated, doesn't that affect the performance, as well?ty.

In release mode, std::vector instances are as fast as dynamically allocated arrays. I expect that your compiler/linker would cut a lot of the bloat when compiling in release mode too.

Share on other sites
hmm, i never compiled a final ver. using std::vector before..
well that's good news, i'm off to using std::vector :)
thanks!

Share on other sites
Quote:
 Original post by Gage64Pass it as a parameter?*** Source Snippet Removed ***Also, you should really use std::vector instead of arrays.

Any in-depth reason for this?

Share on other sites
Quote:
Original post by ColeFreeman
Quote:
 Original post by Gage64Pass it as a parameter?*** Source Snippet Removed ***Also, you should really use std::vector instead of arrays.

Any in-depth reason for this?

They are better in every way. Don't you know that Arrays are evil? [smile]

Share on other sites
Quote:
 Original post by ColeFreemanAny in-depth reason for this?

For what? Passing the size as a parameter? How else would you do it?

Or are you asking why std::vector is a better choice? In that case, automatic memory management (resizing and cleanup) and the ability to be used in STL algorithms are some of the reasons. In general, they are superior to arrays because when working with them you don't have to worry about various low-level details (memory management, tracking the current element, maintaining the size, etc.). In other words, vectors allow you to work at a higher level of abstraction.

Share on other sites
Quote:
 Original post by rip-offIn release mode, std::vector instances are as fast as dynamically allocated arrays. I expect that your compiler/linker would cut a lot of the bloat when compiling in release mode too.

Provided, of course, you don't use a compiler that uses checked iterators in release mode or turn them off if it does.

Though, in general, there are compiler dependent mechanisms for determining the size of a memory block at runtime. For example, MSVC provides _msize(), which will let you know the size of a block allocated by malloc(). However, if you find yourself needing to use them, chances are your design needs work.

1. 1
2. 2
Rutin
25
3. 3
4. 4
5. 5

• 9
• 13
• 19
• 14
• 9
• Forum Statistics

• Total Topics
632940
• Total Posts
3009328
• Who's Online (See full list)

There are no registered users currently online

×

Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!