LinuxQuestions.org
Review your favorite Linux distribution.
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 05-23-2011, 12:49 PM   #1
hectorDUQUE
Member
 
Registered: Jan 2007
Posts: 121

Rep: Reputation: 15
STL container into a C struct


Hi guys,
I have an old C application in which i am trying to include some STL cointainers. When i use the STL container alone it works fine, but when i include it into a C struct i have segmentation faults errors. I know that it is not a good idea to mix C and C++, but any way :-( ....

Considering this code:

Code:
typedef struct{
  int shmid;
  ...
  APPLSPACETYPE applSpace;

}memory;

typedef struct{
	...
	rLoadData_type		rData;
	...


}APPLSPACETYPE, *APPLSPACETYPE_PTR ;


typedef std::map<string, zeroGroup_type> map_completeGroups2zeroGroup_type;

map_completeGroups2zeroGroup_type 		completeGroups2zeroGroup2;
...
	
typedef struct rLoadData_type{
        ...
	map_completeGroups2zeroGroup_type 		completeGroups2zeroGroup;
	...
}rLoadData_type, *rLoadData_typePTR;

so, this insert operation works:

Code:
completeGroups2zeroGroup2.insert(std::make_pair("aaaaa", "bbbbb"));
and this insert operation fails because a segmentation fault.

Code:
mem->applSpace.rData.completeGroups2zeroGroup.insert(std::make_pair("ccccc", "dddddd"));
As i understand, STL makes automatic memory allocation, but it doesnt looks like using structs (2nd case) ... so, i am trying to make a malloc for the STL container ..

May somebody tell me what it is going wrong ?


and how to make a malloc for this issue; something like :


Code:
mem->applSpace.rData.completeGroups2zeroGroup=(map_completeGroups2zeroGroup_type *)malloc((sizeof(map_completeGroups2zeroGroup_type)+1)* sizeof(char ));
thanks in advance
Hector
 
Old 05-23-2011, 12:54 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,533

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
For starters, don't use malloc() in C++ code. Use "new". The latter calls the appropriate constructor for the object you are attempting to create. In your case, the STL map constructor. malloc() will not do this for you.

For additional help, please show how the following is setup:
Code:
mem->applSpace.rData
In other words, how is mem allocated/instantiated? What type is 'applSpace' and 'rData'.
 
Old 05-23-2011, 01:11 PM   #3
hectorDUQUE
Member
 
Registered: Jan 2007
Posts: 121

Original Poster
Rep: Reputation: 15
hi,

these types are defined into the code i provided before:

Code:
APPLSPACETYPE           applSpace;
rLoadData_type		rData;

memory *  mem;
regards,

Hector
 
Old 05-23-2011, 01:43 PM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,533

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by hectorDUQUE View Post
hi,

these types are defined into the code i provided before:

Code:
APPLSPACETYPE           applSpace;
rLoadData_type		rData;

memory *  mem;
regards,

Hector
What you have above (and what you provided earlier) doesn't tell me squat.

For all I know, APPLSPACETYPE is a typedef to an int, and rLoadData is typedef to a struct called Foo.

Have you corrected your code to use "new"?
 
Old 05-23-2011, 01:43 PM   #5
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by hectorDUQUE View Post
Considering this code:
You have not provided definition of zeroGroup_type.

Quote:
Originally Posted by hectorDUQUE View Post
and this insert operation fails because a segmentation fault.
Code:
mem->applSpace.rData.completeGroups2zeroGroup.insert(std::make_pair("ccccc", "dddddd"));
Not enough information. Most likely scenario that mem is not initialized (NULL or invalid pointer). Use debugger, walk through the routine, examine value of variables, find and fix the problem.

Quote:
Originally Posted by hectorDUQUE View Post
As i understand, STL makes automatic memory allocation, but it doesnt looks like using structs (2nd case) ... so, i am trying to make a malloc for the STL container ..
You should forget about it - won't work (all stl-containers are template-based, plus their internals depend on implementation. Either use "new", or don't use dynamic memory allocation at all. Avoiding dynamic memory allocation will be a better approach.

Quote:
Originally Posted by hectorDUQUE View Post
and how to make a malloc for this issue; something like :
You *don't* make malloc for "something like this" and you normally don't use malloc in C++. The only place where you could possible use malloc/free is global operators new/delete, which isn't the case in your situation.
 
Old 05-23-2011, 02:57 PM   #6
hectorDUQUE
Member
 
Registered: Jan 2007
Posts: 121

Original Poster
Rep: Reputation: 15
Code:
	typedef std::string zeroGroup_type;

ok guys,
in summary you advise me, not to use malloc or any dynamic memory allocation at all ... i will try the new/delete operator and will post what happen.

but this throw me into another question :-( .... so, it is not recommended to use STL containers into shared memory structures ???

regards,

hector
 
Old 05-23-2011, 04:09 PM   #7
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,533

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by hectorDUQUE View Post
but this throw me into another question :-( .... so, it is not recommended to use STL containers into shared memory structures ???
That is not what SigTerm is alluding to; he is saying (and I agree) that there really is no need to allocate an STL object. There isn't any benefit to it. After all, the STL object will do its own allocation each time you insert a new object (or value) into it.

As for the code you posted, I'm not sure why you were allocating the STL map; from your struct declaration, there's no need for that.
 
Old 05-23-2011, 05:45 PM   #8
hectorDUQUE
Member
 
Registered: Jan 2007
Posts: 121

Original Poster
Rep: Reputation: 15
Done !!!
:-)

Code:
memory          *mem;
mem = new memory();

APPLSPACETYPE *APPLSPACETYPE_PTR ;
APPLSPACETYPE_PTR = new APPLSPACETYPE ();

rLoadData_type *rLoadData_typePTR;
rLoadData_typePTR = new rLoadData_type ();
thank you VERY much guys ... finally, using "new", C++/STL allocate its space ... as you said :-)

..., let me go into a second question, :-( ...
do you think, can i share these structures using the "share memory" IPC resource ?
i have googled this issue, i only found out this link:

http://dev.firnow.com/course/6_syste...24/176925.html

however, because it deals with dynamic memory allocation, i wonder if it works safely.

please, comments are wellcome,

best regards,
 
Old 05-23-2011, 07:19 PM   #9
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,533

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by hectorDUQUE View Post
however, because it deals with dynamic memory allocation, i wonder if it works safely.
I have doubts that it will work at all. The data that needs to be access by the two (or more) processes needs to be "mappable" to a fixed chunk of memory. An STL object manages its data somewhere in memory, and that location can vary. Since that location is not part of the shared memory segment (data block), it is not possible for the client processes to access it. In fact, even a pointer to the address of a simple data type would not be mappable.
 
Old 05-24-2011, 05:57 PM   #10
hectorDUQUE
Member
 
Registered: Jan 2007
Posts: 121

Original Poster
Rep: Reputation: 15
still a question guys, some troubles yet ...

does it make sense to create a new object for the STL cointainer ?

Code:
something like:

mem->applSpace.rData.completeGroups2zeroGroup = new map_completeGroups2zeroGroup_type ();
what is the right sintaxis for this operation ?

regards
 
Old 05-24-2011, 06:13 PM   #11
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by hectorDUQUE View Post
still a question guys, some troubles yet ...

does it make sense to create a new object for the STL cointainer ?
If you're talking abotu creating container using "new", then NO, it doesn't.

Typically you use stl containers like this:
Code:
struct MyStruct{
    std::vector<int> v;
    std::map<int, int> myMap;
};
I.e. you make a member variable within a class or a struct, and let compiler handle initialization (stl constructors have constructors/destructors which are called automatically). I can't think of a scenario where you would really need to use "new" to create stl container.

Quote:
Originally Posted by hectorDUQUE View Post
Code:
mem->applSpace.rData.completeGroups2zeroGroup = new map_completeGroups2zeroGroup_type ();
If completeGroups2zeroGroup is a container such as std::map (and not a pointer to container), then you don't need to initialize it - it will be done automatically. To "reset" or "clear" a container, call its "clear()" method. If completeGroups2zeroGroup is a pointer to container (such as std::map*), then you'll have to create new object using operator "new".

Last edited by SigTerm; 05-24-2011 at 06:18 PM.
 
Old 05-24-2011, 10:29 PM   #12
hectorDUQUE
Member
 
Registered: Jan 2007
Posts: 121

Original Poster
Rep: Reputation: 15
thank you guys for help .... :-)
 
  


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
stl vector as a member of a struct elias_28 Programming 2 01-17-2011 03:38 PM
GCC compile problem:struct A have a member variable which is just a struct type name? leon.zcom Programming 3 04-18-2008 05:40 PM
retreiving a range of iterators with an stl container g_goblin Programming 1 09-01-2005 06:50 PM
g++ and wrong struct member addresses / struct size misreporting sonajiso Linux - General 5 05-22-2004 11:16 PM

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

All times are GMT -5. The time now is 07:36 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration