473,411 Members | 2,059 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,411 software developers and data experts.

Great Gods of C++, Help A Poor Programmer

Recently, I began to write a program that demostrates memory
representation as a network of concepts. AI is a hobby of mine and I
just began to throw the program together. I didn't use the best style,
but it still should have worked. (You can guess what's coming next!)
It didn't! At the debug() call the program starts printing random
data. I am running Mac OS X 10.3 using XCode. Here is the file, I hope
it isn't too long. Thank you for your help.

#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>
using namespace std;

class Node
{
public:
Node(string repr) { rep = repr;}
void addConnection(Node * newc, bool typer) {
connections.push_back(newc); types.push_back(typer); }
string getRep() { return rep; }
void setRep(string newr) { rep = newr; }
void activate(int strength);

void debug();
private:
string rep;
vector<bool> types;
vector<Node*> connections;
};

vector<Node> memory;
bool temp;

void Node::activate(int strength)
{
cout << strength << "\t" << rep << endl;
srand(time(NULL));
int ns = strength-(1 + rand() % (memory.size()-1));
if(ns <= 0)
return;
for(int i = 0; i < connections.size(); ++i)
{
if(types[i])
cout << "Isa: ";
else
cout << "P: ";
(*connections[i]).activate(ns);
}
}

void Node::debug()
{
cout << "!DEBUGing " << rep << endl;
for(int i = 0; i < connections.size(); ++i)
{
cout << i << " " << (*connections[i]).getRep() << endl;
}
cout << "DEBUG!" << endl;
}

void addNewMem();
void actMem();
void addMem();
void gatherConnections(Node * newMemory);
void dumpMem();

int main()
{
int choice = 0;;
for(;;)
{
cout << "\nWhat to do?" << endl;
cout << " (1) Add new memory" << endl;
cout << " (2) Activate memory" << endl;
cout << " (3) Exit" << endl;
cin >> choice;
getchar();
switch(choice)
{
case 1:
addNewMem();
break;
case 2:
actMem();
break;
case 3:
cout << "Positronic Brains Coming Soon To A Store Near You!" <<
endl;
return 0;
default:
cout << "Butthead" << endl;
return 0;
}
}
return 0;
}

void addNewMem()
{
char input;
while(true)
{
addMem();
cout << "Do you want to create another memory? [yn]: ";
cin >> input;
getchar();
switch(input)
{
case 'y':
break;
case 'n':
return;
default:
return;
}
}
}

void addMem()
{
dumpMem();
string repr;
cout << "Enter stimulus: ";
getline(cin, repr);
Node newMemory(repr);
memory.push_back(newMemory);
gatherConnections(&(memory[memory.size()-1]));
dumpMem();
}

void gatherConnections(Node * newMemory)
{
char input;
char input2;
string minput;
string typs;
bool typer;
cout << "Do you wish to make connections? [yn]: ";
cin >> input2;
getchar();
switch(input2)
{
case 'y':
break;
case 'n':
return;
default:
return;
}
while(true)
{
cout << "Enter name of memory to connect: ";
getline(cin, minput);
cout << "Enter type of connection [isa/p]: ";
getline(cin, typs);
if(typs == "isa")
typer = true;
else
typer = false;
for(int i = 0; i < memory.size(); ++i)
{
if(memory[i].getRep() == minput)
{
(*newMemory).addConnection(&memory[i], typer);
memory[i].addConnection(newMemory, typer);
(*newMemory).debug();
memory[i].debug();
break;
}
}
cout << "Add another connection? [yn]: ";
cin >> input;
getchar();
switch(input)
{
case 'y':
break;
case 'n':
return;
default:
return;
}
}
}

void actMem()
{
string toAct;
cout << "Enter stimulus: ";
getline(cin, toAct);
for(int i = 0; i < memory.size(); ++i)
{
if(memory[i].getRep() == toAct)
{
cout << "Begin: ";
memory[i].activate(memory.size());
break;
}
}
}

void dumpMem()
{
for(int i = 0; i < memory.size(); ++i)
{
cout << i << " " << &memory[i] << " " << memory[i].getRep() << endl;
}
}
Jul 22 '05 #1
5 1139
Matthew wrote:
Recently, I began to write a program that demostrates memory
representation as a network of concepts. AI is a hobby of mine and I
just began to throw the program together. I didn't use the best style,
but it still should have worked. (You can guess what's coming next!)
It didn't! At the debug() call the program starts printing random
data. I am running Mac OS X 10.3 using XCode. Here is the file, I hope
it isn't too long. Thank you for your help.


.....

Hi,
I compiled the program. Running the executable, I am confronted with a
menu. Could you save me some time and tell me, how I reproduce that output
you a worried about?
Best

Kai-Uwe Bux

Jul 22 '05 #2
"Matthew" <ma******@nessiness.com> wrote in message
news:3a**************************@posting.google.c om...
Recently, I began to write a program that demostrates memory
representation as a network of concepts. AI is a hobby of mine and I
just began to throw the program together. I didn't use the best style,
but it still should have worked. (You can guess what's coming next!)
It didn't! At the debug() call the program starts printing random
data. I am running Mac OS X 10.3 using XCode. Here is the file, I hope
it isn't too long. Thank you for your help.


The biggest flaw that I see is that you used a std::vector when it was
inappropriate. Adding elements to a vector will invalidate all of the
pointers to the elements when it reallocates its internal buffer. The
standard may be even more picky about it, but that's your problem, in a
nutshell. The pointers in "vector<Node*> connections" could eventually
point to a region of memory that has been deallocated by the
std::vector<Node> memory, yielding undefined behavior that could explain
garbage output. Consider using another container for the "memory", like a
std::list<Node> or std::deque<Node>. It would probably be the easier to
transition to std::deque<Node>, since it is more like a std::vector<Node>
than std::list<Node>.

--
David Hilsee
Jul 22 '05 #3
Oops! Sorry about that.

The program basically builds a little (or big) memory repository that
can be used to demonstrate remembering or used in later problem
solving programs. In the beginning menu you can add new memories,
simulate remembering, or exit. When you choose the first option you
can enter in the stimulus, in this case the program uses text as
sensory input. For example you could enter "animal". It then asks to
make connections to other memories. Since this is the first memory
there is nothing to connect it to, so "n". It then prints a little
dubugging message about the memory location of the new memory and then
asks to make another memory. In this case we do so "y". Another
dubugging message and we repeat. To get the problem I mentioned enter
in this: (Note that there are two types of connections that are
basically the same, besides the name. There are "isa" connections or
"type of" and "p" connections or "property of". )

What to do?
(1) Add new memory
(2) Activate memory
(3) Exit
1
Enter stimulus: animal
Do you wish to make connections? [yn]: n
0 0x1800e00 animal
Do you want to create another memory? [yn]: y
0 0x1800e00 animal
Enter stimulus: fish
Do you wish to make connections? [yn]: y
Enter name of memory to connect: animal
Enter type of connection [isa/p]: isa
!DEBUGing fish (More debugging: basically looks at an
object and reports the connections)
0 animal
DEBUG!
!DEBUGing animal
0 fish
DEBUG!
Add another connection? [yn]: n
0 0x1801120 animal
1 0x1801144 fish
Do you want to create another memory? [yn]: y
0 0x1801120 animal
1 0x1801144 fish
Enter stimulus: swim
Do you wish to make connections? [yn]: y
Enter name of memory to connect: fish
Enter type of connection [isa/p]: p
!DEBUGing swim
0 fish
DEBUG!
!DEBUGing fish
((((The messed up data has been deleted as google groups doesn't like
it))))
^^^^^^^^^^^^^^^^^
uhoh
Here is the problem. Somehow the vector that contains the connections
in "fish" is corrupted. The same thing happens if we delete the calls
to debug() and activate a memory with the second menu option. (For
reference activating means you conceptualize the memory then activate
or "prime" memories connected. The farther from the source memory the
weaker the activation, until there is no more left.)

I hope this helps.

Kai-Uwe Bux <jk********@gmx.net> wrote in message news:<cg**********@news01.cit.cornell.edu>...
Matthew wrote:
Recently, I began to write a program that demostrates memory
representation as a network of concepts. AI is a hobby of mine and I
just began to throw the program together. I didn't use the best style,
but it still should have worked. (You can guess what's coming next!)
It didn't! At the debug() call the program starts printing random
data. I am running Mac OS X 10.3 using XCode. Here is the file, I hope
it isn't too long. Thank you for your help.


....

Hi,
I compiled the program. Running the executable, I am confronted with a
menu. Could you save me some time and tell me, how I reproduce that output
you a worried about?
Best

Kai-Uwe Bux

Jul 22 '05 #4
Matthew wrote:
Oops! Sorry about that.

The program basically builds a little (or big) memory repository that
can be used to demonstrate remembering or used in later problem
solving programs. In the beginning menu you can add new memories,
simulate remembering, or exit. When you choose the first option you
can enter in the stimulus, in this case the program uses text as
sensory input. For example you could enter "animal". It then asks to
make connections to other memories. Since this is the first memory
there is nothing to connect it to, so "n". It then prints a little
dubugging message about the memory location of the new memory and then
asks to make another memory. In this case we do so "y". Another
dubugging message and we repeat. To get the problem I mentioned enter
in this: (Note that there are two types of connections that are
basically the same, besides the name. There are "isa" connections or
"type of" and "p" connections or "property of". )

What to do?
(1) Add new memory
(2) Activate memory
(3) Exit
1
Enter stimulus: animal
Do you wish to make connections? [yn]: n
0 0x1800e00 animal
Do you want to create another memory? [yn]: y
0 0x1800e00 animal
Enter stimulus: fish
Do you wish to make connections? [yn]: y
Enter name of memory to connect: animal
Enter type of connection [isa/p]: isa
!DEBUGing fish (More debugging: basically looks at an
object and reports the connections)
0 animal
DEBUG!
!DEBUGing animal
0 fish
DEBUG!
Add another connection? [yn]: n
0 0x1801120 animal
1 0x1801144 fish
Do you want to create another memory? [yn]: y
0 0x1801120 animal
1 0x1801144 fish
Enter stimulus: swim
Do you wish to make connections? [yn]: y
Enter name of memory to connect: fish
Enter type of connection [isa/p]: p
!DEBUGing swim
0 fish
DEBUG!
!DEBUGing fish
((((The messed up data has been deleted as google groups doesn't like
it))))
^^^^^^^^^^^^^^^^^
uhoh
Here is the problem. Somehow the vector that contains the connections
in "fish" is corrupted. The same thing happens if we delete the calls
to debug() and activate a memory with the second menu option. (For
reference activating means you conceptualize the memory then activate
or "prime" memories connected. The farther from the source memory the
weaker the activation, until there is no more left.)

I hope this helps.


It does: when I try the sequence above, I get a segfault -- usually that
indicates access to memory that has been returned. As David Hilsee has
pointed out, this is a result of the re(al)location of std::vector<Node> as
it grows. I replaced the line

vector<Node> memory;

by

deque<Node> memory;

and now, it seems to work. The standard [23.2.1.3] requires that inserts at
the ends of a deque are not supposed to invalidate references to elements
of the deque although iterators are invalidated. The std::list<> container
would be safer in that iterators and references are most stable, but since
you are using the size() method it might not be the best choice for your
problem.
Best

Kai-Uwe Bux
Jul 22 '05 #5
Thanks for your help, guys!
"David Hilsee" <da*************@yahoo.com> wrote in message news:<M7********************@comcast.com>...
"Matthew" <ma******@nessiness.com> wrote in message
news:3a**************************@posting.google.c om...
Recently, I began to write a program that demostrates memory
representation as a network of concepts. AI is a hobby of mine and I
just began to throw the program together. I didn't use the best style,
but it still should have worked. (You can guess what's coming next!)
It didn't! At the debug() call the program starts printing random
data. I am running Mac OS X 10.3 using XCode. Here is the file, I hope
it isn't too long. Thank you for your help.


The biggest flaw that I see is that you used a std::vector when it was
inappropriate. Adding elements to a vector will invalidate all of the
pointers to the elements when it reallocates its internal buffer. The
standard may be even more picky about it, but that's your problem, in a
nutshell. The pointers in "vector<Node*> connections" could eventually
point to a region of memory that has been deallocated by the
std::vector<Node> memory, yielding undefined behavior that could explain
garbage output. Consider using another container for the "memory", like a
std::list<Node> or std::deque<Node>. It would probably be the easier to
transition to std::deque<Node>, since it is more like a std::vector<Node>
than std::list<Node>.

Jul 22 '05 #6

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

Similar topics

11
by: John Wellesz | last post by:
Hello, It would be great if there was an option to tell PHP to let the user manage all the HTTP headers instead of sending what it thinks is good for the programmer... For example when you...
0
by: Bill Law | last post by:
Great Company on the Maryland Eastern Shore needs a DB2 Systems Programmer and DBA (DB2) to join their IT team. The selected candidate MUST have a BS degree and both DB2 DBA And DB2 Systems...
32
by: Tom Cole | last post by:
I bet 50% of the posts I've read lately have had at least one bad thing to say about every website or book dedicated to javascript. There are clearly a few posters (you know who you are) who...
42
by: gt8887b | last post by:
Hello! In his recent newsletter embedded expert Jack Ganssle says that programming students, as well as professional developers should readh "great code" (hight quality/well-crafted code that...
0
by: Doug.Fort | last post by:
http://scienceblogs.com/insolence/2006/12/pythons_were_the_oldest_gods.php -- Doug Fort, Consulting Programmer http://www.dougfort.com
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.