473,396 Members | 1,840 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,396 software developers and data experts.

I think it cannot be done but why not?

Hi,

if I have an array, any array, why can I not get it's size?

for example

unsigned char *szTest = new unsigned char[10];

....

how can I get it's size?

I mean if memory for it is allocated then I should be able to get its size?

By size I mean the number of items in the array.

so in the example above

int nSize = sizeofarray(szTest); // = 10;

or even get the total allocated size

int nSize = arraysize(szTest)/sizeof(szTest[0])); // = 10;

regards.

Simon
Jul 22 '05 #1
8 1007

"Simon" <si*********@hotmail.com> wrote in message
news:2v*************@uni-berlin.de...
Hi,

if I have an array, any array, why can I not get it's size?

for example

unsigned char *szTest = new unsigned char[10];

...

how can I get it's size?

I mean if memory for it is allocated then I should be able to get its size?
By size I mean the number of items in the array.

so in the example above

int nSize = sizeofarray(szTest); // = 10;

or even get the total allocated size

int nSize = arraysize(szTest)/sizeof(szTest[0])); // = 10;

regards.

Simon


The answer to all your problems is to use a vector instead. Vector is a
standard C++ class that operates very much like an array but has a whole
load of extra features. For instance your code above could be written like
this

#include <vector>

std::vector<unsigned char>szTest(10); // a vector of size 10
int nSize = szTest.size(); // the size!!!

john

Jul 22 '05 #2
John Harrison wrote:


The answer to all your problems is to use a vector instead. Vector is a
standard C++ class that operates very much like an array but has a whole
load of extra features. For instance your code above could be written like
this

#include <vector>

std::vector<unsigned char>szTest(10); // a vector of size 10
int nSize = szTest.size(); // the size!!!

john


I understands that there are ways around, I am just curious if/why the
standard does not have a function to tell me how much memory was
allocated for a certain item.

Simon
Jul 22 '05 #3
Simon wrote:
[snip] I understands that there are ways around, I am just curious if/why the
standard does not have a function to tell me how much memory was
allocated for a certain item.


Historical reasons: It was that way in C.

Why it was that way in C, I can only guess:
Memory considerations. The compiler would need
to store the number of elements somewhere and it seems
that K&R didn't want to do that. When you dynamically allocate
an array you already know its size, you have given it to
malloc (or new in the C++ case). Same for an auto-generated
array. The program already knows the size, so why store it with
the array?
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #4
On Wed, 10 Nov 2004 12:21:12 +0000, Simon <si*********@hotmail.com>
wrote:
Hi,

if I have an array, any array, why can I not get it's size?
The size of a dynamically allocated array might not be known at
runtime.
for example

unsigned char *szTest = new unsigned char[10];

...

how can I get it's size?
You can't portably. _msize is supported by some C runtimes, but may
not do what you want.
I mean if memory for it is allocated then I should be able to get its size?


Why? Some platforms might only record the number of elements in arrays
of objects with non-trivial destructors (so that it can destroy the
right number when you do delete[]). Also, the size of the block of
memory may be larger than the size of the array in any case. e.g. an
allocation of size 10 might be rounded up to 12 or 16.

Generally, you should use std::vector rather than a dynamic array.
std::vector can usually replace every use of bare dynamic arrays in a
typical program.

Tom
Jul 22 '05 #5
Karl Heinz Buchegger wrote:
Simon wrote:

[snip]
I understands that there are ways around, I am just curious if/why the
standard does not have a function to tell me how much memory was
allocated for a certain item.

Historical reasons: It was that way in C.

Why it was that way in C, I can only guess:
Memory considerations. The compiler would need
to store the number of elements somewhere and it seems
that K&R didn't want to do that. When you dynamically allocate
an array you already know its size, you have given it to
malloc (or new in the C++ case). Same for an auto-generated
array. The program already knows the size, so why store it with
the array?

You have to understand that, though pointers and arrays are so similar
in many ways, they are not equal :-)
when ever you define a pointer and with whatever value you assign it,
sizeof(the_pointer) will give the answer: the size of an pointer, which
in many of the platforms is 4 bytes.
Jul 22 '05 #6
* Simon:

I understands that there are ways around, I am just curious if/why the
standard does not have a function to tell me how much memory was
allocated for a certain item.


Note that there are two items here:
A) How much memory was allocated (and should later be deallocated).

B) How large is the usable part of an array.
Regarding (A) that number _may_ be represented by the memory management. It
can be explicitly represented, e.g. as a field attached in front of the
memory area you get a pointer to. Or it can be implicitly represented. Or,
if the memory manager is one that sits on top of another one (e.g., calling
directly down to the OS), it might not even know the number (A). Requiring
a representation of (A) such that that number could be efficiently retrieved
could therefore impose some unacceptable overhead, depending on the details.

Another reason for not handing you the number (A) is that (B) might be very much
smaller than (A). For an array of class type objects, with constructors and
destructors, the rest of the memory area after the (B) first bytes is just unusable
garbage, so (B) is the only number that is safe to hand you.

The argument for not requiring the memory manager to store (B) is the same as for
(A), only more so.

For even though most of them -- if not all -- actually store (A) somewhere,
they have no reason of their own to store (B), so that would be pure overhead.
You would be paying for something very seldom used. And not having to pay for
something you don't use is a very important guiding principle in C++.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #7

"Tom Widmer" <to********@hotmail.com> wrote in message
news:0q********************************@4ax.com...
On Wed, 10 Nov 2004 12:21:12 +0000, Simon <si*********@hotmail.com>
wrote:
Hi,

if I have an array, any array, why can I not get it's size?


The size of a dynamically allocated array might not be known at
runtime.


I take it you mean at "compile time"? It's certainly known at runtime, as
it is allocated then, and the allocator had to know the number of elements.
(It's right there in the new statement!)
for example

unsigned char *szTest = new unsigned char[10];


[To the OP:]

And of course, since it IS right there in the new statement, there's no
reason it couldn't have been stored in a const int variable by the
programmer in the first place, and that variable later used when the size is
needed.

If you need to pass the array to a function, and need the function to know
the array size, then you need another parameter that states the number of
elements in the array. An easy way to handle that is to wrap the array in a
class which knows the array's size. And that's exactly what a vector
provides you (along with other niceties).

As for the memory used, that's oviously a whole other question, and depends
upon the memory used by each of the objects in the array, which may vary
among the objects in the array, and may change over time as those objects do
their own dynamic allocation of members.

-Howard
Jul 22 '05 #8
On Wed, 10 Nov 2004 15:31:06 GMT, "Howard" <al*****@hotmail.com>
wrote:

"Tom Widmer" <to********@hotmail.com> wrote in message
news:0q********************************@4ax.com.. .
On Wed, 10 Nov 2004 12:21:12 +0000, Simon <si*********@hotmail.com>
wrote:
Hi,

if I have an array, any array, why can I not get it's size?


The size of a dynamically allocated array might not be known at
runtime.


I take it you mean at "compile time"? It's certainly known at runtime, as
it is allocated then, and the allocator had to know the number of elements.
(It's right there in the new statement!)


No, I do mean runtime. Obviously the size is known at the moment of
allocation, but after that some allocators thrown it away. Some
allocators do store the size in an accessible way in some cases (e.g.
in the 4 bytes preceding the start of the allocated memory), but
others don't.

Tom
Jul 22 '05 #9

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

Similar topics

1
by: MLH | last post by:
I had an MS Access app translated into a PHP - MySQL app. The purpose of porting the app was so the app could be run by websurfers right from their browsers. Knowing nothing of PHP, I am trying...
1
by: Navin | last post by:
Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. hi, guys i have asp application running on iis 5.0 windows 2000 i use...
2
by: Yasutaka Ito | last post by:
Hi folks! I have a BaseForm class that inherits System.Windows.Forms.Form. It has a property, whose value I need supplied by the class that inherits it. The BaseForm usees the value supplied...
6
by: deancoo | last post by:
Hello all, I'm having a problem compiling some code which I believe should compile. Can someone tell me what I'm doing wrong. The code in question is appended below, followed by the compiler...
13
by: yoyoyo | last post by:
hi everybody! i'm trying to learn javascript in order to build a function that i need to put in my website.. since i have not so much time to do that i decided to ask to u in the net..probably...
9
by: ani | last post by:
I have been posting this question for the past 2 weeks and there wasn't any response. I really don't understand why I am not getting any responses. Does my question sound very stupid? Anyway, here...
5
by: Chad | last post by:
I want to create a class which contains a date, integer and string property which all allow a NULL value. I don't want to store the values in an Object datatype. I prefer more strongly typed...
1
by: Daniel Mark | last post by:
Hello all: I have following code that works well under command line, but it doesn't work after I convert it as exe application. ############### file: testPath.py import getopt, math, sys,...
5
by: Just_a_fan | last post by:
I tried to put an "on error" statement in a routine and got the message that I cannot user "on error" and a lamda or query expression in the same routine. Help does not list anything useful for...
0
by: raylopez99 | last post by:
I ran afoul of this Compiler error CS1612 recently, when trying to modify a Point, which I had made have a property. It's pointless to do this (initially it will compile, but you'll run into...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.