Hello,
I'm starting to use CUDA at the moment and have to admit that I'm a bit disappointed with the C API. I understand the reasons for choosing C but had the language been based on C++ instead, several aspects would have been a lot simpler, e.g. device memory allocation (via cudaMalloc).
My plan was to do this myself, using overloade...
I just learned about the C++ construct called "placement new". It allows you to exactly control where a pointer points to in memory. It looks like this:
#include <new> // Must #include this to use "placement new"
#include "Fred.h" // Declaration of class Fred
void someCode()
{
char memory[sizeof(Fred)];
void* pla...
I've seen a class which is a class which is defined like this..
class StringChild : public StringBase
{
public:
//some non-virtual functions
static StringChild* CreateMe(int size);
private:
unsigned char iBuf[1];
};
The static factory function has the following implementation..
return new(malloc(__builtin_offsetof...
I have a pool manager template class. When a class object gets added back to the pool manager I would like to reset it back to it's initial state. I would like to call the placment destructor and placment constructor on it so it gets fully reset for the next time it is given out by the pool manager. I've tried many ways to get this to wo...
If I initialize a POD class with placement new, can I assume that the memory will be default initialized (to zeros)? This resource clearly states that if you call the zero argument default constructor explicitly that the fields will be default initialized, but it is not clear if this would hold true using the default implementation of pl...
I made a couple macros to make using placement new a bit easier. I was just wondering if there were any obvious cases where these would not work. Thanks.
#define CONSTRUCT_INPLACE(TYPE,STORAGE,INIT) ::new((TYPE*)STORAGE) TYPE INIT
#define DESTRUCT_INPLACE(TYPE,STORAGE) ((TYPE*)STORAGE)->~TYPE()
...
I'm using C++, and I have the following structures:
struct ArrayOfThese {
int a;
int b;
};
struct DataPoint {
int a;
int b;
int c;
};
In memory, I want to have 1 or more ArrayOfThese elements at the end of each DataPoint. There are not always the same number of ArrayOfThese elements per DataPoint.
Because I have a ridicul...
Hi, I'm playing with dynamic memory allocation "by hand" and I wanted to see how placement new is implemented by guys from MS but when debugging I "stepped into" it moved me to code:
inline void *__CRTDECL operator new(size_t, void *_Where) _THROW0()
{ // construct array with placement at _Where
return (_Where);
}
Could anyone explain...
Here's some C++ code that just looks funny to me, but I know it works.
There is a struct defined, and in the program we allocate memory using a void pointer. Then the struct is created using the allocated buffer.
Here's some code
typedef struct{
char buffer[1024];
} MyStruct
int main()
{
MyStruct* mystruct_ptr = 0;
void* ...
I've never quite understood how the argument lists for operator overloading are determined in a systematic way, and I'm particularly confused by a problem I have now.
When you overload a unary operator it has one argument, or zero if it's a class member. When you overload a binary operator it has two arguments, or one if it's a class m...
Hello
Can I call constructor explicitly, without using new, if I already have a memory for object?
class Object1{
char *str;
public:
Object1(char*str1){
str=strdup(str1);
puts("ctor");
puts(str);
}
~Object1(){
puts("dtor");
puts(str);
free(str);
}
};
Object1 ooo[2] = ...
For a class without default constructor, operator new and placement new can be used to declare an array of such class.
When I read the code in More Effective C++, I found the code as below(I modified some part).....
My question is, why [] after the operator new is needed?
I test it without it, it still works. Can any body explain th...
Is this safe? I'm not using any virtual functions in my actual implementation, but I'm tempted to believe that even if I was, it would still be safe.
class Foo
{
Foo()
{
// initialize things
}
Foo( int )
{
new ( this ) Foo();
}
}
...
This is an example of my codes:
template <typename T> struct MyStruct {
T object;
}
template <typename T> class MyClass {
MyStruct<T>* structPool;
size_t structCount;
MyClass(size_t count) {
this->structCount = count;
this->structPool = new MyStruct<T>[count];
for( size_t i=0 ; i<count ; i++ ) ...
Possible Duplicate:
C++'s placement new
What is an in-place constructor in C++?
e.g. Datatype *x = new(y) Datatype();
...
Hmm... Title is a bit of a mouthful, but I'm really not sure which part of this is causing issues, I've run through it a ton of times, and can't pinpoint why...
The idea is for a single Choice instance to be able to store any one value of any
of the types passed in to it's template list... It's kind of like a union, except
it keeps trac...
Can I call the C++ placement new on constructors with parameters? I am implementing a custom allocator and want to avoid having to move functionality from non-default constructors into an init function.
class CFoo
{
public:
int foo;
CFoo()
{
foo = 0;
}
CFoo(int myFoo)
{
foo = myFoo;
}
};
CFo...
What is the second line? (Seen while answering another question.)
int * x = new int [1] ;
int * y = new (x) int;
After the second line x and y have the same value (point to a same place). What's the difference between y = x and the second line? Is it like a constructor or something?
...
SomeObj<unsigned int>* Buffer;
char* BufferPtr = MemoryManager::giveMeSomeBytes(resX*resY*sizeof(SomeObj<unsigned int>));
Buffer = new(BufferPtr) SomeObj<unsigned int>[resX*resY];
when I step past these lines with the debugger, it shows me the values for the variables Buffer and BufferPtr:
BufferPtr: 0x0d7f004c
Buffer: 0x0d7f0050
...