5

I'm currently working on a project where I want to convert the entries of a CSV file into a vector of Objects. Therefore I have written a function, which converts an array of structs in a vector. The problem is that right now my function only works if the user enters the right size of the array as an additional parameter but if he enters a higher number an exception is thrown because the function is trying to read from an array entry that doesn't exist. Now I want to know if there is anyway that I can determine the size of the array of structs in my function. I have already tried sizeof(array)/sizeof(array[0]) but that doesn't work.

Here is the function I'm talking about:

BANKMANAGEMENT_API int initAccounts(ACCOUNT accArray_[], const int numOfAcc_)
{
    BankmanagementClass *myBankmanagement = BankmanagementClass::createBankmanagementClass();

    for (int i = 0; i < numOfAcc_; i++)
    {
        ACCOUNT acc = accArray_[i];
        Account* newaccount = Account::accountStruct2Obj(&acc);
        myBankmanagement->setNextAccountId(myBankmanagement->determineNextId(newaccount->getAccountId(), myBankmanagement->getNextAccountId()));
        myBankmanagement->addAccount(newaccount);
    }
    LogInfo("Account Vector was initialized with data from Csv File.");
    return 0;
}

I want to get rid of the numOfAcc_ parameter so that the user can't enter the wrong size.

It's for a dll with C interface.

  • 1
    Use `std::vector`. Why are you not using it? – Cheers and hth. - Alf Oct 19 '16 at 12:00
  • **−1** The problem is in the calling code that you haven't shown, and not in the code that you have shown. Voting to close as lacking reproducible example. – Cheers and hth. - Alf Oct 19 '16 at 12:03
  • Upvoters, please explain your upvotes. Why do you think this question would be useful to someone coming from google, say? I see nothing useful whatsoever here. – Cheers and hth. - Alf Oct 19 '16 at 12:05
  • @Cheersandhth.-Alf He is trying to make the function dummy proof. No reason to try and down vote that effort. Sure he could be more strict in the calling code but why, if you can, have a function that is a pointer and size and hope the caller does not lie to you? – NathanOliver Oct 19 '16 at 12:05
  • @NathanOliver: No, he (or she) isn't trying to make the function dummy proof. The function would be OK if it were called with correct arguments. There is a problem in the calling code, and that code's not shown and not discussed: it's a totally worthless question. – Cheers and hth. - Alf Oct 19 '16 at 12:06
  • 1
    @Cheersandhth.-Alf Well that is your opinion and you are entitled to it. I see nothing wrong with asking for help on how to change the function to not be dependent on the caller passing the correct size. – NathanOliver Oct 19 '16 at 12:08
  • @NathanOliver: It's not an opinion, it's a fact. The code isn't shown. There is no discussion of it. There is no way to make the function more bullet proof for that code, without knowing anything about the code. The not shown code will have to be modified to deal with a different function signature, and it's that change that is of interesting: a change of unknown code. It's worthless. – Cheers and hth. - Alf Oct 19 '16 at 12:09
  • @Cheersandhth.-Alf He doesn't need to show the calling code to ask how to change the function signature to not rely on the size being passed in. And with that I am done. – NathanOliver Oct 19 '16 at 12:10
  • 1
    I can't use std:vector it's a function of dll that should be C concurrent. There is no calling code, because my task is only to write the dll – Birnbacherin Oct 19 '16 at 12:12
  • @NathanOliver: That's rubbish, sorry. The function signature can't be changed without changing the calling code correspondingly. It's the calling code that somehow doesn't know the size of the array. And that code isn't show. We can't know why it doesn't know the array size, why it (allegedly) *asks the user* for the array size. – Cheers and hth. - Alf Oct 19 '16 at 12:12
  • 1
    That is also why I want to make it dummy proof, because I'm not the user. I only provide the function via my dll – Birnbacherin Oct 19 '16 at 12:13
  • @Birnbacherin: Please add that information to the question. It's crucial for any answer. Unfortunately it also means "no way" as answer: there is no way you can determine the size of a caller's raw array, and `std::vector` is not well suited in a C-compatible DLL interface. – Cheers and hth. - Alf Oct 19 '16 at 12:15
  • Ok, I will remember that next time. Sorry, it's my first post on StackOverflow – Birnbacherin Oct 19 '16 at 12:16

1 Answers1

0

There is no way to determinate the size of the array. If you have under control how accArray is filled you could use an end marker and check this condition in the for loop.

Peter VARGA
  • 4,780
  • 3
  • 39
  • 75