Usage of sizeof(<dynamic array>) 
Author Message
 Usage of sizeof(<dynamic array>)

Hello,
can anybody help me?

Maybe it's a stupid question:

i want to get the size of an dynamic allociated array like this:

        int nVar = 50;

        int* nArr = new int[nVar];
        int nSize = sizeof(nArr);

if i write this, sizeof(...) returns the size of the pointer, that's
okay.

        int nVar = 50;

        int* nArr = new int[nVar];
        int nSize = sizeof(*nArr);

if i write this, sizeof(...) returns the size of one int.
But i want to have the size of the array, in this case 50.
How can i do that?

Thanks for any answer.

M. Gerlach

--
-----------------------------------------------------------------------------------

You can visit my homepage at
http://www.*-*-*.com/ ~Martin.Gerlach/welcome.html
-----------------------------------------------------------------------------------



Mon, 03 May 1999 03:00:00 GMT  
 Usage of sizeof(<dynamic array>)

|> Hello,
|> can anybody help me?
|>
|> Maybe it's a stupid question:
|>
|> i want to get the size of an dynamic allociated array like this:
|>
|>   int nVar = 50;
|>
|>   int* nArr = new int[nVar];
|>   int nSize = sizeof(nArr);

Since you've posted to comp.lang.c, one must assume that this is C code, and
thus you should be aware that "new int [nVar]" is a syntax error.  This was
probably a typo; you meant "int *nArr = malloc(nVar * sizeof(int));".

|> if i write this, sizeof(...) returns the size of the pointer, that's
|> okay.

Precisely, because nArr is a pointer to an int.

|>   int nVar = 50;
|>
|>   int* nArr = new int[nVar];
|>   int nSize = sizeof(*nArr);
|>
|> if i write this, sizeof(...) returns the size of one int.

Not surprising, since *nArr is the integer object pointed to by nArr.

|> But i want to have the size of the array, in this case 50.
|> How can i do that?

You can't.  At least, not without using some extension to the language.
Assuming that your code was written in C using malloc() instead of C++'s
operator new, there is no portable way to obtain the size of a previously
allocated block of memory.  Your best alternative is to keep track of
how much space you allocated, instead of trying to find out later.

Regards,

--
/*-------------------------------------------------------------
Chris Engebretson // CSB - SED - Scientific Systems Development
United States Geological Survey   -   National Mapping Division
Mundt Federal Building, USGS EROS Data Center
Sioux Falls, SD 57198  http://edcwww.cr.usgs.gov/eros-home.html

-------------------------------------------------------------*/



Mon, 03 May 1999 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. <<<<<<<Parsing help, please>>>>>>>>

2. File Format conversion, ascii freeform -->.csv <-->.wk1<-->dbf<-->?HELP

3. <<<>>>Need C code advice with functions and sorting.<<<>>>

4. <><><>HELP<><><> PCMCIA Motorola Montana 33.6

5. >>>Windows Service<<<

6. sizeof(<Insert_Type_Here>*) = constant?

7. sizeof() and dynamic arrays

8. converting CComVariant to BSTR for Dynamic Array usage....

9. Usage of <vector>

10. proposal: <basic.h>, <pascal.h>, <fortran.h>, <cobol.h>

11. <<<< C Grammar for yacc needed >>>>

12. - - - = = =<><> W e b - U S A <><>= = = - - -

 

 
Powered by phpBB® Forum Software