Please help me to Marshal this array of structs to C++ - c++

The C++ code:
typedef struct {
int bd_number; // number of boardset
int bd_copies; // how many copies
int bd_reserve; // only allocate if needed
} bd_t,*bd_p;
typedef struct boardset_info {
int bs_copies;
int bs_demand;
int bs_allocated;
int bs_ontable_avail;
int bs_ontable_needed;
pstatus bs_status;
int bs_played_sofar;
} bsi_t, *bsi_p;
FC_ERRORCODE dropin_boards(bd_p boards) {
int bs;
bs_info = (bsi_p) calloc(total_boardsets+1, sizeof(bsi_t));//total_boardsets=8
for (bs = 1; bs <= total_boardsets; bs++)
bs_info[bs].bs_status = PS_OUTPLAY;
while (boards->bd_number) { //boards-<bd_number is betweeen 1 and 8
if (boards->bd_number < 0 || boards->bd_number > total_boardsets)
{
debprint("***Error dropin_boards***\n");
debprint("boardsetnumber=%d\n",boards->bd_number);
return FC_ERR_PARAM;
}
//code does not reach this point
}
Calling code:
<StructLayout(LayoutKind.Sequential)>
Public Structure Flex_BoardSetInfo
Public SetNumber As Integer
Public Copies As Integer
Public IsReserve As Integer
End Structure
<DllImport("FlexCalc2.dll", CallingConvention:=CallingConvention.StdCall)>
Public Shared Function FlexCalcBoards(ByRef boards() As Flex_BoardSetInfo) As Flex_ErrorCode
End Function
Dim boardsets() = GetBoardSetInfo() // creates an arry of 8 BoardsetInfo Elements
_result = FlexCalcWrapper.FlexCalcBoards(boardsets)
On the last line on the debug file is logged that bd_p->board_number=517237496! The boardnumbers are initialized 1 to 8 and I can check that that has been correctly done before the code passes to the C++ dll.
How do I solve this?
EDIT:
From VB6 we used a hack to get this C++ method working:
Declare Function FlexCalcBoards Lib "FlexCalc2.dll" (firstBoard As BoardsetInfo)
ret=FlexCalcBoards(Boards(0))
So, we passed the first element of the array in stead of the array itself! (Un?)fortunately, Net does not fall for this trick...

Replace ByRef with ByVal. An array already gets marshaled as a pointer.
Using ByRef would only match a bd_t** on the C side.

Well, the answers and comments seemed to indicate that nothing should be wrong, so....
I found three things:
1. Even after rebuilding the entire solution and copying and pasting the new FlexCalc2.dll to the testproject, the old copy of the dll that resided in the Bin folders was NOT replaced.
2. I am a novice in C++, but it seems that you cannot iterate through an array using LBound to UBound when the method has only received a pointer to it. The method shown uses a nifty way to implement a kind of for each, but here it does not work because bd_p->boardnumber returns a very high number (memory address?), but bd_p[index].boardnumber returns the correct number within the range 1-8. I simply sent the length of the array as an extra parameter to the function and I was all set. (Call it the poor man's choice for all I care ;-))
3. Hans Passant was right in stating that when the method signature in C++ is theReturnValue theMethod(theStruct * theArray) that from Net you must pass the array ByVal (In VB6 this generates a syntax error and is not possible). The fact that a pointer to the array was passed is not immediately obvious since the * was already declared in the typedef of the struct.

Related

Explaining C++ (C Binding Library) Function

I'm trying to understand a Function/Method in a Library in order to port it to Java however some parameters don't make any sense to me and reading the source code the library is based on is not helping.
Function (Note the API has few comments (We can also ignore the calc handle since it's got a supplier method))
Ssr calc_ssr(CalcHandle *calc, NoteInfo *rows, size_t num_rows, float music_rate, float score_goal) {
std::vector<NoteInfo> note_info(rows, rows + num_rows);
auto skillsets = MinaSDCalc(
note_info,
music_rate,
score_goal,
reinterpret_cast<Calc*>(calc)
);
return skillset_vector_to_ssr(skillsets);
}
NoteInfo Struct
struct NoteInfo
{
unsigned int notes;
float rowTime;
};
MinaSDCalc
// Function to generate SSR rating
auto
MinaSDCalc(const std::vector<NoteInfo>& NoteInfo,
const float musicrate,
const float goal,
Calc* calc) -> std::vector<float>
{
if (NoteInfo.size() <= 1) {
return dimples_the_all_zero_output;
}
calc->ssr = true;
calc->debugmode = false;
return calc->CalcMain(NoteInfo, musicrate, min(goal, ssr_goal_cap));
}
Calc expected input file data (Only care about the #Notes: ...)
Pastebin
Question
What is NoteInfo in calc_ssr, I don't know any C or C++ so the *rows to me just seems like a pointer to a Noteinfo instance, however the MinaSDCalc methods requires an Array/Vector which using a pointer to a single instance doesn't make sense to me (pairing this with the fact that NoteInfo needs another parameter rowTime which I think is time of Note occurrence in the file which means that value must not be constant otherwise the produced result would be inaccurate)
Github Project: https://github.com/kangalioo/minacalc-standalone (The code alone may not explain enough but it's worth a try; best to look at API.h and discern what's used from there. Though I do warn you a lot of the Code is esoteric)
Sorry if this doesn't make much sense but I've been looking into this since June/July and this API is the closest abstraction from the bare C++ code I could find.
NoteInfo * rows here is pass by pointer. So, rows actually is a pointer to an instance of type NoteInfo. This is one of the ways to pass arrays in c++ to a function. Since arrays are contiguous in memory so we can just increment the pointer by one and get the next element of the array.
for example look at these three ways to do exactly one thing, parameter to pass an array to a function :-
1. void myFunction(int *param) {}
2. void myFunction(int param[10]) {}
3. void myFunction(int param[]) {}
Look into this link for more understanding : https://www.tutorialspoint.com/cplusplus/cpp_passing_arrays_to_functions.htm
Also search for pass by pointer and pass by reference to look into different ways of passing arguments in c++.
2.however the MinaSDCalc methods requires an Array/Vector which using a pointer to a single instance doesn't make sense to me: as to this question of yours, you can now see MinaSDCalc is actually getting an array and not a single instance as passing the pointer is also one of the ways of passing an array in c++.

Play with elements of Array of Structures in C++

I have just started learning c++ and I am stuck over here.
I made a structure
...
struct student{
string name;
int roll;
};
...
And then, I called it globally. student s1[20]; Inside my main function,
I did something like this
...
int count=0;
student *p;
p=s1;
cout<<"Enter the Number of records, you wish to consider.";
cin>>arraySize;
p = new student[arraySize];
while (count!=arraySize) {
cout<<"Enter name.";
cin>>*(p+count)->name; //Error comes over here
}
...
The error message, I get is Indirection requires pointer operand. Can some one Please help me out with this ??
You could either use pointer arithmetic as you are doing
(p+count)->name
or more canonically use [] to index into the array
p[count].name
The reason you are getting an error is because you are combining * which dereferences your pointer with -> which also dereferences your pointer. You could just do one or the other
(p+count)->name
(*(p+count)).name
Though as I said for readability I would prefer using index notation []
Using *(p+count)->name makes no sense.
Picking up from here, foo->bar calls method bar on the object pointed by pointer foo.
So, you should either do (p+count)->name or (*(p+count)).name.
Note the brackets, they're necessary as . has greater precedence than *.

Dynamic memory allocation initialisation in C++ class

(I'm not posting my code as this is for a project, however I have tried to get help for this issue but have had no luck)
Hi there, I am trying to initialise the size of an array of pointers (char*) which is a private member variable of my class class A
I'm using the constructor to set the size by setting an integer variable (also a member variable) which will then be used to create my array of pointers.
I have done this so far:
// Constructor - 'int value' is set to a value
private:
int value;
char ** myArray = new char*[value];
So basically I want an array of pointers in which each element can point to a string. I am passing string variables to myArray by using (char*) stringVar.c_str();
Although all of this works, I am getting some pretty weird errors when trying to store variables and have even gotten this error:
free (): invalid next size (fast)
It's weird because even when myArray is of size 4, when I try to access, say, the 3rd element, I get the same error as above.
I am very new to C++ and am very intent on solving these issues. I've had to resort to this forum for help and am looking forward to some ideas from you guys :)
if you are new C++ programmer and want work with C++ String list is better work with std::vector<std::string> for complete tutorial of how using vectors see:
http://www.cplusplus.com/reference/vector/vector/
but in you question is String list size fixed?or not?
if string list is not fixed you must malloc space for array first time in constructor and then realloc array when you want insert a string in your string list for example:
class A{
private:
char** arrayy;
int arrayysize;
A(){
arrayy = (char**)calloc(1,sizeof(char*));
arrayysize = 1;
}
insertToarrayy(char* data){
strcpy(arrayy[arrayysize-1],data);
arrayy = (char**)realloc(arrayy,arrayysize+1);
arrayysize += 1;
}
}

Pointer to a variable and pointer to an array of one element

I am writing a socket programming application. C++ and Java as two ends. My system
is in Java and applications using my system uses C++.
I receive a pointer from C++ which holds some data, it could be a variable or an array.
There is another variable nrOfData which tells how many elements the pointer contains.
I have to fill an Object of type String or String[] accordingly.
But my problem is, nrOfData==1 then there is posibility for both a varaible or array(array containing one element)
I am giving a sample program here(tried to get more similar one)
#include<iostream>
using namespace std;
void func(string **strPtr)
{
if(someCondition)
{
*strPtr = new string;
}
else
{
*strPtr = new string[1];
}
}
int main()
{
string *strPtr;
func(&strPtr);
/* I have to fill a variable and send to next level
strPtr contains one element in both cases. How could i determine whether
I need to fill String or String[]*/
}
I should be strict to the requirement.
Thanks for your help !
You can use "sizeof()" operator which is helpful in determining pointer to an array or pointer to a variable. When we pass an array name to "sizeof()", it gives the total size of array in bytes while when we pass a plain pointer it gives the address length.

C++ Why is this passed-by-reference array generating a runtime error?

void pushSynonyms (string synline, char matrizSinonimos [1024][1024]){
stringstream synstream(synline);
vector<int> synsAux;
int num;
while (synstream >> num) {synsAux.push_back(num);}
int index=0;
while (index<(synsAux.size()-1)){
int primerSinonimo=synsAux[index];
int segundoSinonimo=synsAux[++index];
matrizSinonimos[primerSinonimo][segundoSinonimo]='S';
matrizSinonimos [segundoSinonimo][primerSinonimo]='S';
}
}
and the call..
char matrizSinonimos[1024][1024];
pushSynonyms("1 7", matrizSinonimos)
It's important for me to pass matrizSinonimos by reference.
Edit: took away the & from &matrizSinonimos.
Edit: the runtime error is:
An unhandled win32 exception occurred in program.exe [2488]![alt text][1]
What's wrong with it
The code as you have it there - i can't find a bug. The only problem i spot is that if you provide no number at all, then this part will cause harm:
(synsAux.size()-1)
It will subtract one from 0u . That will wrap around, because size() returns an unsigned integer type. You will end up with a very big value, somewhere around 2^16 or 2^32. You should change the whole while condition to
while ((index+1) < synsAux.size())
You can try looking for a bug around the call side. Often it happens there is a buffer overflow or heap corruption somewhere before that, and the program crashes at a later point in the program as a result of that.
The argument and parameter stuff in it
Concerning the array and how it's passed, i think you do it alright. Although, you still pass the array by value. Maybe you already know it, but i will repeat it. You really pass a pointer to the first element of this array:
char matrizSinonimos[1024][1024];
A 2d array really is an array of arrays. The first lement of that array is an array, and a pointer to it is a pointer to an array. In that case, it is
char (*)[1024]
Even though in the parameter list you said that you accept an array of arrays, the compiler, as always, adjusts that and make it a pointer to the first element of such an array. So in reality, your function has the prototype, after the adjustments of the argument types by the compiler are done:
void pushSynonyms (string synline, char (*matrizSinonimos)[1024]);
Although often suggested, You cannot pass that array as a char**, because the called function needs the size of the inner dimension, to correctly address sub-dimensions at the right offsets. Working with a char** in the called function, and then writing something like matrizSinonimos[0][1], it will try to interpret the first sizeof(char**) characters of that array as a pointer, and will try to dereference a random memory location, then doing that a second time, if it didn't crash in between. Don't do that. It's also not relevant which size you had written in the outer dimension of that array. It rationalized away. Now, it's not really important to pass the array by reference. But if you want to, you have to change the whole thingn to
void pushSynonyms (string synline, char (&matrizSinonimos)[1024][1024]);
Passing by reference does not pass a pointer to the first element: All sizes of all dimensions are preserved, and the array object itself, rather than a value, is passed.
Arrays are passed as pointers - there's no need to do a pass-by-reference to them. If you declare your function to be:
void pushSynonyms(string synline, char matrizSinonimos[][1024]);
Your changes to the array will persist - arrays are never passed by value.
The exception is probably 0xC00000FD, or a stack overflow!
The problem is that you are creating a 1 MB array on the stack, which probably is too big.
try declaring it as:
void pushSynonyms (const string & synline, char *matrizSinonimos[1024] )
I believe that will do what you want to do. The way you have it, as others have said, creates a 1MB array on the stack. Also, changing synline from string to const string & eliminates pushing a full string copy onto the stack.
Also, I'd use some sort of class to encapsulate matrizSinonimos. Something like:
class ms
{
char m_martix[1024][1024];
public:
pushSynonyms( const string & synline );
}
then you don't have to pass it at all.
I'm at a loss for what's wrong with the code above, but if you can't get the array syntax to work, you can always do this:
void pushSynonyms (string synline, char *matrizSinonimos, int rowsize, int colsize )
{
// the code below is equivalent to
// char c = matrizSinonimos[a][b];
char c = matrizSinonimos( a*rowsize + b );
// you could also Assert( a < rowsize && b < colsize );
}
pushSynonyms( "1 7", matrizSinonimos, 1024, 1024 );
You could also replace rowsize and colsize with a #define SYNONYM_ARRAY_DIMENSION 1024 if it's known at compile time, which will make the multiplication step faster.
(edit 1) I forgot to answer your actual question. Well: after you've corrected the code to pass the array in the correct way (no incorrect indirection anymore), it seems most probable to me that you did not check you inputs correctly. You read from a stream, save it into a vector, but you never checked whether all the numbers you get there are actually in the correct range. (end edit 1)
First:
Using raw arrays may not be what you actually want. There are std::vector, or boost::array. The latter one is compile-time fixed-size array like a raw-array, but provides the C++ collection type-defs and methods, which is practical for generic (read: templatized) code.
And, using those classes there may be less confusion about type-safety, pass by reference, by value, or passing a pointer.
Second:
Arrays are passed as pointers, the pointer itself is passed by value.
Third:
You should allocate such big objects on the heap. The overhead of the heap-allocation is in such a case insignificant, and it will reduce the chance of running out of stack-space.
Fourth:
void someFunction(int array[10][10]);
really is:
(edit 2) Thanks to the comments:
void someFunction(int** array);
void someFunction(int (*array)[10]);
Hopefully I didn't screw up elsewhere....
(end edit 2)
The type-information to be a 10x10 array is lost. To get what you've probably meant, you need to write:
void someFunction(int (&array)[10][10]);
This way the compiler can check that on the caller side the array is actually a 10x10 array. You can then call the function like this:
int main() {
int array[10][10] = { 0 };
someFunction(array);
return 0;
}