Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 112

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 112
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3368: Cannot modify header information - headers already sent by (output started at /includes/bbcode.php:112)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3370: Cannot modify header information - headers already sent by (output started at /includes/bbcode.php:112)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3371: Cannot modify header information - headers already sent by (output started at /includes/bbcode.php:112)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3372: Cannot modify header information - headers already sent by (output started at /includes/bbcode.php:112)
OrbWorks Community Forum • View topic - Need help with this code...

Need help with this code...

Discuss PocketC for CE (including Desktop Edition)

Postby Glasswalker on Fri Jun 13, 2003 1:27 am

This code is doing something wierd...
When I go build, run... It brings up hourglass for a moment... the does nothing...
once I have done that once, the build and run options on the build menu are greyed out. And no matter what I do, they stay greyed out, even if I open a known good source file, or create a new file... The only way to get them back is to close program and restart...

The below code is the only code that does this... all other programs I have work fine...

I would really appreciate any help anyone can provide... Here is the code:

#define NN_SIZE = 30;
#define NN_HIDDENLAYERS = 2;
#define NN_LAYERS = NN_HIDDENLAYERS + 2;
#define NUM_BUGS = 10;

struct synapse_s
{
int weight;
int fired_short;
int fired_long;
};

struct neuron_s
{
int value;
struct synapse_s synapse[NN_SIZE];
};

struct nn_s
{
struct neuron_s neuron[NN_SIZE];
};

struct brain_s
{
struct nn_s nn_layer[NN_LAYERS];
};

struct smell_s
{
int food;
int bug;
int toxin;
};

struct memory_s
{
int food;
int bug;
int toxin;
int hazard;
int home;
};
};

struct bug_s
{
struct brain_s brain;
int fitness;
int generation;
string name;
int x;
int y;
int facing;
int food;
int water;
int seenfriend;
int social;
int toxin;
int energy;
int health;
int vision;
struct smell_s smell;
struct memory_s memory;
};

struct bug_s bug[NUM_BUGS];

main()
{
alert("Test");
alert(NN_HIDDENLAYERS);
alert(NN_LAYERS);
}
Glasswalker
 
Posts: 47
Joined: Wed Mar 21, 2001 12:18 am
Location: Canada

Postby Mickle on Fri Jun 13, 2003 3:08 am

Your code contains syntax errors. PocketC contains errors.
You have result of combination of these errors.
1.
Code: Select all
struct memory_s
{
int food;
int bug;
int toxin;
int hazard;
int home;
};
}; // error

2. Code bits
#define NN_HIDDENLAYERS = 2;
#define NN_LAYERS = NN_HIDDENLAYERS + 2;
alert(NN_HIDDENLAYERS);
alert(NN_LAYERS);
is same as:
alert(= 2;);
alert(= = 2+ 2;);
.... errors
.
3. PocketC compiler cannot work around many syntax errors / combinations
of syntax errors (for example PocketC can report about ERROR1 and ERROR2
but will crash if your code contain some combination of ERROR1 and ERROR2)
.
4. Do not use global struct variables.
struct bug_s bug[NUM_BUGS];
PocketC cannot work with global structs. Some programs with global structs
will work, but in the majority of cases, you will have strange runtime mistakes
and / or your program will do what you do not wait.
You can use local structs.
Mickle
 
Posts: 48
Joined: Wed Apr 17, 2002 1:22 am
Location: Russia

Postby Glasswalker on Fri Jun 13, 2003 11:35 pm

Thanks for the help... I didn't even notice those things... So I have fixed it up as per your instructions... The problem is, when I run the program now I get an error by pocketc... the error is:

Problem @ file Bugz.pc line 16
-------------
Keyword [struct] declare error, ex: struct person {string name; int age};

Also, I have a problem if I can't use public structs... I need to have many functions that deal with the whole struct. If I can't use public structs, how do I get that data (and return the data) to and from a function. I have a main array of bugs, and individual functions need to work with various bis of the struct, but normally need large portions of the struct (specifically the Neural Net functions) Now I know in regular C or C++ I can declare a function with one of it's parameters being of type struct, and then pass in the whole struct. But the other problem, is due to memory constraints, I need to work with that specific instance of the variable, if I create a new instance and pass the struct back then I am creating a copy of the struct in ram... And the other question is: can you even do that in pocketc?

So basically how would you write this so that my functions and my main function can all read and write the same instance of the struct. I wish I could explain it more to make it clearer, but I don'y know how... Anywhoo, that, and I need to know how to correct that error...

Here is the current versio of the code:

#define NN_SIZE 30;
#define NN_HIDDENLAYERS 2;
#define NN_LAYERS NN_HIDDENLAYERS + 2;
#define NUM_BUGS 10;

struct synapse_s
{
int weight;
int fired_short;
int fired_long;
};

struct neuron_s
{
int value;
struct synapse_s synapse[NN_SIZE];
};

struct nn_s
{
struct neuron_s neuron[NN_SIZE];
};

struct brain_s
{
struct nn_s nn_layer[NN_LAYERS];
};

struct smell_s
{
int food;
int bug;
int toxin;
};

struct memory_s
{
int food;
int bug;
int toxin;
int hazard;
int home;
};

struct bug_s
{
struct brain_s brain;
int fitness;
int generation;
string name;
int x;
int y;
int facing;
int food;
int water;
int seenfriend;
int social;
int toxin;
int energy;
int health;
int vision;
struct smell_s smell;
struct memory_s memory;
};

struct bug_s bug[NUM_BUGS];

main()
{

}
Glasswalker
 
Posts: 47
Joined: Wed Mar 21, 2001 12:18 am
Location: Canada

Postby Mickle on Sat Jun 14, 2003 1:05 pm

"... the error is: Problem @ file Bugz.pc line 16"
Code bits
Code: Select all
#define NN_SIZE 30;
struct neuron_s {
    int value;
    struct synapse_s synapse[NN_SIZE];
};

is same as
Code: Select all
struct neuron_s {
    int value;
    struct synapse_s synapse[30;]; // syntax error (semicolon after "30")
};

.
After you delete all semicolons in #define you will have error:
Code: Select all
struct brain_s {
    struct nn_s nn_layer[NN_LAYERS];
};

because it is same as
Code: Select all
struct brain_s {
    struct nn_s nn_layer[2 + 2]; // syntax error
};

You can use constants but not constant expressions in array declaration.
PocketC preprocessor not calculate constant expressions. It is bad but it is fact.
In some cases for speed you should use:
#define NN_HIDDENLAYERS 2
#define NN_LAYERS 4 // NN_HIDDENLAYERS + 2
instead
#define NN_HIDDENLAYERS 2
#define NN_LAYERS NN_HIDDENLAYERS + 2
For example:
Code: Select all
for (i = 0; i < 100000; i++)
{
    x = NN_LAYERS + something; // 2 + 2 + something or 4 + something
}

.
In addition in most common cases you should use parenthesis:
#define NN_LAYERS (NN_HIDDENLAYERS + 2)
instead
#define NN_LAYERS NN_HIDDENLAYERS + 2
because you will have problem:
x = NN_LAYERS * something; // what you want? 2 + 2 * something or (2 + 2) * something ?
Anyway, you need to read any C/C++ book about C-preprocessor usage...
.
Other problem:
Code below not will work:
Code: Select all
main()
{
    struct bug_s bug[NUM_BUGS];

    bug[2].memory.bug = 5; // problem with names ("bug" is name of array and name of field)
    alert(bug[2].memory.bug);
}

.
"...If I can't use public structs, how do I get that data..."
structs usage in PocketC is completely different with C and completely undocumented.
In addition PocketC contains lot of bugs. I can't do overview all problems/solutions in this post.
You can emulate struct (in most common cases it will better solution):
Code: Select all
#define  SYNAPSE pointer
// offsets
#define  SYNAPSE_WEIGHT    0
#define  SYNAPSE_FIREDSHORT    1
#define  SYNAPSE_FIREDLONG    2
// PocketC "size"
#define  SYNAPSESIZE        3

Synapse_New(int weight, int fired_short, int fired_long)
{
    SYNAPSE p;

    if (p = malloc(SYNAPSESIZE))
    {
        p[SYNAPSE_WEIGHT]        = weight;
        p[SYNAPSE_FIREDSHORT] = fired_short;
        p[SYNAPSE_FIREDLONG]    = fired_long;
    }
    return p;
}
Synapse_Delete(SYNAPSE p)
{
    if (p) free(p);
}

.
"...If I can't use public structs, how do I get that data..."
Very simple. I'm cut some code from my program. You not needed to understand
what it do but it show struct usage:
Code: Select all
struct _dibinfoheader {
// it is not important
};
#define DIBINFOHEADER struct _dibinfoheader // for short code

DibHdr_ColorEntries(DIBINFOHEADER bmih)
{
    if (bmih.ClrUsed) return (bmih.ClrUsed);
    if (bmih.Compression == 3)
    {
        if (bmih.BitCount == 1)      return (2); // OS/2 2.x BMP header
        if (bmih.Size < BMP_WIN4HDR) return (3);
    }
    if (bmih.BitCount <= 8) return (1 << bmih.BitCount);
    return (0);
}

DibHdr_BitmapinfoSize(DIBINFOHEADER bmih)
{
    if (bmih.Size == BMP_WIN2HDR) return (3 * DibHdr_ColorEntries(bmih) + bmih.Size);
    return (4 * DibHdr_ColorEntries(bmih) + bmih.Size);
}

main()
{
    DIBINFOHEADER bmih; int size;
// more.....
    Dib_Load(bmih, /*more...*/);
// more.....
    size = DibHdr_BitmapinfoSize(bmih);
// more.....
}

.
"...But the other problem, is due to memory constraints, I
need to work with that specific instance of the variable, if I create a new instance and pass the
struct back then I am creating a copy of the struct in ram... And the other question is: can you
even do that in pocketc?"
If my understanding is correct (sorry, my English is bad)...
You not needed to return copy of passed struct (and cannot do this).
You can modify passed struct.
someFunction(struct _something MyStruct)
{
// it will modify passed struct. You not needed return MyStruct and cannot do it.
MyStruct.field = 0;
}
In addition really it not create new copy of struct (in PocketC) it mean same as
someFunction(struct _something *MyStruct)
{
MyStruct->field = 0;
}
in C.
If you need to create copy of struct and modify it copy:
Code: Select all
someFunction(struct _something MyStruct1)
{
    struct _something MyStruct2;

    memcpy(&MyStruct2, &MyStruct1, SOMETHING_SIZE);
}

.
If you need in global object then you can use few solutions:
1. Change your project so that you not will need in global object.
It is always possible.
.
2. Simple method (but bad) to do (1):
Code: Select all
main()
{
    struct bug_s bug;
   
    Function1(bug, /*other arguments*/);
}

Function1(struct bug_s bug, /*other arguments*/)
{
......
    Function2(bug, /*other arguments*/);
......
}

and so on.
But it is not solution if you use array of structs:
Code: Select all
main()
{
    struct bug_s bug[NUM_BUGS];
}

You cannot pass it array into any other function!
struct bug_s bug[NUM_BUGS] - bug is pointer to instance of struct bug_s! You can write:
someFunction(pointer bug)
{
// but you must calculate offsets manually
// You can't use p[2].memory.bug = ...
}
.
3. Use global arrays and emulate structs array:
int Synapse_Weight[NUM_OF_SYNAPSES];
int Synapse_FiredShort[NUM_OF_SYNAPSES];
int Synapse_FiredLong[NUM_OF_SYNAPSES];
instead
Code: Select all
struct synapse_s {
    int weight;
    int fired_short;
    int fired_long;
};
struct synapse_s synapes[NUM_OF_SYNAPSES];

.
4. Use global pointer and emulate struct (and array of structs) with dynamic
allocated memory:
Code: Select all
pointer bug; // struct bug_s bug[NUM_BUGS];

BugArray_New() // ;)
{
pointer p;
...............
return p;
}

main()
{
    bug = Bug_New();
}

Probably multi-dimensional arrays library by Wolfgang will useful for it.
Mickle
 
Posts: 48
Joined: Wed Apr 17, 2002 1:22 am
Location: Russia


Return to PocketC for CE

Who is online

Users browsing this forum: No registered users and 2 guests

cron