473,320 Members | 1,909 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

what is (*vector) ?

Greetings c.l.c...

I am trying to understand some structure definitions in working code
(e.g. compiles, runs & produces expected results) that I have. I
think I've trimmed the code to what is relevant to my question...

Which is: What is the (*vector) in the "struct function" declaration.
What is that line doing?

My best guess is that it is a function definition that returns a struct
variable * and takes two paramters; an int (argc) and a pointer to a
struct variable (argv). I'm not quite sure what the (*vector) is ...
other than the pointer to the function itself?

Is that the name of the struct [function] member, because I see code
like fnc_start.vector = null; later in the progam... but i never see it
assigned to a "real" function address (for fnc_start and fnc_end,
anyhow) - So I'm skeptical of my understanding...

Any insight/explaination would be greatly appreciated. Thanks.

#define MAXVARNAMESIZE 32

struct variable
{
char name[ MAXVARNAMESIZE + 1 ];
int type;
char *array;
size_t array_units;
struct variable *next;
};

struct function
{
char name[ MAXVARNAMESIZE + 1 ];
int arguments;
int type;
struct variable * (*vector) ( int argc, struct variable *argv ); /*

function pointer ? */
struct function *next;
};

extern struct function fnc_start, fnc_end;

Nov 15 '05 #1
5 1917
RocTheEngy wrote:
[...]
Which is: What is the (*vector) in the "struct function" declaration.
What is that line doing?

My best guess is that it is a function definition that returns a struct
variable * and takes two paramters; an int (argc) and a pointer to a
struct variable (argv). I'm not quite sure what the (*vector) is ...
other than the pointer to the function itself? [...] struct variable * (*vector) ( int argc, struct variable *argv ); /*
function pointer ? */

[...]

Close. "vector" isn't a function, but rather a pointer to a function,
with the return type and arguments as you described.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Nov 15 '05 #2


RocTheEngy wrote On 10/05/05 16:23,:
Greetings c.l.c...

I am trying to understand some structure definitions in working code
(e.g. compiles, runs & produces expected results) that I have. I
think I've trimmed the code to what is relevant to my question...

Which is: What is the (*vector) in the "struct function" declaration.
What is that line doing?

My best guess is that it is a function definition that returns a struct
variable * and takes two paramters; an int (argc) and a pointer to a
struct variable (argv). I'm not quite sure what the (*vector) is ...
other than the pointer to the function itself?
That's right: the element named `vector' is a pointer.
It doesn't point to data the way an `int*' or `char*' does;
instead it can point to a function. If you point it at a
function of the proper type (that is, at a function that
takes an `int' and a `struct variable*' as arguments and
returns a `struct variable*' result), you can use it to
call that function without actually knowing its name:

ptr2 = fnc_start.vector(5, ptr1);

The program might point `vector' to different functions at
different times (just as a `char*' can point to different
characters at different times), so the line above might
call a different actual function each time it's executed.
Is that the name of the struct [function] member, because I see code
like fnc_start.vector = null; later in the progam... but i never see it
assigned to a "real" function address (for fnc_start and fnc_end,
anyhow) - So I'm skeptical of my understanding...
Observe that `null' is not `NULL'. Perhaps there's a
function named null() somewhere -- given the suggestive name,
I'd imagine it would be a rather short function, easily
overlooked.
Any insight/explaination would be greatly appreciated. Thanks.

#define MAXVARNAMESIZE 32

struct variable
{
char name[ MAXVARNAMESIZE + 1 ];
int type;
char *array;
size_t array_units;
struct variable *next;
};

struct function
{
char name[ MAXVARNAMESIZE + 1 ];
int arguments;
int type;
struct variable * (*vector) ( int argc, struct variable *argv ); /*

function pointer ? */
struct function *next;
};

extern struct function fnc_start, fnc_end;


It looks like the program intends to set up a linked list
of these structs, probably with fnc_start at the head and
fnc_end at the tail. Then (I'd guess) it might insert other
`struct function' instances at various spots in between them,
probably with their `vector' elements pointing at assorted
functions that might be scattered all over the place.

This sort of thing -- not necessarily with this exact
data structure -- is often found in "frameworks," where a
piece of the program controls the execution of other pieces
it might not even know about when it's compiled. If there's
a loop in the "framework" that walks along a list of these
structs and calls all the functions they point to, then a
previously unknown sub-system can "install" itself by adding
an appropriate struct to the list. The framework doesn't need
to know anything about the new sub-system; it just calls
everything that has been "registered" in the list.

--
Er*********@sun.com

Nov 15 '05 #3
hope this posts correctly (trying to use the 'correct' google
instructions found in other threads...)
Eric Sosman wrote:
RocTheEngy wrote On 10/05/05 16:23,:
Greetings c.l.c...

[snip]
My best guess is that it is a function definition that returns a struct
variable * and takes two paramters; an int (argc) and a pointer to a
struct variable (argv). I'm not quite sure what the (*vector) is ...
other than the pointer to the function itself?
That's right: the element named `vector' is a pointer.
It doesn't point to data the way an `int*' or `char*' does;
instead it can point to a function. If you point it at a
function of the proper type (that is, at a function that
takes an `int' and a `struct variable*' as arguments and
returns a `struct variable*' result), you can use it to
call that function without actually knowing its name:

ptr2 = fnc_start.vector(5, ptr1);


So "vector" also becomes the name of the member to reference... okay
that's part of my confusion. Clearer.
The program might point `vector' to different functions at
different times (just as a `char*' can point to different
characters at different times), so the line above might
call a different actual function each time it's executed.
Is that the name of the struct [function] member, because I see code
like fnc_start.vector = null; later in the progam... but i never see it
assigned to a "real" function address (for fnc_start and fnc_end,
anyhow) - So I'm skeptical of my understanding...


Observe that `null' is not `NULL'. Perhaps there's a
function named null() somewhere -- given the suggestive name,
I'd imagine it would be a rather short function, easily
overlooked.


You're right. I goofed when I searched for the varnames in the code
and incorrectly wrote "null" when I posted. Your assumption is
correct, because now that I paid more attention I see:

fnc_end.vector = fnc_null; /* in a few places */

/* fnc_null code: */
struct variable *fnc_null( int argc, struct variable *argv )
{
static struct variable nvar;
static int init = FALSE;

/* But when is this NOT FALSE? */
if ( init == FALSE )
{
init = TRUE;
var_make( &nvar, INTEGER ); }

return &nvar; /* return the address of a local var? */
/* i have to read up on 'static' i guess... */
}
/* end fnc_null code */

....

/* elsewhere in the code */
f->vector = NULL; /* an example of what I goofed up and you corrected
*/

....
[snip]

extern struct function fnc_start, fnc_end;


It looks like the program intends to set up a linked list
of these structs, probably with fnc_start at the head and
fnc_end at the tail. Then (I'd guess) it might insert other
`struct function' instances at various spots in between them,
probably with their `vector' elements pointing at assorted
functions that might be scattered all over the place.

This sort of thing -- not necessarily with this exact
data structure -- is often found in "frameworks," where a
piece of the program controls the execution of other pieces
it might not even know about when it's compiled. If there's
a loop in the "framework" that walks along a list of these
structs and calls all the functions they point to, then a
previously unknown sub-system can "install" itself by adding
an appropriate struct to the list. The framework doesn't need
to know anything about the new sub-system; it just calls
everything that has been "registered" in the list.


The code IS an from a language interpreter... and with that
explanation, I bet you're correct again. Probably, as it reads in the
"code" it: tokenizes it, formats it into its internal structs, and then
sets up this data structure to "run" everything once the entire program
is read in. "Run" would essentailly mean walk the linked list...

Wow... I've been reading this code for days (wondering what all the
extra 'overhead' was for) and you picked up on all that from a couple
struct definitions... Now I can take a different approach to
understanding what is going on. lmao... NICE!

Mucho Thanks!

Nov 15 '05 #4


RocTheEngy wrote On 10/05/05 17:31,:
[...]
Wow... I've been reading this code for days (wondering what all the
extra 'overhead' was for) and you picked up on all that from a couple
struct definitions... Now I can take a different approach to
understanding what is going on. lmao... NICE!


I'm expecting to receive the Nobel Prize for Programming
any day now.

Seriously, it's not a matter of genius but of experience.
I've been reading and writing C for almost thirty years, have
seen lots and lots of gadgetry invented by people smarter
than I am, and have learned to recognize some recurring
patterns. Read as much code as you can, learn several different
programming languages, and become proficient in at least two
or three; a broader perspective never hurts.

By the way, not all recurring patterns are good ones.
But even the Frequently Committed Errors are worth becoming
familiar with: not only will you improve your ability to spot
them when debugging somebody else's code, but you'll be more
likely to avoid them in your own.

Here's the acid test: Go back and read some code that you
yourself wrote two or three years earlier. If you can't see
how to improve it, you've ceased to learn and it's time to
trim your hair into points. ;-)

--
Er*********@sun.com

Nov 15 '05 #5
RocTheEngy wrote:
hope this posts correctly (trying to use the 'correct' google
instructions found in other threads...)
Eric Sosman wrote:
RocTheEngy wrote On 10/05/05 16:23,:
[big snip]
extern struct function fnc_start, fnc_end;


It looks like the program intends to set up a linked list
of these structs, probably with fnc_start at the head and
fnc_end at the tail. Then (I'd guess) it might insert other
`struct function' instances at various spots in between them,
probably with their `vector' elements pointing at assorted
functions that might be scattered all over the place.

This sort of thing -- not necessarily with this exact
data structure -- is often found in "frameworks," where a
piece of the program controls the execution of other pieces
it might not even know about when it's compiled. If there's
a loop in the "framework" that walks along a list of these
structs and calls all the functions they point to, then a
previously unknown sub-system can "install" itself by adding
an appropriate struct to the list. The framework doesn't need
to know anything about the new sub-system; it just calls
everything that has been "registered" in the list.


The code IS an from a language interpreter... and with that
explanation, I bet you're correct again. Probably, as it reads in the
"code" it: tokenizes it, formats it into its internal structs, and then
sets up this data structure to "run" everything once the entire program
is read in. "Run" would essentailly mean walk the linked list...


It would not be difficult to store the data structure in a file for
later retrieval and "execution". It would be a nice feature to add to
this interpreter... (I realize "something" would have to be done with
the function pointers when they are retrieved... but I think I can
handle that).

Would it be possible to write a "stub" C program and attach the
datafile created above? I'm not sure how to even research this part...
My imagination is that another utility would append the datafile
created above to the end of the "stub" program which contains all the
relevant function code, and have to 'patch' the stub somehow so it knew
the offset of where the data begins. The stub program is esentially
the 'framework' mentioned: it reads itself (from memory or disk?)
skipping over N [code] bytes until it arrived at the data structure...
Then, read in the data structure, populating a linked list, and walk
the linked list again... IIRC, flavors of BASIC, REXX and others did
something like that.

Is there a way/convention to accomplish the above in C without the
post-compile 'patching' of code in the "stub"? I know I've way over
simplified process (possibly totally off-base), so can someone direct
me to information where I can research this, or do I need to have
intimate knowledge of compilers, linkers, etc... ?

Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Steven T. Hatton | last post by:
This is something I've been looking at because it is central to a currently broken part of the KDevelop new application wizard. I'm not complaining about it being broken, It's a CVS images. ...
4
by: John | last post by:
Hi all; I write my first code using vector and list. When I run it, segmentation fault. Try to debug it, but it can not pass linking with -g option. What is the error with it? Thanks a lot....
10
by: Steven T. Hatton | last post by:
I read Stroustrup's article of the day: http://www.research.att.com/~bs/C++.html Programming with Exceptions. InformIt.com. April 2001. http://www.research.att.com/~bs/eh_brief.pdf Some of...
11
by: Richard Thompson | last post by:
I've got a memory overwrite problem, and it looks as if a vector has been moved, even though I haven't inserted or deleted any elements in it. Is this possible? In other words, are there any...
2
by: hvaisane | last post by:
Valgrind says ==11604== Invalid read of size 4 ==11604== at 0x8048ABB: main (foo.cc:36) ==11604== Address 0x1B92415C is 4 bytes inside a block of size 8 free'd ==11604== at 0x1B90514F:...
6
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a...
11
by: Micha | last post by:
Hello there, I think I've run into some classic c++ pitfall and maybe some of you guys can help me out. For my project I will need to use matrices and vectors and so I decided to implement them...
6
by: Magcialking | last post by:
the following code just won't compile, ......... vector< vector< vector<int >::iterator it1=fr.begin(),it1_end=fr.end(); for(;it1!=it1_end();it1++){ ......... the message from the compliler...
2
by: Lambda | last post by:
The code is simple: // Token.h #ifndef TOKEN_H #define TOKEN_H #include <vector> #include <string> class Token
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.