473,322 Members | 1,566 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

get the allocated size of a vector

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;
}
Oct 10 '08 #1
5 2624
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.comwrote:
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;

}

Oct 10 '08 #2
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;
}
Oct 10 '08 #3
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
Oct 10 '08 #4
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
Oct 10 '08 #5
On Oct 10, 7:02*am, utab <umut.ta...@gmail.comwrote:
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....d11a2cc9c0d7be

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?"
Oct 10 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jeff Williams | last post by:
Ok, everyone loves to talk about dynamic arrays and ptr's etc, they provide endless conversation :) so here goes: Will this leak memory (my intuition says yes): void foo(vector<int*>& vec) {...
31
by: bilbothebagginsbab5 AT freenet DOT de | last post by:
Hello, hello. So. I've read what I could find on google(groups) for this, also the faq of comp.lang.c. But still I do not understand why there is not standard method to "(...) query the...
12
by: Sabiyur | last post by:
Hi All, Is there any way to find out how much memory is allocated for a given pointer. For example: int *p=new int; I know, compiler keeps the information of how much bytes are allocated...
7
by: Jo | last post by:
Hi, How can i differentiate between static and dynamic allocated objects? For example: void SomeFunction1() { CObject *objectp = new CObject; CObject object;
11
by: mast2as | last post by:
This question has been posted to this forum before and I read the thread but found that the answers were perhaps imcomplete, so I am trying again. Whenever I am creating objects I would like to...
7
by: Serpent | last post by:
The C-FAQ describes some techniques here: http://c-faq.com/aryptr/dynmuldimary.html I was using something slightly different from the C-FAQ and I was wondering if it was legal. Say I want a...
15
by: tom | last post by:
why delete the dynamically allocated memory twice causes an error, see the code below: int _tmain(int argc, _TCHAR* argv) { int *pi = new int(12); cout<<*pi; delete pi; delete pi; }
13
by: pratap | last post by:
how could i find out how much memory is blocked(or has been allocated to a pointer) consider, int *p=new int; or int *p=new int; suppose i dont know the right hand side of the statement...
8
by: Ray D. | last post by:
I'm trying to write a C function to compute the compressed row storage (CRS) vectors of a 2-d matrix. This is used primarily for increasing computing efficiency of matrices whose main elements are...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.