Solved

finding the array size (sizeof)

Posted on 2007-03-20
6
1,017 Views
Last Modified: 2007-12-19
I currently have an if statement which compares two values.

One of these values represents the size of an array.  I've created this value by having the following code in the loop which generates the array (size++)

Is there another way to do this, is there a function called sizeof which could be used in a function like this:

if value01 < sizeof(array01);
{
    //some code
}

Many Thanks
0
Comment
Question by:andyw27
  • 3
  • 2
6 Comments
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 18755405
>>>> Is there another way to do this

the sizeof function only works on fixed arrays but not on pointers or arrays passed to a function:

    int array[3];
    int size = sizeof(array) / sizeof(int);    // ok size == 3

    int arr* = new int[3];
    int size = sizeof(arr) / sizeof(int);  // not ok: size == 1   == pointer size

    void f(int arr[])
    {
          int size = sizeof(arr) / sizeof(int);  // not ok: size == 1   == pointer size
    }

So, for dynamic arrays it is good practice to store the size in an extra variable so that it can be passed or evaluated where needed.
    void f(int arr[], int siz)
    {
          int size = iz; // ok
    }

Regards, Alex

0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 total points
ID: 18755411
Correction:

  int* arr = new int[3];
0
 

Author Comment

by:andyw27
ID: 18755436

Cheers,

I've tried, however I'm getting the following problem:

error C2070: illegal sizeof operand
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:andyw27
ID: 18755447

Please ignore that last remark.
0
 
LVL 2

Expert Comment

by:navaneeth_f50
ID: 18759308
i think in the above comment made the size is mistaken by the length of array...so i prefer the program as
 
if value01 < size;
{

    int size = sizeof(array01) ;
    //some code
}
as the array grows it will be automatically updated.....in the above coment,
>>>>>  int size = sizeof(array) / sizeof(int);    // ok size == 3
it gives the length of the array......
size=how much space it will take to store the data......
length=how many variables it has(whatever may be the size of each variable)
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 18760640
>>>> the size is mistaken by the length of array
though I understand what navaneeth wants to say his opinion is not commonly agreed. E. g. the STL which is part of C++ standard uses member function size() for number of elements and only the string class has length as a synonym, additionally. IMO, the space an array or a container will take is a minor value and though I personally prefered items() or elements() in my former container classes, we can't oversee that size() has lost the meaning of space in bytes in most standard C++ programs.

Regards, Alex
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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.

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now