473,233 Members | 1,553 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,233 software developers and data experts.

STL & pipe, socket descriptors


Hi,

I am writing a simple irc-like server and came across weird behavior
while debugging. Could anyone tell me why the following happens :

I have a class which stores pipe descriptors as a member field.
Descriptors are initialized with pipe(descr) in class constructor.
Then I have an array of objects of this class, preferably a STL
vector. If I initialize the vector with:

vector<objectobjects(20);

all the objects have the same pipe descriptors (pipes are the same &
there is no point in have 20 of them)
this happens if I initialize with objects.push_back(...) as well. If I
initilize objects like that :

object* first_object = new object;
object* second_object = new object;

it is ok. (the descriptors given by pipe(descr); are different)

I want to use STL vector/list/whatever but it just doesn't work. I can
understand that vector<objectobjects(20); may construct just one
object and then copy it 20 times, but shouldn't push_back() be any
better?

Please help.

Marcin Endraszka

Jun 16 '07 #1
8 3327
M.*********@gmail.com wrote:
>
Hi,

I am writing a simple irc-like server and came across weird behavior
while debugging. Could anyone tell me why the following happens :

I have a class which stores pipe descriptors as a member field.
Descriptors are initialized with pipe(descr) in class constructor.
Then I have an array of objects of this class, preferably a STL
vector. If I initialize the vector with:

vector<objectobjects(20);
this calls

vector<object>::vector(vector<object>::size_type, const object& t=object());

so the definition of objects is the same as if you wrote

vector<objectobjects(20, object());

and this creates 20 copies of the temporary created as second argument.
all the objects have the same pipe descriptors (pipes are the same &
there is no point in have 20 of them)
this happens if I initialize with objects.push_back(...) as well.
What do you mean initialize with push_back?

vector<objectobjects;
for (int i = 0; i < 20; ++i)
objects.push_back(object());

?

It's hard to guess without seeing any code.

--
rbh
Jun 16 '07 #2
M.*********@gmail.com wrote:
Hi,

I am writing a simple irc-like server and came across weird behavior
while debugging. Could anyone tell me why the following happens :

I have a class which stores pipe descriptors as a member field.
Descriptors are initialized with pipe(descr) in class constructor.
Then I have an array of objects of this class, preferably a STL
vector. If I initialize the vector with:

vector<objectobjects(20);

all the objects have the same pipe descriptors (pipes are the same &
there is no point in have 20 of them)
this happens if I initialize with objects.push_back(...) as well. If I
initilize objects like that :

object* first_object = new object;
object* second_object = new object;

it is ok. (the descriptors given by pipe(descr); are different)

I want to use STL vector/list/whatever but it just doesn't work. I can
understand that vector<objectobjects(20); may construct just one
object and then copy it 20 times, but shouldn't push_back() be any
better?

Please help.

Marcin Endraszka
Post some code. Probably your definition of object is wrong, but who can
say without seeing some code.

john
Jun 16 '07 #3
On 16 Cze, 21:08, Robert Bauck Hamar <roberth+n...@ifi.uio.nowrote:
It's hard to guess without seeing any code.
class object
{
public:
int p[2];

object(int id)
{
pipe(p);
printf("Object %d has descriptor %d.",id,p[0]);
}

~object()
{
close(p[0]);
close(p[1]);
}
};
Static array of those will return eg.

object objects[3];
Object 1 has descriptor 7.
Object 2 has descriptor 9.
Object 3 has descriptor 11. (there are pairs of them of
c.)

STL vector will return:

vector<objectobjects;
objects.push_back(1);objects.push_back(2);objects. push_back(3);

Object 1 has descriptor 7.
Object 2 has descriptor 7.
Object 3 has descriptor 7.

Which is totally not what I want. What's more if there was a socket
descriptor no.7 before, I may get same pipe descriptor AND socket
descriptor at the same time, which is so wrong.

Ask whatever you need.

Marcin

Jun 16 '07 #4
M.*********@gmail.com wrote:
Static array of those will return eg.

object objects[3];
Object 1 has descriptor 7.
Object 2 has descriptor 9.
Object 3 has descriptor 11. (there are pairs of them of
c.)

STL vector will return:

vector<objectobjects;
Try at this point:

objects.reserve(3);
objects.push_back(1);objects.push_back(2);objects. push_back(3);

Object 1 has descriptor 7.
Object 2 has descriptor 7.
Object 3 has descriptor 7.

Which is totally not what I want.
You definitly violate the "rule of three". That may be the problem here.
Google for it. If it works with a reserve(3) in this example, that this is
your problem. The vector copies your objects and makes temporaries which
get destructed, closing your pipes. So the next pipe gets an old yet unused
descriptor.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Jun 16 '07 #5
M.*********@gmail.com wrote:
On 16 Cze, 21:08, Robert Bauck Hamar <roberth+n...@ifi.uio.nowrote:
>It's hard to guess without seeing any code.

class object
{
public:
int p[2];

object(int id)
{
pipe(p);
printf("Object %d has descriptor %d.",id,p[0]);
}

~object()
{
I'll give you a hint. Insert

printf("Descriptors %d and %d closed\n", p[0], p[1]);

here.
close(p[0]);
close(p[1]);
}
};

--
rbh
Jun 16 '07 #6
On 16 Cze, 22:38, "Thomas J. Gritzan" <Phygon_ANTIS...@gmx.dewrote:
You definitly violate the "rule of three". That may be the problem here.
Google for it. If it works with a reserve(3) in this example, that this is
your problem. The vector copies your objects and makes temporaries which
get destructed, closing your pipes. So the next pipe gets an old yet unused
descriptor.
Ok. I see the sequence is CONSTRUCT TEMP -COPY TO DESTINATION ->
DESTROY TEMP
which I don't like, but still makes sense

Writing a copy constructor seems to help

object(object const& in)
{
id = in.id;
pipe(p);
}

I also added assigment operator as one of you suggested:

object& operator=(const object& in)
{
p[0] = in.p[0];
p[1] = in.p[1];
}

Though I think compiler generated one would do the same.
Thanks for mentioning the "rule of three"

Cheers

Jun 16 '07 #7
M.*********@gmail.com wrote:
On 16 Cze, 22:38, "Thomas J. Gritzan" <Phygon_ANTIS...@gmx.dewrote:
>You definitly violate the "rule of three". That may be the problem here.
Google for it. If it works with a reserve(3) in this example, that this
is your problem. The vector copies your objects and makes temporaries
which get destructed, closing your pipes. So the next pipe gets an old
yet unused descriptor.

Ok. I see the sequence is CONSTRUCT TEMP -COPY TO DESTINATION ->
DESTROY TEMP
which I don't like, but still makes sense.
It's what C++ does.
Writing a copy constructor seems to help

object(object const& in)
{
id = in.id;
pipe(p);
}
IMHO, this is the wrong solution. It creates a lot of pipes, and it makes
the object harder to use (copies aren't equal as one would usually expect).
I also added assigment operator as one of you suggested:

object& operator=(const object& in)
{
p[0] = in.p[0];
p[1] = in.p[1];
}

Though I think compiler generated one would do the same.
Your compiler would generate the same thing. You need the rule of three, but
not this way.

Solution 1)
class object {
... //as before
private:
void operator = (const object&); // No need to actually implement these
object(const object&);
};

vector<boost::shared_ptr<object objects;
for (...)
objects.push_back(boost::shared_ptr<object>(new object(i)));

This would make the objects non copyable, and your boost::shared_ptr takes
care of deleting. You could also use std::auto_ptr instead of
boost::shared_ptr.

Solution 2)
class object {
struct arr {
int p[2];
};
boost::shared_ptr<arrp;
public:
object() : p(new arr)
{ pipe(p->p); }
// compiler generated copy ctor, assignment op and dtor works
//...
};

Boost::shared_ptr can be downloaded from <URL:http://boost.org>. It's also
up for the new standard, and might be found if your compiler ships the tr1
libs. In that case it's called std::tr1::shared_ptr (turn on tr1 and
#include <memory>)

--
rbh
Jun 16 '07 #8
On Jun 16, 7:34 pm, Robert Bauck Hamar <roberth+n...@ifi.uio.no>
wrote:
vector<boost::shared_ptr<object objects;
for (...)
objects.push_back(boost::shared_ptr<object>(new object(i)));

This would make the objects non copyable, and your boost::shared_ptr takes
care of deleting. You could also use std::auto_ptr instead of
boost::shared_ptr.
no, you couldn't

Diego

Jun 18 '07 #9

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

Similar topics

1
by: jenny | last post by:
Hi, I have a java socket program running on AIX 4.3.3.0 platform. It opens a socket and sends data to our customer over a leased fractional T1 line. The line is always connected. However,...
2
by: Hans Deragon | last post by:
Greetings. I am performing: commands.getstatusoutput("rsync <params>"); rsync takes a long time and prints out a steady stream of lines showing which file it is currently working on.
7
by: Dario | last post by:
I have an unmanaged library that handle many TCP/IP connections. In my .NET application i want to test if there is input available on these connections. Using an existing function of the unmanaged...
4
by: Pascal Ehlert | last post by:
I don't know if this is the right newsgroup because the question is maybe a bit linux specific so if not slap me ;-) I'm trying to send the output of a perl-script to a socket. So I'm opening a...
4
by: billiejoex | last post by:
Hi, I'm writing a small asyncore-based server application serving a lot of clients. When I have to handle more than 1021 client simoultaneously the 'binded' socket object raises an error: ...
11
by: atlaste | last post by:
Hi, In an attempt to create a full-blown webcrawler I've found myself writing a wrapper around the Socket class in an attempt to make it completely async, supporting timeouts and some scheduling...
3
by: Yang | last post by:
Hi, I'm experiencing a problem when trying to close the file descriptor for a socket, creating another socket, and then closing the file descriptor for that second socket. I can't tell if my issue...
1
by: Jean-Paul Calderone | last post by:
On Mon, 12 May 2008 17:35:44 -0700 (PDT), schickb <schickb@gmail.comwrote: Alas, you haven't actually closed src's standard input. Though you did close src.stdin, you didn't close the copy of it...
3
by: Jeremy Sanders | last post by:
Hi - I have some code which works under linux. It starts a remote python process using subprocess and communicates to it via a pipe created by os.pipe. As far as I understand, child processes...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.