[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

get the allocated size of a vector

utab

10/10/2008 10:09:00 AM

Dear,

Is there a way to get the allocated size of the val in the code below?
Actual problem is that I have a double pointer in code and its addressed
is passed to a routine which does some allocation for this pointer to
pointer if I am right in the description. I tried to reproduce a very
simple code for this. Is that possible to get the allocated size in bytes
in main or it should be done in the function itself, but in main I should
be able to perform some operations on this vector? This is actually a C
code I am trying to interface in C++.

#include<iostream>

using namespace std;

void alloc(double **ptr)
{
int i;
*ptr = new double[10];
for(i=0;i<10;++i)
(*ptr)[i]=i*0.36;
}

int main()
{
double* val;
alloc(&val);
// how to get the allocated size or is that
// possible to retrieve some elements for the
// above allocation ?
return 0;
}
5 Answers

pbozzoli

10/10/2008 12:23:00 PM

0

You can't use _countof(), it only works with arrays.
The simplest way I can think of, is to add a variable (size) to your
alloc function:

void alloc(double **ptr, int *size)
{
int i;
*ptr = new double[10];
for(i=0;i<10;++i)
(*ptr)[i]=i*0.36;
*size = sizeof(double)*10;
}

I hope this helps.
Bye
Paolo

On 10 Ott, 12:08, utab <umut.ta...@gmail.com> wrote:
> Dear,
>
> Is there a way to get the allocated size of the val in the code below?
> Actual problem is that I have a double pointer in code and its addressed
> is passed to a routine which does some allocation for this pointer to
> pointer if I am right in the description. I tried to reproduce a very
> simple code for this. Is that possible to get the allocated size in bytes
> in main or it should be done in the function itself, but in main I should
> be able to perform some operations on this vector? This is actually a C
> code I am trying to interface in C++.
>
> #include<iostream>
>
> using namespace std;
>
> void alloc(double **ptr)
> {
>   int i;
>   *ptr = new double[10];
>   for(i=0;i<10;++i)
>     (*ptr)[i]=i*0.36;
>
> }
>
> int main()
> {
>   double* val;
>   alloc(&val);
>   // how to get the allocated size or is that
>   // possible to retrieve some elements for the
>   // above allocation ?
>   return 0;
>
> }
>
>

red floyd

10/10/2008 1:58:00 PM

0

utab wrote:
> Dear,
>
> Is there a way to get the allocated size of the val in the code below?

No. You have to maintain the count yourself. We just had a whole
thread on this.


> Actual problem is that I have a double pointer in code and its addressed
> is passed to a routine which does some allocation for this pointer to
> pointer if I am right in the description. I tried to reproduce a very
> simple code for this. Is that possible to get the allocated size in bytes
> in main or it should be done in the function itself, but in main I should
> be able to perform some operations on this vector? This is actually a C
> code I am trying to interface in C++.
>
> #include<iostream>
>
> using namespace std;
>
> void alloc(double **ptr)
> {
> int i;
> *ptr = new double[10];
> for(i=0;i<10;++i)
> (*ptr)[i]=i*0.36;
> }
>
> int main()
> {
> double* val;
> alloc(&val);
> // how to get the allocated size or is that
> // possible to retrieve some elements for the
> // above allocation ?
> return 0;
> }

utab

10/10/2008 2:03:00 PM

0

On Fri, 10 Oct 2008 06:57:36 -0700, red floyd wrote:

> utab wrote:
>> Dear,
>>
>> Is there a way to get the allocated size of the val in the code below?
>
> No. You have to maintain the count yourself. We just had a whole
> thread on this.

I guess I missed that can you point me to that thread, thx

Hendrik Schober

10/10/2008 2:40:00 PM

0

pbozzoli wrote:
> You can't use _countof(), it only works with arrays.
> The simplest way I can think of, is to add a variable (size) to your
> alloc function:

Actually, the simplest way would be to use 'std::vector'.
It's made for just this type of problems, certainly knows
its own size, doesn't leak, and interfaces with C APIs.

Schobi

red floyd

10/10/2008 4:36:00 PM

0

On Oct 10, 7:02 am, utab <umut.ta...@gmail.com> wrote:
> On Fri, 10 Oct 2008 06:57:36 -0700, red floyd wrote:
> > utab wrote:
> >> Dear,
>
> >> Is there a way to get the allocated size of the val in the code below?
>
> > No.  You have to maintain the count yourself.  We just had a whole
> > thread on this.
>
> I guess I missed that can you point me to that thread, thx

1. Sorry, it was in c.l.c++.moderated. Here's the thread:
http://groups.google.com/group/comp.lang.c++.moderated/browse_frm/thread/3029d75616283ee8/e9d11a...

2. Be careful with your terminology. "vector" has a specific meaning
when discussed here (usually std::vector). You are asking "how do I
get the size of a memory block allocated on the free store?"