473,671 Members | 2,298 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.vecto r = 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 1932
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.vecto r(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.vecto r = 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.vecto r(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.vecto r = 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
3293
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. Such things happen. The whole subsystem is going through radical changes. I don't really want to say what I think of the code just yet. That would influence the opinions of others, and I really want to know how other people view these things,...
4
1734
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. John
10
2048
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 these ideas are finally beginning to sink in. I believe I looked at the same article a while back and decided I wasn't quite ready for it. If I understood things correctly, there seems to be a slight problem with the design of his exception safe...
11
2896
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 circumstances in which the STL will move a vector, or invalidate iterators to elements in the vector, if you don't insert or remove elements? My actual problem seems to be as follows: I have class X, which contains an STL vector. The constructor...
2
22793
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: operator delete(void*) (vg_replace_malloc.c:156) ==11604== by 0x804A1BA: __gnu_cxx::new_allocator<Foo>::deallocate(Foo*, unsigned) (new_allocator.h:86) ==11604== by 0x8049C08: std::_Vector_base<Foo, std::allocator<Foo> >::_M_deallocate(Foo*,...
6
2565
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 "SEGV" when run (presumably - attempt to delete deleted memory. Please take a look and see if you can notice any mistakes I'm making. Basically, I want to store classes of my objects in a vector. I also have three further questions:
11
2590
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 by myself. I know there are already tons of vector and matrix implementations, but I wanted to have one taylored for my needs and without debugging someones else code. Also is's become somewhat personal meanwhile ;-).
6
1770
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 is "36 E:\Dev-Cpp\pp.cpp no match for call to
2
13054
by: Lambda | last post by:
The code is simple: // Token.h #ifndef TOKEN_H #define TOKEN_H #include <vector> #include <string> class Token
0
8474
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8392
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8912
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8819
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8597
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8669
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6222
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5692
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2809
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.