Solved

finding the array size (sizeof)

Posted on 2007-03-20
6
1,035 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
[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
  • 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
Industry Leaders: 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!

 

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

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!

Question has a verified solution.

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

Suggested Solutions

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…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

726 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