Trying to convert a macro from C++ to C# What does this do?

  • Thread starter Thread starter Garry Freemyer
  • Start date Start date
G

Garry Freemyer

I'm trying to convert this macro to a c# function but I have a big problem.
It's on the LEFT side of an assignment statement and I am extremely
flustered over this one because I'm a little rusty and have been struggling
over this for days, not wanting to show all my ignorance in final desparate
plea for help which this is ...

Here is the macro ...

#define X(t,b) (sp->x[(t)*sp->beecount+(b)])

sp is a pointer to a structure like this

typedef struct {
int beecount; // beecount is set to 144
int *x // is set to a random pixel X location.
} swarmstruct

x is allocated memory thusly ...

sp->x = (short *) malloc(sizeof(short) * sp->beecount * TIMES); // TIMES is
a constant = 4

Later, X is called like this ..
for (b = 0; b < sp->beecount; b++) {
X(0,b) = rand() % width; // width is the screen width.
X(1, b) = X(0, b);
}

What confuses me is that when I try to translate this I get the following
for the first call ...

sp->x[(0)*sp->beecount+(0)]

I am confused by this. If I take this * operator as meaning the indirection
operator than what does the (0) I get what appears to make no sense to me
.... x[(0)MemLoc(beecount)+0] = a random pixal x location and am trying to
assign a number to an address and treating an address as an indexor?

If I take the * to mean multiply than I am doing x[0 * sp->beecount + b] =
random pixal x location but that is so redundant because it just comes down
to x = something.

I am equally confused by the second call which to me looks like it should
create an overflow error.

I do not understand what is happening here. There is no real meaningful
documentation and the author allows the inclusion of the source code as long
as I mention his name in the ending program.

I would have put this in the C++ forum here but there ISN'T one, and I'm
trying to convert it to c# using arrays instead of pointers and I'm stuck
bigtime on this point.

Anyone have any hints as to how I should convert or interprety this macro?
I've been trying to interpret it by simply doing the pre-processing inlining
by hand since c# has no macro ability but since the macros use pointers and
I'm trying to convert to array logic, I haven't a hope of interpreting this
until I understand what X is supposed to do.

Thanks for any help. /hangs my head in shame of his ignorance.
 
I can't seem to get any replies via email. I get the following. This is a
test to see if my modified email address shows up. I wish I knew how to stop
getting this bouncing as below.

Unknown host: (e-mail address removed)
 
If I understand this correctly, your second understand would be the correct
interpretation.

#define X(t, b) (sp->x[(t) * sp->beecount + (b)])

I don't think the "sp" is a pointer to a pointer! So, you should be looking
at the product of "t" and "sp->beecount", later adding "b".

James
 
The macro is attempting to simulate a 2 dimentional array: X(a,b)
roughly equals x[a]. However, that would require at least one dimension
being fixed. Of course, in this design, one dimension *is* fixed, but I
guess at some point in the design phase TIMES was a variable. In other
words, the C code (and this is C code, not C++) could have been written as:

// type INT_TIMES is an array of 4 integers.
typedef int INT_TIMES[TIMES];
typedef struct {
int beecount:
INT_TIMES* x; // x points to an array of INT_TIMES objects.
}swarmstruct;

sp->beecount = 144;
sp->x = malloc(sp->beecount * sizeof(INT_TIMES));

for (b = 0; b < sp->beecount; b++)
{
// note I had to reverse the indexes here.
sp->x[0] = rand() % width;
sp->x[1] = sp->x[0];
}


Now that we (hopefully) understand what's going on, it should be easy to
translate that into C#:

const int TIMES = 4;
struct swarmstruct
{
int beecount;
int[,] x;
}

sp.beecount = 144;
sp.x = new int[TIMES, sp.beecount]
for (b = 0; b < sp.beecount; b++)
{
// Note, the indexes are back in their original order.
sp.x[0] = rand() % width;
sp.x[1] = sp->x[0];
}



--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

Garry Freemyer said:
I'm trying to convert this macro to a c# function but I have a big problem.
It's on the LEFT side of an assignment statement and I am extremely
flustered over this one because I'm a little rusty and have been struggling
over this for days, not wanting to show all my ignorance in final desparate
plea for help which this is ...

Here is the macro ...

#define X(t,b) (sp->x[(t)*sp->beecount+(b)])

sp is a pointer to a structure like this

typedef struct {
int beecount; // beecount is set to 144
int *x // is set to a random pixel X location.
} swarmstruct

x is allocated memory thusly ...

sp->x = (short *) malloc(sizeof(short) * sp->beecount * TIMES); // TIMES is
a constant = 4

Later, X is called like this ..
for (b = 0; b < sp->beecount; b++) {
X(0,b) = rand() % width; // width is the screen width.
X(1, b) = X(0, b);
}

What confuses me is that when I try to translate this I get the following
for the first call ...

sp->x[(0)*sp->beecount+(0)]

I am confused by this. If I take this * operator as meaning the indirection
operator than what does the (0) I get what appears to make no sense to me
... x[(0)MemLoc(beecount)+0] = a random pixal x location and am trying to
assign a number to an address and treating an address as an indexor?

If I take the * to mean multiply than I am doing x[0 * sp->beecount + b] =
random pixal x location but that is so redundant because it just comes down
to x = something.

I am equally confused by the second call which to me looks like it should
create an overflow error.

I do not understand what is happening here. There is no real meaningful
documentation and the author allows the inclusion of the source code as long
as I mention his name in the ending program.

I would have put this in the C++ forum here but there ISN'T one, and I'm
trying to convert it to c# using arrays instead of pointers and I'm stuck
bigtime on this point.

Anyone have any hints as to how I should convert or interprety this macro?
I've been trying to interpret it by simply doing the pre-processing inlining
by hand since c# has no macro ability but since the macros use pointers and
I'm trying to convert to array logic, I haven't a hope of interpreting this
until I understand what X is supposed to do.

Thanks for any help. /hangs my head in shame of his ignorance.
 
Thanks!, That is exactly what I did. I made x a two dimentional array. What
blew me away was when I finished the program and hit run, I expected the
computer to blow chunks, due to all the guessing I had to do, and the
confusion, caused by my having lost the progrmmers reference for Zortech and
the HELP file too, so I had to guess what the program was doing, but when I
finished the screensaver, it worked perfectly. I haven't had that happen in
about 15 years, to have such a huge program, run perfectly the first time.
I've yet to get some details ironed out, but I'll probably be posting it as a
sample.

James Curran said:
The macro is attempting to simulate a 2 dimentional array: X(a,b)
roughly equals x[a]. However, that would require at least one dimension
being fixed. Of course, in this design, one dimension *is* fixed, but I
guess at some point in the design phase TIMES was a variable. In other
words, the C code (and this is C code, not C++) could have been written as:

// type INT_TIMES is an array of 4 integers.
typedef int INT_TIMES[TIMES];
typedef struct {
int beecount:
INT_TIMES* x; // x points to an array of INT_TIMES objects.
}swarmstruct;

sp->beecount = 144;
sp->x = malloc(sp->beecount * sizeof(INT_TIMES));

for (b = 0; b < sp->beecount; b++)
{
// note I had to reverse the indexes here.
sp->x[0] = rand() % width;
sp->x[1] = sp->x[0];
}


Now that we (hopefully) understand what's going on, it should be easy to
translate that into C#:

const int TIMES = 4;
struct swarmstruct
{
int beecount;
int[,] x;
}

sp.beecount = 144;
sp.x = new int[TIMES, sp.beecount]
for (b = 0; b < sp.beecount; b++)
{
// Note, the indexes are back in their original order.
sp.x[0] = rand() % width;
sp.x[1] = sp->x[0];
}



--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

Garry Freemyer said:
I'm trying to convert this macro to a c# function but I have a big problem.
It's on the LEFT side of an assignment statement and I am extremely
flustered over this one because I'm a little rusty and have been struggling
over this for days, not wanting to show all my ignorance in final desparate
plea for help which this is ...

Here is the macro ...

#define X(t,b) (sp->x[(t)*sp->beecount+(b)])

sp is a pointer to a structure like this

typedef struct {
int beecount; // beecount is set to 144
int *x // is set to a random pixel X location.
} swarmstruct

x is allocated memory thusly ...

sp->x = (short *) malloc(sizeof(short) * sp->beecount * TIMES); // TIMES is
a constant = 4

Later, X is called like this ..
for (b = 0; b < sp->beecount; b++) {
X(0,b) = rand() % width; // width is the screen width.
X(1, b) = X(0, b);
}

What confuses me is that when I try to translate this I get the following
for the first call ...

sp->x[(0)*sp->beecount+(0)]

I am confused by this. If I take this * operator as meaning the indirection
operator than what does the (0) I get what appears to make no sense to me
... x[(0)MemLoc(beecount)+0] = a random pixal x location and am trying to
assign a number to an address and treating an address as an indexor?

If I take the * to mean multiply than I am doing x[0 * sp->beecount + b] =
random pixal x location but that is so redundant because it just comes down
to x = something.

I am equally confused by the second call which to me looks like it should
create an overflow error.

I do not understand what is happening here. There is no real meaningful
documentation and the author allows the inclusion of the source code as long
as I mention his name in the ending program.

I would have put this in the C++ forum here but there ISN'T one, and I'm
trying to convert it to c# using arrays instead of pointers and I'm stuck
bigtime on this point.

Anyone have any hints as to how I should convert or interprety this macro?
I've been trying to interpret it by simply doing the pre-processing inlining
by hand since c# has no macro ability but since the macros use pointers and
I'm trying to convert to array logic, I haven't a hope of interpreting this
until I understand what X is supposed to do.

Thanks for any help. /hangs my head in shame of his ignorance.

 
Back
Top