pointer argument trouble

V

voidtwerp

Am I being really stupid here - I cant see what the problem is.

how can I be getting different values in coincount and count? (note
that I dont think either is correct but I cannot be sure at this
stage).

// 3rd party dll header file
extern "C" {
APIDLL_EXPORT bool _cdecl ENGINE_GetCoinCount(uint CoinValue,ulong*
cnt);
}

// my .cpp file

void Help_ProductSale_CountCoins(uint coinvalue[],ulong coincount[])
{
ulong count;
for(int i=6;i--;)
{
ENGINE_GetCoinCount(coinvalue,&coincount);
ENGINE_GetCoinCount(coinvalue,&count);
printf("Count %d: %d (%d)\r\n",i,coincount,count);
}
}
bool Test_ProductSale()
{
uint coinvalue[]={5,10,20,50,100,200};
ulong coinstart[6];
Help_ProductSale_CountCoins(coinvalue,coinstart);
}

output:
Count 5: 0 (0)
Count 4: 0 (0)
Count 3: 0 (0)
Count 2: 235138746 (0)
Count 1: 5 (0)
Count 0: 60597997 (0)
 
E

Eric Jensen

voidtwerp said:
Am I being really stupid here - I cant see what the problem is.

how can I be getting different values in coincount and count? (note
that I dont think either is correct but I cannot be sure at this
stage).

// 3rd party dll header file
extern "C" {
APIDLL_EXPORT bool _cdecl ENGINE_GetCoinCount(uint CoinValue,ulong*
cnt);
}

// my .cpp file

void Help_ProductSale_CountCoins(uint coinvalue[],ulong coincount[])

printf("Count %d: %d (%d)\r\n",i,coincount,count);


You might wanna read the the printf documentation.
Since this is C++ use std::cout instead

std::cout << "Count " << i << ": " << coincount .... fill in rest
yourself.

For the prinf you use %d or %i is for signed decimal integer
%u is for unsigned decimal integer.. And i recall you type is
UINT (unsigned int)

//eric
 
V

Victor Bazarov

voidtwerp said:
Am I being really stupid here - I cant see what the problem is.

how can I be getting different values in coincount and count? (note
that I dont think either is correct but I cannot be sure at this
stage).

// 3rd party dll header file
extern "C" {
APIDLL_EXPORT bool _cdecl ENGINE_GetCoinCount(uint CoinValue,ulong*
cnt);


And what is the implemenatation of that function? Next time post it
because it might be relevant...

BTW, I can _guess_ what 'ulong' and 'uint' are, but you need to tell
us what it is. And, 'APIDLL_EXPORT' or '_cdecl' are also undefined
here.
}

// my .cpp file

void Help_ProductSale_CountCoins(uint coinvalue[],ulong coincount[])
{
ulong count;
for(int i=6;i--;)
{
ENGINE_GetCoinCount(coinvalue,&coincount);
ENGINE_GetCoinCount(coinvalue,&count);
printf("Count %d: %d (%d)\r\n",i,coincount,count);


If you want to print 'unsigned long' values, you need to add 'l' to the
format specification, IIRC.
}
}
bool Test_ProductSale()
{
uint coinvalue[]={5,10,20,50,100,200};
ulong coinstart[6];
Help_ProductSale_CountCoins(coinvalue,coinstart);
}

output:
Count 5: 0 (0)
Count 4: 0 (0)
Count 3: 0 (0)
Count 2: 235138746 (0)
Count 1: 5 (0)
Count 0: 60597997 (0)

V
 
V

voidtwerp

void Help_ProductSale_CountCoins(uint coinvalue[],ulong coincount[])
printf("Count %d: %d (%d)\r\n",i,coincount,count);


You might wanna read the the printf documentation.
Since this is C++ use std::cout instead

std::cout << "Count " << i << ": " << coincount .... fill in rest
yourself.


unfortunately as I am using eVC++ this is not an option - however the
result I was showing stands even if I do change the format specifiers.
 
V

voidtwerp

voidtwerp said:
Am I being really stupid here - I cant see what the problem is.
how can I be getting different values in coincount and count? (note
that I dont think either is correct but I cannot be sure at this
stage).

// 3rd party dll header file
extern "C" {
APIDLL_EXPORT bool _cdecl ENGINE_GetCoinCount(uint CoinValue,ulong*
cnt);

And what is the implemenatation of that function? Next time post it
because it might be relevant...


I dont have access to the dll code which includes implementation of the
above
BTW, I can _guess_ what 'ulong' and 'uint' are, but you need to tell
us what it is. And, 'APIDLL_EXPORT' or '_cdecl' are also undefined
here.

unsigned long, unsigned int, _declspec(dllexport) - I have a suspicion
that this is compiler specific as is _cdecl
maybe I need to take this to a compiler specific group rather than a
C++ one.
// my .cpp file
void Help_ProductSale_CountCoins(uint coinvalue[],ulong coincount[])
{
ulong count;
for(int i=6;i--;)
{
ENGINE_GetCoinCount(coinvalue,&coincount);
ENGINE_GetCoinCount(coinvalue,&count);
printf("Count %d: %d (%d)\r\n",i,coincount,count);


If you want to print 'unsigned long' values, you need to add 'l' to the
format specification, IIRC.


the output results still stand when I change the format specifiers
- Hide quoted text -
- Show quoted text -
}
}
bool Test_ProductSale()
{
uint coinvalue[]={5,10,20,50,100,200};
ulong coinstart[6];
Help_ProductSale_CountCoins(coinvalue,coinstart);
}
output:
Count 5: 0 (0)
Count 4: 0 (0)
Count 3: 0 (0)
Count 2: 235138746 (0)
Count 1: 5 (0)
Count 0: 60597997 (0)
 
V

Victor Bazarov

voidtwerp said:
voidtwerp said:
Am I being really stupid here - I cant see what the problem is.
how can I be getting different values in coincount and count?
(note that I dont think either is correct but I cannot be sure at
this stage).

// 3rd party dll header file
extern "C" {
APIDLL_EXPORT bool _cdecl ENGINE_GetCoinCount(uint CoinValue,ulong*
cnt);

And what is the implemenatation of that function? Next time post it
because it might be relevant...


I dont have access to the dll code which includes implementation of
the above


So it's part of your program but not part of Standard library and not
written by you, right? We can't help you with that.
[...]
bool Test_ProductSale()
{
uint coinvalue[]={5,10,20,50,100,200};
ulong coinstart[6];
Help_ProductSale_CountCoins(coinvalue,coinstart);
}
output:
Count 5: 0 (0)
Count 4: 0 (0)
Count 3: 0 (0)
Count 2: 235138746 (0)
Count 1: 5 (0)
Count 0: 60597997 (0)

It seems that the "ENGINE" you're using to count the coins has no
idea that coins with values of 5 or 20 exist. Could it be you're
using a setting with your "ENGINE" that only allow coins of some
specific nomination? As an example, in the USA accepted coins have
nominations of 1 cent, 5, 10, 25, 50 cents, and 1 dollar. There
is no coin with value 20 or with value 200 in the USA (IIRC).

So, RTFM for your "ENGINE". Your problem is not of the C++ nature.

V
 
J

Jim Langston

voidtwerp said:
Am I being really stupid here - I cant see what the problem is.

how can I be getting different values in coincount and count? (note
that I dont think either is correct but I cannot be sure at this
stage).

// 3rd party dll header file
extern "C" {
APIDLL_EXPORT bool _cdecl ENGINE_GetCoinCount(uint CoinValue,ulong*
cnt);
}

// my .cpp file

void Help_ProductSale_CountCoins(uint coinvalue[],ulong coincount[])
{
ulong count;
for(int i=6;i--;)
{
ENGINE_GetCoinCount(coinvalue,&coincount);


Warning: you passed coinvalue which was declared in Test_ProductSale as:
uint coinvalue[]={5,10,20,50,100,200};
this array has 6 values. They are numbered from 0 to 5. Yet you are
looking at coinvalue[6] which is an array overflow.

coincount was declared as:
ulong coinstart[6];
again, numbered 0 to 5, yet you are looking at [6], another array overflow.

I'm not sure if this is your problem, but it sure in the heck doesn't help.
Undefined behavior and all.
ENGINE_GetCoinCount(coinvalue,&count);
printf("Count %d: %d (%d)\r\n",i,coincount,count);
}
}
bool Test_ProductSale()
{
uint coinvalue[]={5,10,20,50,100,200};
ulong coinstart[6];
Help_ProductSale_CountCoins(coinvalue,coinstart);
}

output:
Count 5: 0 (0)
Count 4: 0 (0)
Count 3: 0 (0)
Count 2: 235138746 (0)
Count 1: 5 (0)
Count 0: 60597997 (0)
 
V

Victor Bazarov

Jim said:
voidtwerp said:
Am I being really stupid here - I cant see what the problem is.

how can I be getting different values in coincount and count?
(note that I dont think either is correct but I cannot be sure at
this stage).

// 3rd party dll header file
extern "C" {
APIDLL_EXPORT bool _cdecl ENGINE_GetCoinCount(uint CoinValue,ulong*
cnt);
}

// my .cpp file

void Help_ProductSale_CountCoins(uint coinvalue[],ulong coincount[])
{
ulong count;
for(int i=6;i--;)
{
ENGINE_GetCoinCount(coinvalue,&coincount);


Warning: you passed coinvalue which was declared in Test_ProductSale
as: uint coinvalue[]={5,10,20,50,100,200};
this array has 6 values. They are numbered from 0 to 5. Yet you are
looking at coinvalue[6] which is an array overflow.


No. The condition (the second expression in the 'for' statement) has
been executed once at this point, so 'i' actually contains 5. You need
to pay attention to those things...

V
 
J

Jim Langston

Victor Bazarov said:
Jim said:
voidtwerp said:
Am I being really stupid here - I cant see what the problem is.

how can I be getting different values in coincount and count?
(note that I dont think either is correct but I cannot be sure at
this stage).

// 3rd party dll header file
extern "C" {
APIDLL_EXPORT bool _cdecl ENGINE_GetCoinCount(uint CoinValue,ulong*
cnt);
}

// my .cpp file

void Help_ProductSale_CountCoins(uint coinvalue[],ulong coincount[])
{
ulong count;
for(int i=6;i--;)
{
ENGINE_GetCoinCount(coinvalue,&coincount);


Warning: you passed coinvalue which was declared in Test_ProductSale
as: uint coinvalue[]={5,10,20,50,100,200};
this array has 6 values. They are numbered from 0 to 5. Yet you are
looking at coinvalue[6] which is an array overflow.


No. The condition (the second expression in the 'for' statement) has
been executed once at this point, so 'i' actually contains 5. You need
to pay attention to those things...


Gah, you're right. I actually thought of that before I replied, but then
realized that i-- was post and would be decremented after the statement, but
I was thinking the statement block instead of the for statement itself.
That's why I hate that type of code.
 
N

Noah Roberts

Jim said:
Victor Bazarov said:
Jim said:
Am I being really stupid here - I cant see what the problem is.

how can I be getting different values in coincount and count?
(note that I dont think either is correct but I cannot be sure at
this stage).

// 3rd party dll header file
extern "C" {
APIDLL_EXPORT bool _cdecl ENGINE_GetCoinCount(uint CoinValue,ulong*
cnt);
}

// my .cpp file

void Help_ProductSale_CountCoins(uint coinvalue[],ulong coincount[])
{
ulong count;
for(int i=6;i--;)
{
ENGINE_GetCoinCount(coinvalue,&coincount);

Warning: you passed coinvalue which was declared in Test_ProductSale
as: uint coinvalue[]={5,10,20,50,100,200};
this array has 6 values. They are numbered from 0 to 5. Yet you are
looking at coinvalue[6] which is an array overflow.


No. The condition (the second expression in the 'for' statement) has
been executed once at this point, so 'i' actually contains 5. You need
to pay attention to those things...


Gah, you're right. I actually thought of that before I replied, but then
realized that i-- was post and would be decremented after the statement, but
I was thinking the statement block instead of the for statement itself.
That's why I hate that type of code.


Yes, certainly a great way to obscure your intention.
 
V

voidtwerp

Jim said:
Victor Bazarov said:
Jim Langston wrote:
ulong count;
for(int i=6;i--;)
{
ENGINE_GetCoinCount(coinvalue,&coincount); [snip]
this array has 6 values. They are numbered from 0 to 5. Yet you are
looking at coinvalue[6] which is an array overflow.
No. The condition (the second expression in the 'for' statement) has
been executed once at this point, so 'i' actually contains 5. You need
to pay attention to those things...
[snip]
That's why I hate that type of code.


Yes, certainly a great way to obscure your intention.


what would you suggest as the least obscure form of
for(int i=6;i--;)
 
A

Alf P. Steinbach

* voidtwerp:
for(int i=6;i--;)

for( int i = 5; i >= 0; --i )

This says exactly which values 'i' will have in the loop body, and
allows any starting value, as opposed to only start values >= 0.
 
V

voidtwerp

uint coinvalue[]={5,10,20,50,100,200};
It seems that the "ENGINE" you're using to count the coins has no
idea that coins with values of 5 or 20 exist. Could it be you're
using a setting with your "ENGINE" that only allow coins of some
specific nomination? As an example, in the USA accepted coins have
nominations of 1 cent, 5, 10, 25, 50 cents, and 1 dollar. There
is no coin with value 20 or with value 200 in the USA (IIRC).

but there is in europe :)
So, RTFM for your "ENGINE". Your problem is not of the C++ nature.

TFM has been thoroughly read - though I am beginning to suspect the dll
implementation.

void Help_ProductSale_CountCoins(uint coinvalue[],ulong coincount[])
{
ulong count;
for(int i=6;i--;)
{
ENGINE_GetCoinCount(coinvalue,&coincount);
ENGINE_GetCoinCount(coinvalue,&count);
printf("Count %d: %lu (%lu)\r\n",i,coincount,count);
}
}

what I am most concerned with at the moment is that the above code
snipped produces different values for coincount and count - what
could be going on?

Count 5: 0 (0)
Count 4: 0 (0)
Count 3: 0 (0)
Count 2: 5 (0)
Count 1: 182 (0)
Count 0: 200815 (0)
 
V

Victor Bazarov

voidtwerp said:
[..]
what I am most concerned with at the moment is that the above code
snipped produces different values for coincount and count - what
could be going on?

Count 5: 0 (0)
Count 4: 0 (0)
Count 3: 0 (0)
Count 2: 5 (0)
Count 1: 182 (0)
Count 0: 200815 (0)


Who the F knows? Contact the vendor of your "ENGINE".

V

P.S. This is another wild guess, but check that you initialise your
"ENGINE" correctly. See if TFM contains a "reset the counts" kind of
operation, and see if it needs to be performed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top