Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

finding the array size (sizeof)

Posted on 2007-03-20
6
Medium Priority
?
1,092 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 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 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.
Suggested Courses

618 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