LinuxQuestions.org
Register a domain and help support LQ
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-28-2010, 07:05 AM   #1
primenu
LQ Newbie
 
Registered: Oct 2009
Posts: 13
Blog Entries: 1

Rep: Reputation: 0
Post std::bad_alloc' what(): St9bad_alloc


Hi, I have a program suffering from bad memory allocation :

typedef struct packet {
int source;
int destination;
int data;
nt type;// Head =1, Body = 0, Tail = -1
flits *next;//For the next trail flits in the packets.
Tail->next = NULL
};

class node{
packet *head,*body, *tail;
public:
list<packet *>buffer_queue;
void update_node(double, string, int, int);
void pkt_Received();
};

void node:: pkt_Received(){
packet *previous, *next;
previous = received_queue.front();
received_queue.pop_front();
while( previous->next!=NULL )
{
next = received_queue.front();
received_queue.pop_front();
if(previous->next != next)
{
cout<<"\nError While Receiving Data at Node :["<<Xpos<<"]["<<Ypos<<"]";
exit(0);
}
delete previous;
previous = next;
}
Msg_Received++;
}
void node::update_node(int Destination, int msg_size)
{
head = new packet();
tail = new packet();
head->next = tail;
head->type = 1;
//Also the packet contains fields to specify the destination

while(i <= (msg_size / pkt_size))
{
body = new packet();
body->type = 0;
if(i == 1)
{
body->next = head->next;
head->next = body;
previous = body;
}else
{

body->next = previous->next;
previous->next = body;
previous = body;
}
buffer_queue.push_back(body);
i++;
}
buffer_queue.push_back(tail);

while(! buffer_queue.empty())
{

if(OutChannel.size() < buffer_size) //here OutChannel is a list for structure pointers: packets.
pkt = buffer_queue.front();
//Here depending upon the type of the packetype, and the channel availability, they are sent to destination nodes. The pkt_Received method is called upon by the destination node when it receives the packets, here the whole linked list.
buffer_queue.pop_front();
}else
{
//Packet Dropped: Output Channel Occupied,Wait for the Next Round
break;
}
}
}
:The Problem is I get "terminate called after throwing an instance of 'std::bad_alloc' what(): St9bad_alloc " error when I try to create huge numbers of packets in my program, I am using Message-Size from the trace file to determine the numbers of packets to be created for each object node, I am creating a mesh of 16 nodes here, which communicate to each other.
With small number of messages the program runs fine. I am not sure if
"delete previous" is the right way to free the memory..

Last edited by primenu; 01-29-2010 at 05:38 AM.
 
Old 01-29-2010, 04:50 AM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,368
Blog Entries: 52

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Without looking at the code in too much depth:

The declarations in class node:
Code:
packet *head,body, tail;
Were these all supposed to be pointers?, eg
Code:
packet *head, *body, *tail;
Also, the last else statement doesn't seem to have a preceding if statement.

Incidentally, when forum posting sections of code, CODE tags make it easier to read.

Last edited by neonsignal; 01-29-2010 at 04:51 AM.
 
Old 01-29-2010, 05:30 AM   #3
primenu
LQ Newbie
 
Registered: Oct 2009
Posts: 13
Blog Entries: 1

Original Poster
Rep: Reputation: 0
Yes, in the code all these are structure pointers
packet *head, *body, *tail;
I am sorry for the incomplete code.
I was trying to cut my code to make it readable, so I missed the preceding if statement.
 
Old 01-29-2010, 06:12 AM   #4
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,368
Blog Entries: 52

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
I'm assuming this is a single threaded app, because the STL list is not thread-safe.

Using 'delete previous' to delete the packets is fine (assuming they were all created with new in the first place).

I notice that pkt_Received doesn't check if the queue is empty, presumably it is only invoked after a message comes in.

In update_node, the type of the tail node is never set (but since it isn't used in this code fragment, I don't know if that is significant).

Also, the counter i is never declared or initialized. I'm guessing it is initialized to 1.

The head packet is never pushed into the list either, which doesn't look quite right.

It is worth running your application using valgrind to check for new/delete issues.

Last edited by neonsignal; 01-29-2010 at 06:13 AM.
 
  


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
32-bit executable getting st9bad_alloc @ 3GB mem allocation Juan234 Programming 3 01-14-2010 07:51 PM
what does the std:: do? RHLinuxGUY Programming 1 11-15-2005 02:01 PM
Std C++ Thaith Programming 7 08-28-2005 06:45 AM
Kicker needs more RAM? St9bad_alloc sloik2000 Linux - Software 5 05-10-2005 12:49 PM
Knoppix Std OMEGA-DOOM Linux - Distributions 2 06-28-2004 03:37 PM


All times are GMT -5. The time now is 04:41 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration