Solved

finding the array size (sizeof)

Posted on 2007-03-20
6
1,021 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ finding a sting in a char* string from a text file 3 99
FMX StringGrid1->Canvas->FillRect Problem 3 126
Why isn't object file created? 6 39
c++, dynamic object by json 1 19
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

948 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

22 Experts available now in Live!

Get 1:1 Help Now