473,512 Members | 15,363 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why the commented code make things wrong?

Hello, everyone,

I am writing a segment of code for listing the permutation of some
event sequences.
I tried to use template to make the code more general. When I
commented the
code as shown below, the code works okay. Otherwise, it will goes to
seg fault.
I am puzzled on this. Can anybody give me any hints?

Thank you very much!

- Andy

The following is the code:

#include <vector>

using std::vector;

template<typename T>
class Scheduler
{
struct ChoicePoint
{
public:
// ChoicePoint() {
// event_counters.clear();
// pid = event_index = -1;
// }

// ChoicePoint(const ChoicePoint& cp){
// event_counters = cp.event_counters;
// pid = cp.pid;
// event_index = cp.event_index;
// }

// ChoicePoint operator = (const ChoicePoint& rhs){
// event_counters = rhs.event_counters;
// pid = rhs.pid;
// event_index = rhs.event_index;
// }

// bool operator == (const ChoicePoint& rhs){
// return (event_counters == rhs.event_counters &&
// pid == rhs.pid && event_index == rhs.event_index);
// }

public:
vector<int> event_counters; // progress of each process
int pid;
int event_index;
};

public:
Scheduler(vector<vector<T> >& _events)
{
events = _events;
num_procs = events.size();

ChoicePoint cp;
cp.event_counters.clear();
cp.event_counters.resize(num_procs, 0);
cp.pid = -1;
cp.event_index = -1;

choice_stack.clear();
choice_stack.push_back(cp);
}

bool has_other_choices(ChoicePoint cp){
int i;
for (i = cp.pid + 1; i < num_procs; i++)
if (cp.event_counters[i] < events[i].size() - 1) break;

return (i < num_procs);
}

ChoicePoint next_choice(ChoicePoint cp){
int i;
assert( has_other_choices(cp) );
for (i = cp.pid + 1; i < num_procs; i++)
if (cp.event_counters[i] < events[i].size() - 1) break;

ChoicePoint newcp = cp;
newcp.event_counters[cp.pid]--;
newcp.pid = i;
newcp.event_index = cp.event_counters[i];
newcp.event_counters[i]++;

return newcp;
}

bool has_next_level_choice(ChoicePoint cp){
int i;
for (i = 0; i < num_procs; i++)
if (cp.event_counters[i] < events[i].size()) return true;
return false;
}

ChoicePoint next_level_choice(ChoicePoint cp){
int i;
assert( has_next_level_choice(cp) );
for (i = 0; i < num_procs; i++)
if (cp.event_counters[i] < events[i].size()) break;

ChoicePoint newcp;
newcp = cp;
newcp.pid = i;
newcp.event_index = cp.event_counters[i];
newcp.event_counters[i]++;

return newcp;
}
bool next_permutation(vector<T>& result){
choice = choice_stack[ choice_stack.size() - 1];
choice_stack.pop_back();

while ( !has_other_choices(choice) && !choice_stack.empty() ){
choice = *(choice_stack.rbegin());
choice_stack.pop_back();
}

if (!has_other_choices(choice)) return false;

ChoicePoint newcp;
int i, len;

choice = next_choice(choice);
while ( has_next_level_choice(choice) ){
choice_stack.push_back(choice);
choice = next_level_choice(choice);
}

choice_stack.push_back(choice);

T val;
int pid, eidx;

result.clear();
len = choice_stack.size();
for (i = 0; i < len; i++){
pid = choice_stack[i].pid;
eidx= choice_stack[i].event_index;

val = events[pid][eidx];
result.push_back(val);
}
return true;
}
void set_input(vector<vector<T> > &val);

private:

void get_trace_from_choice_stack(vector<T>& trace);

private:
vector<vector<T> > events;
int num_procs;

vector<ChoicePoint> choice_stack;
ChoicePoint choice;
};
#include <iostream>

using namespace std;

int main()
{
vector<vector<int> > a;

vector<int> a1, a2;
a1.push_back(1);
a1.push_back(3);

a2.push_back(4);
a2.push_back(6);

a.push_back(a1);
a.push_back(a2);

Scheduler<int> sched(a);

vector<int> result;

while ( sched.next_permutation(result) ){
int i;
for (i = 0; i < result.size(); i++)
cout<< result[i] <<" ";
cout<<endl;
}

return 0;
}

Mar 7 '06 #1
3 1673
// ChoicePoint() {
// event_counters.clear();
// pid = event_index = -1;
// }


A shot from the hip: The clear() segfaults.
Mar 7 '06 #2
Andy wrote:
Hello, everyone,

I am writing a segment of code for listing the permutation of some
event sequences.
I tried to use template to make the code more general. When I
commented the
code as shown below, the code works okay. Otherwise, it will goes to
seg fault.
I am puzzled on this. Can anybody give me any hints?

Thank you very much!

- Andy

The following is the code:

#include <vector>

using std::vector;

template<typename T>
class Scheduler
{
struct ChoicePoint
{
public:
// ChoicePoint() {
// event_counters.clear();
// pid = event_index = -1;
// }

// ChoicePoint(const ChoicePoint& cp){
// event_counters = cp.event_counters;
// pid = cp.pid;
// event_index = cp.event_index;
// }

// ChoicePoint operator = (const ChoicePoint& rhs){
// event_counters = rhs.event_counters;
// pid = rhs.pid;
// event_index = rhs.event_index;


Most likely you're missing a "return *this" just here?

Note that you're comparing signed and unsigned integers in some parts of
your program...

Mathias
Mar 7 '06 #3
* Andy:
Hello, everyone,

I am writing a segment of code for listing the permutation of some
event sequences.
I tried to use template to make the code more general. When I
commented the
code as shown below, the code works okay. Otherwise, it will goes to
seg fault.
I am puzzled on this. Can anybody give me any hints?

Thank you very much!

- Andy

The following is the code:

#include <vector>

using std::vector;

template<typename T>
class Scheduler
{
struct ChoicePoint
{
public:
// ChoicePoint() {
// event_counters.clear();
// pid = event_index = -1;
Preferentially use a constructor initializer list, and don't use
multiple assignment.
// }

// ChoicePoint(const ChoicePoint& cp){
// event_counters = cp.event_counters;
// pid = cp.pid;
// event_index = cp.event_index;
// }
This copy constructor has the same final effect as the
compiler-generated one, but is less efficient (using vector assignment
instead of vector construction).


// ChoicePoint operator = (const ChoicePoint& rhs){
// event_counters = rhs.event_counters;
// pid = rhs.pid;
// event_index = rhs.event_index;
// }
Must return *this. Since you don't you have undefined behavior.

But even when fixed this assignment operator is inefficient. It should
instead return a reference to the object assigned to.

And when /that/ is fixed it's identical to the compiler-generated one.
// bool operator == (const ChoicePoint& rhs){
Should be a const function.

// return (event_counters == rhs.event_counters &&
// pid == rhs.pid && event_index == rhs.event_index);
// }

public:
vector<int> event_counters; // progress of each process
int pid;
int event_index;
};


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 7 '06 #4

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

Similar topics

289
1805
by: napi | last post by:
I think you would agree with me that a C compiler that directly produces Java Byte Code to be run on any JVM is something that is missing to software programmers so far. With such a tool one could...
192
9302
by: Vortex Soft | last post by:
http://www.junglecreatures.com/ Try it and tell me what's happenning in the Microsoft Corporation. Notes: VB, C# are CLS compliant
40
3006
by: Neo The One | last post by:
I think C# is forcing us to write more code by enforcing a rule that can be summarized as 'A local variable must be assgined *explicitly* before reading its value.' If you are interested in what...
1
1100
by: Jim Heavey | last post by:
Hello, I thought I was loosing my mind. I had a page which was working perfectly fine until I decided to try to add a control to a footer line in code. So I took the Validation control which was...
4
1247
by: John Salerno | last post by:
Here's my code, with the error following it: props = group1 = group2 = group3 = group4 = # Submitter: Michael Davies def all_perms(str):
1
1114
by: shara | last post by:
Hello there, Can anybody please help me with this.I've a php code in which i've commneted certain part of the code .But still i'm getting a message that there is an error in that commented part.I...
3
4275
by: christianlott1 | last post by:
I wouldn't have brought this up except that a friend had a similar problem as well. Situation: Code works fine for months, then suddenly breaks. In my case the code broke on a piece of...
2
1792
by: tawright915 | last post by:
Ok so here is my regex (--.*\n|/\*(.|\n)*?\*/). It finds all comments just fine. However I want it to return to me all strings that are not commented out. Is there a way to exclude the comments...
3
1327
by: =?Utf-8?B?UmljaGFyZA==?= | last post by:
Hi, I removed some functionality from an ASP.NET 2 webpage by deleting the input ior textbox control from the page's code. Then I commented out the JavaScript function on the same page that...
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7093
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7517
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5077
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1583
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.