Solved

finding the array size (sizeof)

Posted on 2007-03-20
6
1,027 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
  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 …
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

813 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

17 Experts available now in Live!

Get 1:1 Help Now