0

i have this code looks like the sizeof(skyline) returns always 8 and when deviding it with sizeof(int) that is 4 give me wrong array size . The code :

int calcualteSkyLine(const int* skyline)
{
    int arrSize = sizeof(skyline) / sizeof(int);
    for(int i=0;i <=arrSize;i++)
    {
        std::cout << skyline[i] << std::endl;
    }
}

int main(int argv,char** argc)
{
    const int  buldingArry[] = {1,3,2,1,2,1,5,3,3,4,2};
    calcualteSkyLine(buldingArry);
    return 0;
}

arrSize is always 8 .. therefor the array cant be printed , what is wrong here ? if i replace the "arrSize" with 10 all works

user63898
  • 29,839
  • 85
  • 272
  • 514
  • 1
    you pass a pointer to the function. Pointers are not arrays. – 463035818_is_not_an_ai Jan 28 '23 at 13:09
  • Does this answer your question? https://stackoverflow.com/questions/5724171/passing-an-array-by-reference – 463035818_is_not_an_ai Jan 28 '23 at 13:09
  • you should use `std::size` to get the size of a c-array. WHen used wrong it fails to compile rather than returning bogus results like `sizeof` does – 463035818_is_not_an_ai Jan 28 '23 at 13:10
  • You don't pass an array, you pass a pointer. Use [`std::array`](https://en.cppreference.com/w/cpp/container/array) (non-resizable array) or [`std::vector`](https://en.cppreference.com/w/cpp/container/vector) (resizable array) in C++ if you want to pass the length of the array too. E.g. `int calculateSkyLine(const std::vector& skyline);` And use [range based for loop] to loop over all items in the array (https://en.cppreference.com/w/cpp/language/range-for) (and don't have to manually worry about the size at all) . – Pepijn Kramer Jan 28 '23 at 13:12
  • It looks like you are learning C++ from an outdated source, look at cppreference for examples. Get a [recent C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or have a go at https://www.learncpp.com/ (that's pretty decent, and pretty up-to-date). – Pepijn Kramer Jan 28 '23 at 13:15
  • consider that `sizeof` is compile time. The `sizeof` any type is always the same. It cannot know that the pointer in the function points to an array. – 463035818_is_not_an_ai Jan 28 '23 at 13:16
  • Also `sizeof(skyline)` will return the size of the pointer (int*), not the whole array. All in all I think you have enough reason not to use "C" style arrays and fall into the traps of "pointer decay" – Pepijn Kramer Jan 28 '23 at 13:17
  • `int calcualteSkyLine(const int* skyline)` -- You literally wrote that the parameter is a `const int*`. So `sizeof(const int *) / sizeof(int)` is what that all boils down to in your function. – PaulMcKenzie Jan 28 '23 at 13:50

1 Answers1

1

What your code would look like in current C++ (and a little extra, since I needed a return value ;)

#include <numeric>
#include <iostream>
#include <vector>

int calcualteSkyLine(const std::vector<int>& skyline)
{
    for(const int value : skyline)
    {
        std::cout << value << "\n";
    }

    // return the sum of all values (another nice thing you can do with numeric/vector)
    // is to avoid (visible) for loops completely
    return std::accumulate(skyline.begin(), skyline.end(), 0);
}

int main(int argv, char** argc)
{
    std::vector<int> buldingArray{ 1,3,2,1,2,1,5,3,3,4,2 };
    auto sum = calcualteSkyLine(buldingArray);
    std::cout << "sum = " << sum;
    return 0;
}
Pepijn Kramer
  • 9,356
  • 2
  • 8
  • 19
  • Thanks , but what if i needed to use int array not vector ? – user63898 Jan 28 '23 at 15:05
  • A std::vector is a C++ integer array :) Why would you need to use something else? If it is your teacher telling you to do so then he is teaching C++ as it was before 1998 (and I know I've been using C++ since 1995). If you'd like I'd be happy to talk to him/her. Because still using"C" style arrays is just asking for bugs – Pepijn Kramer Jan 28 '23 at 15:31
  • For reference : [https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rr-newdelete](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rr-newdelete). Which basically means, use datastructures from the standard library or hide new/delete inside datastructure of your own. – Pepijn Kramer Jan 28 '23 at 15:48