LinuxQuestions.org
Visit Jeremy's Blog.
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 01-17-2011, 12:55 PM   #1
elias_28
LQ Newbie
 
Registered: Jul 2010
Posts: 7

Rep: Reputation: 0
stl vector as a member of a struct


Hi,

is there any problem that might rise by by having a vector as a member of struct in c++ as follows.

ex.

struct A
{
int a;
vector<int> b;
}

class B {
A **ptr_A;
}

i am having a double pointer to A and allocating the data to that ptr with malloc. I know that i am mixing C with C++ but i want to know if there can be any memory problems predicted by having this mixture.

Especially that the constructor of b will not be called when allocating A.

thanks
 
Old 01-17-2011, 01:05 PM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by elias_28 View Post
is there any problem that might rise by by having a vector as a member of struct in c++
Normally no. But ...

Quote:
i am having a double pointer to A and allocating the data to that ptr with malloc. I know that i am mixing C with C++ but i want to know if there can be any memory problems predicted by having this mixture.

Especially that the constructor of b will not be called when allocating A.
I'm not clear on what you are allocating with malloc (even less clear on why).

The constructor/destructor that matters in the code you posted is A() and ~A(). You need to be sure those are called correctly.

For example (but I didn't test it for typos etc.):
Code:
B b;
b.ptr_A = (A**)( malloc( n * sizeof(A*) ) );
I think that is OK, because the pointers don't necessarily need construction.

Code:
b.ptr_A[0] = (A*)(malloc( sizeof(A) ) );
That one is not OK. It is skipping the constructor for A.

But regarding your question about problems arising from having a vector as a member of a struct: I think that is a very distorted point of view. The problem (if any) comes from not calling a required constructor. Blaming that problem on the aspect of the struct that requires construction is not a good software engineering viewpoint.

Last edited by johnsfine; 01-17-2011 at 03:26 PM.
 
Old 01-17-2011, 02:38 PM   #3
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

Rep: Reputation: 475Reputation: 475Reputation: 475Reputation: 475Reputation: 475
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
GCC compile problem:struct A have a member variable which is just a struct type name? leon.zcom Programming 3 04-18-2008 04:40 PM
STL vector, push_front, pop_front not available !!! Laeeq Programming 3 04-04-2006 01:08 PM
g++ and wrong struct member addresses / struct size misreporting sonajiso Linux - General 5 05-22-2004 10:16 PM
STL vector of 3 doubles? true_atlantis Programming 2 04-22-2004 10:13 AM
Vector(STL) question(push_back & erase) Ohmu Programming 1 01-29-2004 04:50 PM

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

All times are GMT -5. The time now is 12:28 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