Click here to Skip to main content
15,888,977 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Passing an array as argument to a function Pin
Calin Negru28-Mar-20 22:59
Calin Negru28-Mar-20 22:59 
GeneralRe: Passing an array as argument to a function Pin
Richard MacCutchan28-Mar-20 23:23
mveRichard MacCutchan28-Mar-20 23:23 
GeneralRe: Passing an array as argument to a function Pin
Calin Negru28-Mar-20 23:44
Calin Negru28-Mar-20 23:44 
GeneralRe: Passing an array as argument to a function Pin
Richard MacCutchan28-Mar-20 23:55
mveRichard MacCutchan28-Mar-20 23:55 
GeneralRe: Passing an array as argument to a function Pin
Calin Negru29-Mar-20 0:48
Calin Negru29-Mar-20 0:48 
GeneralRe: Passing an array as argument to a function Pin
Richard MacCutchan29-Mar-20 0:50
mveRichard MacCutchan29-Mar-20 0:50 
GeneralRe: Passing an array as argument to a function Pin
Calin Negru29-Mar-20 1:14
Calin Negru29-Mar-20 1:14 
GeneralRe: Passing an array as argument to a function Pin
k505429-Mar-20 4:03
mvek505429-Mar-20 4:03 
Further to what Richard has said, you could also do some defensive programming by passing in the length of the array. eg:
C++
void myFunc(int* somedata, size_t data_len)
{
    for(size_t i = 0; i < data_len; ++i)
        somedata[i] *= 2;
    return;
}

//...
int myArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
const size_t arrLen = sizeof(myArray)/sizeof(myArray[0]);
myFunc(myArray), arrLen);
// ... etc
This will prevent data overflows, and/or segfaults, when accessing myArray inside the function.
Things to note:
* we can get the compiler to tell us how many elements in the array using the sizeof(myArray)/sizeof(myArray[0]) construct. This is calculated at compile time and in release mode, in most cases it will be optimized out, so it does not add to your memory usage, if that's a concern.
* Using the constant arrLen means that if we change the number of elements in myArray, we don't need to go through the code and find all uses of myArray and make sure we're passing in the right number
* If we had used arrLen = sizeof(myArray)/sizeof(int), we need to remember to change the definition of arrLen if we change the type of myArray. Using sizeof(myArray[0] mean that if we change the type of myArray from int[] to double[], for example, we don't need to remember to change the definition of arrLen as well
Keep Calm and Carry On

GeneralRe: Passing an array as argument to a function Pin
Richard MacCutchan29-Mar-20 4:18
mveRichard MacCutchan29-Mar-20 4:18 
GeneralRe: Passing an array as argument to a function Pin
leon de boer29-Mar-20 4:23
leon de boer29-Mar-20 4:23 
GeneralRe: Passing an array as argument to a function Pin
Richard MacCutchan29-Mar-20 4:28
mveRichard MacCutchan29-Mar-20 4:28 
GeneralRe: Passing an array as argument to a function Pin
k505429-Mar-20 4:34
mvek505429-Mar-20 4:34 
GeneralRe: Passing an array as argument to a function Pin
Richard MacCutchan29-Mar-20 5:48
mveRichard MacCutchan29-Mar-20 5:48 
GeneralRe: Passing an array as argument to a function Pin
Stefan_Lang30-Mar-20 23:05
Stefan_Lang30-Mar-20 23:05 
GeneralRe: Passing an array as argument to a function Pin
Richard MacCutchan30-Mar-20 23:37
mveRichard MacCutchan30-Mar-20 23:37 
GeneralRe: Passing an array as argument to a function Pin
leon de boer29-Mar-20 15:31
leon de boer29-Mar-20 15:31 
GeneralRe: Passing an array as argument to a function Pin
Greg Utas29-Mar-20 2:19
professionalGreg Utas29-Mar-20 2:19 
GeneralRe: Passing an array as argument to a function Pin
Calin Negru29-Mar-20 2:58
Calin Negru29-Mar-20 2:58 
GeneralRe: Passing an array as argument to a function Pin
leon de boer29-Mar-20 4:11
leon de boer29-Mar-20 4:11 
GeneralRe: Passing an array as argument to a function Pin
Greg Utas29-Mar-20 5:54
professionalGreg Utas29-Mar-20 5:54 
GeneralRe: Passing an array as argument to a function Pin
Calin Negru29-Mar-20 6:07
Calin Negru29-Mar-20 6:07 
GeneralRe: Passing an array as argument to a function Pin
Richard MacCutchan29-Mar-20 4:19
mveRichard MacCutchan29-Mar-20 4:19 
GeneralRe: Passing an array as argument to a function Pin
leon de boer29-Mar-20 4:21
leon de boer29-Mar-20 4:21 
GeneralRe: Passing an array as argument to a function Pin
Greg Utas29-Mar-20 5:53
professionalGreg Utas29-Mar-20 5:53 
GeneralRe: Passing an array as argument to a function Pin
Richard MacCutchan29-Mar-20 6:55
mveRichard MacCutchan29-Mar-20 6:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.