473,785 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Do you think this code will produce any undefined behavior?

I am working on a high-speed futures implementation in C++, and was
tinkering around with ways to wrap the function pointer logic. I am a
C/Assembly programmer that doesn't make a lot of use of C++. So my skills
can be improved upon for sure. I was wondering if I was subjecting my first
attempt at a function pointer to any undefined behavior(s). Here is the
code:
http://appcore.home.comcast.net/vzoo...cope-guard.cpp
I'm basically using Andrei and Petru scopeguard technique, with one small
tweak: I am using a function pointer to the templated call details, instead
of a boolean. That way I can use it like a scopeguard by testing the value
of the function pointer against NULL, and I can call it like a virtual
function of sorts. So, I can call it virtually, or locally through the
templated SafeWxecute function... Does this sound Kosher to you C++ gurus'?

;^)

Thank you all for your time,

Chris Thomasson
Nov 17 '06 #1
10 1360
* Chris Thomasson:
I am working on a high-speed futures implementation in C++, and was
tinkering around with ways to wrap the function pointer logic. I am a
C/Assembly programmer that doesn't make a lot of use of C++. So my skills
can be improved upon for sure. I was wondering if I was subjecting my first
attempt at a function pointer to any undefined behavior(s). Here is the
code:
http://appcore.home.comcast.net/vzoo...cope-guard.cpp
I'm basically using Andrei and Petru scopeguard technique, with one small
tweak: I am using a function pointer to the templated call details, instead
of a boolean. That way I can use it like a scopeguard by testing the value
of the function pointer against NULL, and I can call it like a virtual
function of sorts. So, I can call it virtually, or locally through the
templated SafeWxecute function... Does this sound Kosher to you C++ gurus'?

;^)
No. A scopeguard's job is to clean up. Executing the clean-up action
more than once is bound to lead to problems. If you want a "delegate",
a callable entity that wraps an object pointer plus member function
pointer, consider the standard library and Boost/TR1 classes for that.
The Boost classes are superior because they build on the experience with
the standard library, plus additional techniques developed after 1998.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 17 '06 #2
"Alf P. Steinbach" <al***@start.no wrote in message
news:4s******** ****@mid.indivi dual.net...
>* Chris Thomasson:
>I am working on a high-speed futures implementation in C++,
[...]
>I was wondering if I was subjecting my first attempt at a function
pointer to any undefined behavior(s). Here is the code:

http://appcore.home.comcast.net/vzoo...cope-guard.cpp

I'm basically using Andrei and Petru scopeguard technique, with one small
tweak:
[...]
>So, I can call it virtually, or locally through the templated SafeWxecute
function... Does this sound Kosher to you C++ gurus'?

;^)

No. A scopeguard's job is to clean up.
Okay... I think the name I used for the object didn't safely match its
functionality. Therefore, I will rename the entire object to something like:
'funcall_base' which will not call anything in the destructor; it won't use
its scope in any aspect of its functionality.. .

Executing the clean-up action more than once is bound to lead to problems.
Then I can add a variant called 'funcall_scoped ' which will indeed invoke
the call in its destructor, only if it was not previously "Dismissed" . That
should define my indented usage a bit clearer than before...

If you want a "delegate", a callable entity that wraps an object pointer
plus member function pointer,
That's kind of what I am after, except I kind of want to stay away from
virtual destructors/functions in 'funcall_base' because it ultimately has to
be a POD that is compatible with my low-level C/Assembly library, which
contains the actual implementation details of my new futures algorithm. It
should have a single member which is a function pointer of a type that is
"standard" to my low-level library:

class funcall_base {
typedef void (*fpfuturesapi_ t) (void const*);
fpfuturesapi_t m_fpcall_virtua l;
};
So that the library can call it directly to get the actual templated
implementation details to invoke itself. Also, I think I can skip calls
into the function pointer contained in 'funcall_base':

class funcall_base {
public:
void execute_virtual () {
m_fpcall_virtua l(this);
}
};
when the type of its templated call details are known by the caller in
advance:

class funcall_base {
public:
template<typena me T_calldetail>
void execute_proxy() {
static_cast<T_c alldetail*>(thi s)->execute_direct ();
}
};

// assume mycall is base class of calldetails_t
funcall_base mycall;
// I think that this call:

mycall.execute_ virtual();
// should have more overhead than this call:

mycall.execute_ proxy<calldetai ls_t>();
// Am I way off base here?

:^)

I would appreciate it if you could clarify a bit...

consider the standard library and Boost/TR1 classes for that. The Boost
classes are superior because they build on the experience with the
standard library, plus additional techniques developed after 1998.
Unfortunately, I can't make use of any part of Boost, or any other
third-party library, in the implementation details of my C++ futures
library.

;^(...

Thanks for your patience!
Nov 17 '06 #3

Chris Thomasson wrote:
Unfortunately, I can't make use of any part of Boost, or any other
third-party library, in the implementation details of my C++ futures
library.

;^(...

Why not? You know that boost is open source but NOT GPL, right? It is
more like BSD license - use it in open source or commercial, whatever
you want.

Tony

Nov 17 '06 #4
VJ
go**********@gm ail.com wrote:
Chris Thomasson wrote:

>>Unfortunately , I can't make use of any part of Boost, or any other
third-party library, in the implementation details of my C++ futures
library.

;^(...



Why not? You know that boost is open source but NOT GPL, right? It is
more like BSD license - use it in open source or commercial, whatever
you want.
Boost is not suitable for some projects, because it is big library.
Nov 17 '06 #5
* VJ:
go**********@gm ail.com wrote:
>Chris Thomasson wrote:
>>Unfortunately , I can't make use of any part of Boost, or any other
third-party library, in the implementation details of my C++ futures
library.

Why not? You know that boost is open source but NOT GPL, right? It is
more like BSD license - use it in open source or commercial, whatever
you want.
Boost is not suitable for some projects, because it is big library.
That is a misconception.

Boost is essentially a library of smaller independent libraries, most of
which are in turn collections of largely independent header files and
nothing more.

It follows the general C++ philosophy of not paying (in size etc.) for
what you don't use.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 17 '06 #6
VJ
Alf P. Steinbach wrote:
* VJ:
>go**********@gm ail.com wrote:
>>>
Why not? You know that boost is open source but NOT GPL, right? It is
more like BSD license - use it in open source or commercial, whatever
you want.
Boost is not suitable for some projects, because it is big library.


That is a misconception.

Boost is essentially a library of smaller independent libraries, most of
which are in turn collections of largely independent header files and
nothing more.
Thats correct, but there are other libraries, which are smaller in size,
but do not have full boost functionality.
>
It follows the general C++ philosophy of not paying (in size etc.) for
what you don't use.
So, when you compile it how big it is?

This is not true if you are dynamicaly linking boost library
Nov 17 '06 #7
* VJ:
Alf P. Steinbach wrote:
>* VJ:
>>go**********@gm ail.com wrote:
Why not? You know that boost is open source but NOT GPL, right? It is
more like BSD license - use it in open source or commercial, whatever
you want.

Boost is not suitable for some projects, because it is big library.


That is a misconception.

Boost is essentially a library of smaller independent libraries, most
of which are in turn collections of largely independent header files
and nothing more.

Thats correct, but there are other libraries, which are smaller in size,
but do not have full boost functionality.
>>
It follows the general C++ philosophy of not paying (in size etc.) for
what you don't use.

So, when you compile it how big it is?
There's no single boost library. If you use a part such as
boost::shared_p tr there isn't anything to link in: it's all in the
header files. It's not meaningful to ask for the compiled size of the
full library: first of all that size can't be accurately defined since
much of the code is in header files, and secondly because the parts that
are compiled may not necessarily be used by you and incur no size overhead.

This is not true if you are dynamicaly linking boost library
?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 17 '06 #8
VJ
Alf P. Steinbach wrote:
* VJ:
>Alf P. Steinbach wrote:
>>* VJ:

go**********@gm ail.com wrote:

>
Why not? You know that boost is open source but NOT GPL, right?
It is
more like BSD license - use it in open source or commercial, whatever
you want.
>
Boost is not suitable for some projects, because it is big library.

That is a misconception.

Boost is essentially a library of smaller independent libraries, most
of which are in turn collections of largely independent header files
and nothing more.


Thats correct, but there are other libraries, which are smaller in
size, but do not have full boost functionality.
>>>
It follows the general C++ philosophy of not paying (in size etc.)
for what you don't use.

So, when you compile it how big it is?


There's no single boost library. If you use a part such as
boost::shared_p tr there isn't anything to link in: it's all in the
header files. It's not meaningful to ask for the compiled size of the
I know the original concept was to have everything in header files. But
they abandoned it
full library: first of all that size can't be accurately defined since
much of the code is in header files, and secondly because the parts that
are compiled may not necessarily be used by you and incur no size overhead.
Therefore the conclusion is: it depends what you use.

Does it means that the boost library will not boost much the size of
your program?

For our project, that was the reason not to use this library
Nov 17 '06 #9
<go**********@g mail.comwrote in message
news:11******** **************@ e3g2000cwe.goog legroups.com...
Chris Thomasson wrote:
>Unfortunatel y, I can't make use of any part of Boost, or any other
third-party library, in the implementation details of my C++ futures
library.
[...]
Why not?
[...]
I am creating the C++ API in order to wrap a low-level C API that abstracts
an external futures library written in Assembly Language. Therefore, the
"function-call base object" needs to be a POD which I am currently declaring
like this:
-----------

// declare
struct funcall_base_po d {
typedef void (*fpfuturesapi_ t) (void const*);
fpfuturesapi_t m_fpcall_virtua l;
void execute_virtual ();
template<typena me T_calldetailvoi d execute_proxy() ;
};


and defining like this:
-----------

// define
void funcall_base_po d::execute_virt ual() {
m_fpcall_virtua l(this);
}

template<typena me T_calldetail>
void funcall_base_po d::execute_prox y() {

/* T_calldetail MUST directly derive from this 'funcall_base_p od'
* T_calldetail interface MUST have execute_direct( )
*/

static_cast<T_c alldetail*>(thi s)->execute_direct ();
}


and asserting its size it equal to that of a pointer to void like this:
-----------

// assertion
namespace {
namespace assertion {
template<bool Tstruct assert_static;
template<struct assert_static<t rue{
};

assert_static<( sizeof(funcall_ base_pod) == sizeof(void*))>
assert_funcall_ base_pod_sizeof _voidptr;

}} // namespace anonymous::asse rtion


I don't think Boost will work to well with the requirements of my low-level
Assembly Language library... Anyway, I really wondering if the member
functions of 'funcall_base_p od' will subject the caller to any undefined
behavior(s)...
Nov 17 '06 #10

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

Similar topics

289
1805
by: napi | last post by:
I think you would agree with me that a C compiler that directly produces Java Byte Code to be run on any JVM is something that is missing to software programmers so far. With such a tool one could stay with C and still be able to produce Java byte code for platform independent apps. Also, old programs (with some tweaking) could be re-compiled and ported to the JVM. We have been developing such a tool over the last 2 years and currently...
18
1512
by: joshc | last post by:
I've got two bits of code that I would like some more experienced folks to check for conformance to the Standard. I've tried my best to read the standard and search around and I think and hope this code contains no cause for concern. /* taking absolute value of signed integer */ int32 val; uint32 abs_val; val = -492;
9
1981
by: ais523 | last post by:
I have some code, which I know will not work (I haven't included <math.h>, so sqrt will be assumed to return an int). What I want to know is, is an ANSI-compliant implementation required to produce an executable as output (even if the actual output is undefined)? #include <stdio.h> /*#include <math.h>*/ int main() {
67
3808
by: neilcancer | last post by:
i come from china,and i'm sorry that my english is very poor. now i'm studing data structure and i met some problem about c language. could you tell me what will happen after i use free()? i mean once i use free() on a pointer,what will the pointer points to ? for example: #include<stdio.h>
16
2032
by: Mr. Ken | last post by:
Left shift by negative numbers, will I get 1/2? Thanks.
232
13349
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first set of examples, after decoding the HTML FORM contents, merely verifies the text within a field to make sure it is a valid representation of an integer, without any junk thrown in, i.e. it must satisfy the regular expression: ^ *?+ *$ If the...
2
1777
by: anson | last post by:
this code is an example code of TPOP, i type them and can be compiled... but when give some input , it getting segment fault .... i observed that when the build() initial the word table ... it invoke next_word(in the book its name is new() )and invoke the routine lookup ...but it cannot lookup anything ....and return NULL. where goes wrong? below is the code...
55
3329
by: lovecreatesbea... | last post by:
Do you check all error conditions for all library calls in you code? Is it necessary to check all errors? Is it convenient to check all errors (or how to make code clean and readable with mass of non business logic related code block)? The following code from net-snmp library calls atoi five times but checks none. Do we code a wrapper my_atoi_with_error_check() around atoi and call this new one everywhere else? Is it a good practice...
144
5005
by: dominantubergeek | last post by:
Hello, I'm a highly experienced expert C programmer and I've written this code to reverse a string in place. I think you could all learn something from it! int reverse(char* reverseme){ int retval=-1; if(retval!=NULL){ int len=strlen(retval){ if(len>0){ int half=len>>1;
0
9646
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
10346
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
10157
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...
0
9956
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...
0
6742
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();...
0
5514
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4055
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
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.