473,803 Members | 3,422 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using special allocator interfaces...

Does this program produce any undefined-behavior:

http://pastebin.com/m5eb3c81a

?
Jun 27 '08 #1
5 1306
"Alf P. Steinbach" <al***@start.no wrote in message
news:cI******** *************** *******@posted. comnet...
>* Chris Thomasson:
>Does this program produce any undefined-behavior:

http://pastebin.com/m5eb3c81a

?

Yes,

*pregion = (Region*)0x1234 8765

is formally UB.
Is that the only thing you can find? BTW, this region pointer is never used.
It is only there to make sure the region value holds one a per-block basis.
Is there for the call to assert on line 26.

;^)

Jun 27 '08 #2
"Alf P. Steinbach" <al***@start.no wrote in message
news:xeOdnY77pZ L6mr7VnZ2dnUVZ_ gSdnZ2d@comnet. ..
>* Chris Thomasson:
>"Alf P. Steinbach" <al***@start.no wrote in message
news:cI******* *************** ********@posted .comnet...
>>* Chris Thomasson:
Does this program produce any undefined-behavior:

http://pastebin.com/m5eb3c81a

?

Yes,

*pregion = (Region*)0x1234 8765

is formally UB.

Is that the only thing you can find?

If you want a more thorough discussion, post the code, and explain what
you're concerned about. Then others will probably jump on it. It's The
Way(TM).
Okay:
/*************** *************** *************** *************** **********/
#include <cstdio>
#include <cstddef>
#include <cassert>
#include <new>

// Imagine there is a "special" allocator interface...
// I will just use ::operator new/delete for place holders
namespace Allocator {
struct Region;

static void* MemRequest(
Region** pregion,
std::size_t size
) {
*pregion = (Region*)0x1234 8765;
return ::operator new(size);
}

static void MemRelease(
Region* region,
void* ptr,
std::size_t size
) {
assert(region == (Region*)0x1234 8765);
::operator delete(ptr);
}
}


// Can I use the above allocator interface like this:
template<typena me T>
struct AllocatorBlock {
unsigned char mBuffer[sizeof(T)];
Allocator::Regi on* mRegion;

static void* MemRequest() {
Allocator::Regi on* region;
AllocatorBlock* const block = (AllocatorBlock *)
Allocator::MemR equest(&region, sizeof(T));
block->mRegion = region;
return block->mBuffer;
}

static void MemRelease(void * ptr) {
AllocatorBlock* const block = (AllocatorBlock *)ptr;
Allocator::MemR elease(block->mRegion, ptr, sizeof(T));
}
};
struct MyObject {
int const State;
MyObject(int state) : State(state) {}

// special per-object new/delete overload...
void* operator new(std::size_t size) {
return AllocatorBlock< MyObject>::MemR equest();
}

void operator delete(void* ptr) {
AllocatorBlock< MyObject>::MemR elease(ptr);
}
};


#define OBJECTS 10

int main() {
int i;
MyObject* objs[OBJECTS] = { NULL };

for (i = 0; i < OBJECTS; ++i) {
objs[i] = new MyObject(i + 1);
std::printf("(% p/%d) - Create\n", (void*)objs[i], objs[i]->State);
}

std::puts("-----------------------------------------");

for (i = 0; i < OBJECTS; ++i) {
std::printf("(% p/%d) - Destroy\n", (void*)objs[i], objs[i]->State);
delete objs[i];
}
std::puts("\n\n \n_____________ _______________ ______\
_______________ __________\nPre ss <ENTERto exit...");
std::getchar();
return 0;
}
/*************** *************** *************** *************** **********/
Jun 27 '08 #3
Chris Thomasson wrote:
"Alf P. Steinbach" <al***@start.no wrote in message
news:cI******** *************** *******@posted. comnet...
>* Chris Thomasson:
>>Does this program produce any undefined-behavior:

http://pastebin.com/m5eb3c81a

?

Yes,

*pregion = (Region*)0x1234 8765

is formally UB.

Is that the only thing you can find? BTW, this region pointer is never
used. It is only there to make sure the region value holds one a
per-block basis. Is there for the call to assert on line 26.

;^)
I believe "undefined behavior" can include the behavior of "cause a CPU
exception". If you need to create a special instance of Region to do
assertions, go ahead and do so!

Region specialRegion;

--
Daniel Pitts' Tech Blog: <http://virtualinfinity .net/wordpress/>
Jun 27 '08 #4
"Chris Thomasson" <cr*****@comcas t.netwrote in message
news:6Y******** *************** *******@comcast .com...
"Alf P. Steinbach" <al***@start.no wrote in message
[...]
// Can I use the above allocator interface like this:
template<typena me T>
struct AllocatorBlock {
unsigned char mBuffer[sizeof(T)];
Allocator::Regi on* mRegion;

static void* MemRequest() {
Allocator::Regi on* region;
AllocatorBlock* const block = (AllocatorBlock *)
Allocator::MemR equest(&region, sizeof(T));
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^
This is in ERROR!

The size is all wrong on a per-object basis; here is correction:
AllocatorBlock* const block = (AllocatorBlock *)
Allocator::MemR equest(&region, sizeof(*block)) ;
block->mRegion = region;
return block->mBuffer;
sizeof(T) is not sufficient, however sizeof(*block) is.

block->mRegion = region;
return block->mBuffer;
}

static void MemRelease(void * ptr) {
AllocatorBlock* const block = (AllocatorBlock *)ptr;
Allocator::MemR elease(block->mRegion, ptr, sizeof(T));
}
};


[...]

Strange that I did not get flamed for this yet!
;^D

Jun 27 '08 #5
"Chris Thomasson" <cr*****@comcas t.netwrote in message
news:m8******** *************** *******@comcast .com...
"Chris Thomasson" <cr*****@comcas t.netwrote in message
news:6Y******** *************** *******@comcast .com...
>"Alf P. Steinbach" <al***@start.no wrote in message
[...]
>// Can I use the above allocator interface like this:
template<typen ame T>
struct AllocatorBlock {
unsigned char mBuffer[sizeof(T)];
Allocator::Regi on* mRegion;

static void* MemRequest() {
Allocator::Regi on* region;
AllocatorBlock* const block = (AllocatorBlock *)
Allocator::MemR equest(&region, sizeof(T));
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^

This is in ERROR!

The size is all wrong on a per-object basis; here is correction:
[...]
> static void MemRelease(void * ptr) {
AllocatorBlock* const block = (AllocatorBlock *)ptr;
Allocator::MemR elease(block->mRegion, ptr, sizeof(T));
}
};

sizeof(*block), not sizeof(T) in MemRelease:

static void MemRelease(void * ptr) {
AllocatorBlock* const block = (AllocatorBlock *)ptr;
Allocator::MemR elease(block->mRegion, ptr, sizeof(*block)) ; // <-- !
}
Of course!

Jun 27 '08 #6

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

Similar topics

7
10630
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using namespace std; : : vector<string>* getTyphoon()
7
3182
by: sbobrows | last post by:
{Whilst I think much of this is OT for this newsgroup, I think the issue of understanding diagnostics just about gets under the door. -mod} Hi, I'm a C++ newbie trying to use the Boost regex libraries. Here's my situation. My system is Red Hat Linux 9, using the Borland C++BuilderX Personal IDE (which uses the g++ compiler).
2
2285
by: Weddick | last post by:
I decided to try creating a map with microsoft visual C++ 6. When building this small app I get 95 warnings which make no sense to me. Anybody else see this before? Thanks, // CODE SAMPLE #include <map> #include <string>
13
9654
by: Stumped and Confused | last post by:
Hello, I really, really, need some help here - I've spent hours trying to find a solution. In a nutshell, I'm trying to have a user input a value in form's textfield. The value should then be assigned to a variable and output using document.write. (Note, there is no submit button or other form elements. Basically
13
9681
by: kamaraj80 | last post by:
Hi I am using the std:: map as following. typedef struct _SeatRowCols { long nSeatRow; unsigned char ucSeatLetter; }SeatRowCols; typedef struct _NetData
4
2947
by: Robert Frunzke | last post by:
Hello, I need to implement a custom allocator to speed up the allocation of a specific class in my project and instead of hardwiring it, I would "templatize"(?) it. The allocator should have minimal overhead and be used for allocation of a large number of "Node" classes which represent a hierarchical structure. The custom allocator has to be at least intrusive as possible for the user. The allocator has to be somehow bound to the...
7
17059
by: tehn.yit.chin | last post by:
I am trying to experiment <algorithm>'s find to search for an item in a vector of struct. My bit of test code is shown below. #include <iostream> #include <vector> #include <algorithm> #include <string> struct abc_struct {
3
9869
by: Nelis Franken | last post by:
Good day. I'm having trouble telling STL to use a member function to sort items within the relevant class. The isLess() function needs to have access to the private members of the Foo class to determine if the one item is less than the other (it uses two vectors, one containing the actual data, and one that stores IDs that index into the data vector). The code below is pretty self-explanatory. I've looked into mem_fun, but I'm stuck....
3
2829
by: vrsathyan | last post by:
Hi.., While executing the following code in purifier.., std::vector<int> vecX; vecX.clear(); int iCount = 0; { int iVal;
0
9703
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
9565
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
10317
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
10069
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
9125
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7604
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
6844
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
5501
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4275
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.