Can I determine the size/length of an array in C++ without having to hardcode it? -


i looking sort of "dynamic" way of passing size/length of array function.


i have tried:

void printarray(int arrayname[]) {     for(int = 0 ; < sizeof(arrayname); ++i)     {         cout << arrayname[i] << ' ';     } } 

but realized considers bytesize , not how many elements on array.


and also:

void printarray(int *arrayname) {     while (*arrayname)     {         cout << *arrayname << ' ';         *arrayname++;         } } 

this has @ least printed me more expected, doesn't work how want to. reckon because don't tell how big need plays "safe" , throws me big size , starts printing me odd integers after last element in array.


so got work around, yet believe there better out there!:

void printarray(int *arrayname) {     while (*arrayname)     {         if (*arrayname == -858993460)         {             break;         }         cout << *arrayname << ' ';         *arrayname++;     }     cout << '\n'; } 

after running program few times realized value after last element of array have input always: -858993460, made break while loop once value encountered.


include <iostream> include <conio.h>  using namespace std;      // functions prototypes void printarray (int arrayname[], int lengtharray);      // global variables      //main int main () {     int firstarray[] = {5, 10, 15};     int secondarray[] = {2, 4, 6, 8, 10};     printarray (firstarray,3);     printarray (secondarray,5);      // end of program     _getch();     return 0; }      // functions definitions void printarray(int arrayname[], int lengtharray)  {     (int i=0; i<lengtharray; i++)     {         cout << arrayname[i] << " ";     }     cout << "\n"; } 

thank much.

1st try

when arrays passed functions decay pointers. normally, using sizeof on array give size in bytes divide size in bytes of each element , number of elements. now, since have pointer instead of array, calling sizeof gives size of pointer (usually 4 or 8 bytes), not array , that's why fails.

2nd try

the while loop in example assumes array ends 0 , that's bad (unless did use 0 terminator null-terminated strings example do). if array doesn't end 0 might accessing memory isn't yours , therefore invoking undefined behavior. thing happen array has 0 element in middle print first few elements.

3rd try

this special value found lurking @ end of array can change time. value happened there @ point , might different time hardcoding dangerous because again, end accessing memory isn't yours.

your final code

this code correct , passing length of array along array commonly done (especially in apis written in c). code shouldn't cause problems long don't pass length that's bigger real length of array , can happen error prone.

another solution

another solution use std::vector, container along keeping track of size, allows add many elements want, i.e. size doesn't need known @ runtime. this:

#include <iostream> #include <vector> #include <cstddef>  void print_vec(const std::vector<int>& v) {     std::size_t len = v.size();      (std::size_t = 0; < len; ++i)     {         std::cout << v[i] << std::endl;     } }  int main() {     std::vector<int> elements;      elements.push_back(5);     elements.push_back(4);     elements.push_back(3);     elements.push_back(2);     elements.push_back(1);      print_vec(elements);      return 0; } 

useful links worth checking out

undefined behavior: undefined, unspecified , implementation-defined behavior

array decay: what array decaying?

std::vector: http://en.cppreference.com/w/cpp/container/vector


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -