LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-14-2006, 05:25 AM   #1
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
C++: construct object at certain address


How can I construct an object at a certain address?
The problem is: I have allocated (enough) memory for an std::vector, and now I want to call an std::vector constructor for this memory address. Is there any way I can do this?
 
Old 06-14-2006, 05:54 AM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
...I have allocated (enough) memory for an std::vector...
???
A vector looks after its own allocation of memory, I don't understand why you have done this.

Last edited by dmail; 06-14-2006 at 05:55 AM.
 
Old 06-14-2006, 06:26 AM   #3
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Ya.... if I'm understand your question correctly, the only way to accomplish this is to write a class that overloads the way the constructor for the std::vector works. In the typical situation the standard vector calls the standard allocator on creation and handles the memory for you... that is kind of the point of the STL classes...
 
Old 06-14-2006, 06:47 AM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Maybe you have a vector of objects, with each vector element being the size to hold an object?

If so that's not the way to go. Create a vector of pointers to the base object and then as you create the objects that you want assign them into the vector. This will work for objects of different classes so long as they share the same common parent, and then polymorphism with kick in for your virtual methods.
 
Old 06-14-2006, 08:42 AM   #5
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Original Poster
Rep: Reputation: 31
You didn't understand my question. I do not want to construct the objects held by the vector, but instead the vector itself, at a certain address.
An example (independent of std::vector):
Code:
class A
{
public:
  A( int value ) : x( value ) {}
  int x;
}

// ...

A *p = malloc( sizeof( A ) );
// Now call the constructor of A for p
// Something like:
p.constructor( 5 );

// ...

p.destructor();
free( p );
A in this example corresponds to std::vector in my code.
(I have my reasons not to write "p = new A( 5 );" )

I hope I could make my problem clear. Do you have any suggestions?
 
Old 06-14-2006, 09:53 AM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
There is nothing stopping you from having a method called constructor and using it like an initialiser method - remember in C++ the constructor is the name of the class and when called dynamically is used in conjunction with the new keyword.
 
Old 06-14-2006, 09:56 AM   #7
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
I should add not using the new keyword is going to provide you with code that is harder for others to maintain, because they will be expecting to use the new keyword.

Second your code needs to dereference the pointer, hence it should be p->constructor(5);
 
Old 06-14-2006, 10:50 AM   #8
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Ok I sort of see what you mean but...
Quote:
(I have my reasons not to write "p = new A( 5 );" )
can you tell me the reason for the above?

Not using the keywords is bad for reason mentioned and because delete also calls the destructor of classes/templates. If you insist on using malloc and free things get difficult.

using new you would have
Code:
std::vector<type>* new_vector = new std::vector<type>;
translating this to using malloc throws some problems.
Code:
std::vector<type>* malloc_vector = malloc(sizeof(std::vector<type>));
the above is not valid as it will not cast from a void* to a vector pointer.
If you try the c way of doing things ie
Code:
std::vector<type>* malloc_vector = (std::vector<type>*)malloc(sizeof(std::vector<type>));
then this is not valid C++(see other thread in this forum for reasons)
http://www.linuxquestions.org/questi...casting+malloc
Code:
std::vector<int>* malloc_vector = dynamic_cast<std::vector<int>*>(malloc(sizeof(std::vector<int>)));
another option that can't be used is dynamic_cast as it can not convert from a void* to vector<int>*
same goes for reinterpret_cast
Code:
std::vector<int>* malloc_vector = reinterpret_cast<std::vector<int>*>(malloc(sizeof(std::vector<int>)));
So as for how to make this possible, well i dont know unless you use an unsafe c style cast or I have missed something.??

[edit]added link

Last edited by dmail; 06-14-2006 at 11:01 AM.
 
Old 06-14-2006, 10:52 AM   #9
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by graemef
There is nothing stopping you from having a method called constructor and using it like an initialiser method
Yes, there is:
1.) Obviously I can't modify std::vector (or, in the example, class A).
2.) If the class contains virtual functions, the virtual-functions-table pointer has to be initialized. This can't be done manually.
Quote:
I should add not using the new keyword is going to provide you with code that is harder for others to maintain, because they will be expecting to use the new keyword.
As I said, there are reasons.
Consider the following situation: you have a class which uses only a very small amount of memory, say 1 byte, and you can't change that class (its declaration and definition are given).
Now, using new will occupy at least 12 bytes on my 32-bit machine (new[] is not an option, for certain reasons). If you create 2^20 instances, 11 MiB will be wasted for just 1 MiB payload. This is hardly acceptable.
Quote:
Second your code needs to dereference the pointer, hence it should be p->constructor(5);
I know. This was supposed to be some kind of pseudo code to make my intentions clear.
 
Old 06-14-2006, 11:02 AM   #10
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Original Poster
Rep: Reputation: 31
Quote:
Consider the following situation: you have a class which uses only a very small amount of memory, say 1 byte, and you can't change that class (its declaration and definition are given).
Now, using new will occupy at least 12 bytes on my 32-bit machine (new[] is not an option, for certain reasons). If you create 2^20 instances, 11 MiB will be wasted for just 1 MiB payload. This is hardly acceptable.
I should add: in my case, malloc() will not add the mentioned overhead, because I can malloc() a large chunk of memory (say, 16 kiB), and fill it with instances of A. These instances won't be free()'d before program termination, so they do not have to be free()'d individually.

I know that the described situation is rare (and I just had an idea how I can use new[] after all).

Still, I would like to know if it is possible, and how (out of curiousity ).

Last edited by addy86; 06-14-2006 at 11:03 AM.
 
Old 06-14-2006, 11:24 AM   #11
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by addy86
Consider the following situation: you have a class which uses only a very small amount of memory, say 1 byte, and you can't change that class (its declaration and definition are given).
Now, using new will occupy at least 12 bytes on my 32-bit machine
I'm sorry but I don't follow you line of argument here. Using new will return a pointer to your object, which would be 4 bytes, but if you only want to store the object then that will be back to the 1 byte. I'm sure I've missed something but I can't see where the 12 bytes come from.
 
Old 06-14-2006, 11:57 AM   #12
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Original Poster
Rep: Reputation: 31
new uses either the same or a similar implementation as malloc(). The glibc implementation uses (on 32-bit systems) 4 bytes before and 4 bytes after the payload memory area. These two values are aligned to 4-byte borders, hence 12 bytes are needed (plus the internal management overhead of malloc(), which I didn't count).
The returned pointer has nothing to do with this calculation (as it is needed either way).
 
Old 06-14-2006, 01:37 PM   #13
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Are there compelling reasons for the individual bytes to be held as individual objects? I would look at having the object to hold more data, or maybe even removing the object concept and running this part procedurally, because holding it as an object will have other overheads such as those that will allow rtti to work.
 
Old 06-14-2006, 02:28 PM   #14
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
These are the a few ways a program is going to get memory to store anything:
1. Have a global or static variable, in which case the memory is set aside at the beginning of the program
2. Have a local variable, which is stored on the stack
3. Have an instance variable, in which case it would be stored as part of however the instance is stored
4. Allocate it with "new" from the heap

So if you don't like any of the above methods, then tough luck.
 
Old 06-14-2006, 08:31 PM   #15
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
then this is not valid C++(see other thread in this forum for reasons)
oops.
It's valid C++ not C.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
getting rpoblem in assigning value to variable in if construct Ankit mittal Programming 9 01-07-2006 09:12 AM
how to get ip address, broadcast address, mac address of a machine sumeshstar Programming 2 03-12-2005 04:33 AM
how-to construct execution trees when using yacc sibtay Programming 0 12-30-2004 06:32 AM
Event driven object-to-object: C++ template class mecanism ( NOT STL or STDC++) bretzeltux Programming 2 12-23-2003 02:45 PM
Help me construct an Xmms Skin mini-Howto Please read! shassouneh Linux - General 3 05-01-2002 06:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration