?
Solved

sizeof array problem

Posted on 2006-06-23
7
Medium Priority
?
585 Views
Last Modified: 2012-05-05
Here's the code:


#include <stdafx.h>
#include <iostream>
#include <iomanip>
using namespace std ;

void outputSize( int vals [] )
{
      cout << "In outputSize():  {" << sizeof(vals)/sizeof(vals[0]) << "}" << endl ;
}

int main()
{
      int vals [] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } ;

      // output array size
      cout << "In main():  {" << sizeof(vals)/sizeof(vals[0]) << "}" << endl ;
      
      // pass array to function, where it's size should be outputted
      outputSize( vals ) ;

      return 0 ;
}


And here's the output:

  In main():  {10}
  In outputSize():  {1}


For some reason, it would seem that 'sizeof(vals)' in the outputSize() function is getting the sizeof a single element within the array, rather than the total amount of memory occupied by the entire array...

- Why is this?
- How can I solve it?


Thanks

btw: Am using VS 2005 Pro now
0
Comment
Question by:InteractiveMind
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 300 total points
ID: 16967624
void outputSize( int vals [] )

The only thing that compiler knows anout vals[] parameter is that it is pointerts int. sizeof(int*) = 4. Compiler doesn't know at compile time what array is passed to outputSize function.
sizeof operator is executed by compiler at compile time, it is replaced by constant and inserted to executable code. So, in the main function, compiler knows that vals is array of 10 integers, and in outputSize function, vals is just pointer to integer.

Solution: add size parameter to the function:

void outputSize( int vals [], int size );
...

outputSize( vals, sizeof(vals)/sizeof(int) ) ;
0
 
LVL 22

Assisted Solution

by:grg99
grg99 earned 300 total points
ID: 16967663
In C, unlike Pascal, Ada, Java, Delphi, FORTRAN 90, COBOL, SNOBOL, APL, PL/I, PL/M, BASIC+, JavaScript, Simula, REXX, Spitbol, MAtLab, Perl, and a few dozen other languages, what gets passed is just the raw address of the array, and nothing else.  So it's really hard to divine the size of the array, much less the size of each element, when neither bit of info is available.  Well, that's not quite true,  if you defined the receiving array of a specific type, then you can get the sizeof an element, but that's of no use if you want to inquire about the size of arbitrary parameters. Or you can poke through the stabs debug info, if the program was compiled with debug info turned on, AND the variable is a static, global, or local variable, (not a malloc'ed variable), and you can figure out the info.  There's also the RTTi stuff, but that's also a can of worms.

So in general, you can't.

You can FAKE it by using a macro in some cases:

#define CallAndPassSize(Func,Array)       Func( (void* )&Array, (sizeof(Array)/sizeof(Array[0])) )


You use it like this:

 CallAndPassSize(   MyFunct, A )

and receive it like this:

void MyFunct( void * ArbitraryArray, unsigned int ArrayLength ){ .... }



0
 
LVL 30

Expert Comment

by:Axter
ID: 16967670
You can get the desired functionallity by making your function a template function.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Axter
ID: 16967674
>>You can FAKE it by using a macro in some cases:

I recommed using a template over a macro, because macros enter all namespaces, where-as a template stays within it's own namespace.
0
 
LVL 22

Expert Comment

by:grg99
ID: 16967675
OOPS, I goofed, try this to get both pieces of info:

#define CallAndPassSize(Func,Array)       Func( (void* )&Array, (sizeof(Array)/sizeof(Array[0])), (sizeof(Array[0])) )


You use it like this:

 CallAndPassSize(   MyFunct, A )

and receive it like this:

void MyFunct( void * ArbitraryArray, unsigned int ArrayLength, unsigned int ArrayElements ){ .... }


0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 400 total points
ID: 16967686
Example code:

template < class T > 
void outputSize( T& vals)
{
      cout << "In outputSize():  {" << sizeof(vals)/sizeof(vals[0]) << "}" << endl ;
}

int main()
{
      int vals [] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } ;

      // output array size
      cout << "In main():  {" << sizeof(vals)/sizeof(vals[0]) << "}" << endl ;

      // pass array to function, where it's size should be outputted
      outputSize( vals ) ;

      return 0 ;
}
0
 
LVL 25

Author Comment

by:InteractiveMind
ID: 16967703
Thanks very much all.

Being a Java geek myself, and having limited C++ experience, I had no idea about this -- but you've all done a great job explaining it. It's much clearer now.

:)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question