-1

I'm new to C/C++ and I want to know what is the proper way to pass array to function without copy for example:

void someFunction(uint8_t &array) {
    // some work here
}

int main() {
    uint8_t *array = (uint8_t*) malloc(sizeOf(uint8_t) * 100);
    someFunction(*array);
}

Is this way correct and does the array copy when I pass it to someFunction()?

easy_breezy
  • 1,073
  • 7
  • 18
  • 3
    1) There is no such language as C/C++. What you are using is, clearly C++ (as you can't compile such code with C compiler) 2) What made you think, that passing `uint8_t*` to the function would copy the array? – Algirdas Preidžius Aug 26 '20 at 16:12
  • 1
    Do you know of the way to pass array by copy? – Dan M. Aug 26 '20 at 16:12
  • A function defined as `void someFunction(uint8_t &array)` gets passed a **single** `uint8_t` by reference. – Andrew Henle Aug 26 '20 at 16:14
  • dont use `malloc` in C++, it isnt wrong here, but only by coincidence, and there is no reason to use it – 463035818_is_not_an_ai Aug 26 '20 at 16:15
  • In C, there's no way to pass an array to a function *except* by reference. In C++, you use vectors. – Lee Daniel Crocker Aug 26 '20 at 16:19
  • 1
    There is no array in your example. You are using pointers. – PaulMcKenzie Aug 26 '20 at 16:22
  • @AlgirdasPreidžius I just wanted to know is it a correct way because I'm new to C++, so if I pass any type of value e.g. uint8_t, int, char, struct or an instance of some class by reference to some function it's prevent the argument from copying? – easy_breezy Aug 26 '20 at 16:24
  • @easy_breezy Are you asking a C++ question or a C question? There are different ways for passing arrays to functions in C and in C++. – François Andrieux Aug 26 '20 at 16:25
  • 3
    @easy_breezy "_I just wanted to know is it a correct way because I'm new to C++_ "In C++, if you want statically-sized array, you would use `std::array`, and if you wanted to use dynamically-sized array, you would use `std::vector`, and pass references to those. – Algirdas Preidžius Aug 26 '20 at 16:26
  • 2
    @easy_breezy `int x[10];` That is an array. If you want to know how to pass that, then C++ has additional answers than `C` does. – PaulMcKenzie Aug 26 '20 at 16:32
  • @AlgirdasPreidžius ok thank you, I've already try to use std::vector but them slow down my app and when I changed them to pointers everything works fine, and if I understand it correctly the way of working with arrays that I described above it's a C and not C++ ? – easy_breezy Aug 26 '20 at 16:38
  • @easy_breezy "_I've already try to use std::vector but them slow down my app_" Then there's something wrong in how you use it (as in: your `std::vector` usage, and your pointer usage, aren't equivalent). Can't give advice on what's wrong in your usage, without seeing it. – Algirdas Preidžius Aug 26 '20 at 23:59

1 Answers1

0

You mix up some terminology. You aren't passing arrays you are passing reference to your allocated data. Arrays need to be fixed in size so you won't be able to dynamically allocate them (Edit: with that I mean you cant allocate them on the stack without knowing the size by compile time). An easier way here would be to change the signature of the function to accept a pointer

void someFunction(uint8_t *array) { /* ... */ }

and then pass the pointer by value

someFunction(array);

I also advise you to use the new and delete[] operators designed for allocating such data in C++, but it's not necessary for this question.

Edit: Of course, a safer way is to use the std::vector class for dynamic-length data and std::array class for static-length data.

Paulsor
  • 100
  • 7
  • "Arrays need to be fixed in size so you won't be able to dynamically allocate them" unclear what you mean. Arrays can be dynamically allocated. Also the advise to use `new` should at least come with a hint to `std::vector` or `std::array`. In modern C++ raw `new` should be avoided – 463035818_is_not_an_ai Aug 26 '20 at 16:24
  • @idclev463035818 Because he is new to C++ i didn't want to put too much information in this answer but I can adjust it based on your critique. – Paulsor Aug 26 '20 at 16:26