Jump to content
  • Advertisement
Sign in to follow this  
Thirthe

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

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Thirthe
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.


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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Gage64
Pass it as a parameter?

*** Source Snippet Removed ***

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


Any in-depth reason for this?

Share this post


Link to post
Share on other sites
Quote:
Original post by ColeFreeman
Quote:
Original post by Gage64
Pass 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 this post


Link to post
Share on other sites
Quote:

Original post by ColeFreeman
Any 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 this post


Link to post
Share on other sites
Quote:
Original post by rip-off
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.


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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

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!