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

Started by
23 comments, last by Thirthe 16 years, 3 months ago
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()?
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<>.
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.
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.
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.
hmm, i never compiled a final ver. using std::vector before..
well that's good news, i'm off to using std::vector :)
thanks!
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?
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]
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.
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.

This topic is closed to new replies.

Advertisement