Can I dynamically allocate std::queue?

This is a discussion on Can I dynamically allocate std::queue? within the C++ Programming forums, part of the General Programming Boards category; I'm having problems with templates blah blah blah.. lack of understanding.. confusion etc etc. I want to create a queue ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    24

    Can I dynamically allocate std::queue?

    I'm having problems with templates blah blah blah.. lack of understanding.. confusion etc etc.

    I want to create a queue object from the Standard Template Library for a type of my own classes. I've got it to compile but I get a segmentaion fault whenever I try and do anything with the instantiated queue. Even if I try to make a std::queue of int I get a segmentation fault. This is what I have done :

    header:
    Code:
    std::queue<int,std::deque<int> >* intQueue;
    constructor:
    Code:
    std::queue<int,std::deque<int> >* intQueue = new std::queue<int,std::deque<int> >();
    And finally in an instance method where I want to do something with the queue:
    Code:
    intQueue->push(dcnt); // This gives segmentation fault
    Whatever I do with the queue intQueue, I get a segmentation fault. I can't seem to find examples of how to allocated a template on the HEAP, all text books just show it on the STACK. Is it possible to allocate on the HEAP and if so what am I doing wrong ?

    I'm using gcc under Linux (Ubuntu)

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    Quote Originally Posted by CodeBugs View Post
    I'm having problems with templates blah blah blah.. lack of understanding.. confusion etc etc.

    I want to create a queue object from the Standard Template Library for a type of my own classes. I've got it to compile but I get a segmentaion fault whenever I try and do anything with the instantiated queue. Even if I try to make a std::queue of int I get a segmentation fault. This is what I have done :

    header:
    Code:
    std::queue<int,std::deque<int> >* intQueue;
    constructor:
    Code:
    std::queue<int,std::deque<int> >* intQueue = new std::queue<int,std::deque<int> >();
    And finally in an instance method where I want to do something with the queue:
    Code:
    intQueue->push(dcnt); // This gives segmentation fault
    Whatever I do with the queue intQueue, I get a segmentation fault. I can't seem to find examples of how to allocated a template on the HEAP, all text books just show it on the STACK. Is it possible to allocate on the HEAP and if so what am I doing wrong ?

    I'm using gcc under Linux (Ubuntu)
    Looks okay. Can you post a compilable example that crashes?
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    Is there a specific reason you want to put it on the heap? The queue is dynamic by nature since it is managed by the STL.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    24
    Quote Originally Posted by Sebastiani View Post
    Looks okay. Can you post a compilable example that crashes?
    OK:

    template.h:
    Code:
    //#include <stdio>
    #include <queue>
    #include <deque>
    
    class Test {
    	public:
    	Test();
    	void pushANumber(int num);
    	private:
    	std::queue<int,std::deque<int> > *intQueue;
    };
    template.cpp:
    Code:
    #include "template.h"
    
    Test::Test (){
    	std::queue<int,std::deque<int> > *intQueue = new std::queue<int,std::deque<int> >;
    }
    
    void Test::pushANumber(int num){
    	intQueue->push(num);
    }
    
    int main (int argc, char** argv){
    	Test *t = new Test();
    	t->pushANumber(5);
    	return 0;
    }
    This is just a simple example of what I'm trying to achieve and it also gives me segmentation fault. compiled with g++ :

    g++ -o template template.cpp

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    Quote Originally Posted by CodeBugs View Post
    OK:

    template.h:
    Code:
    //#include <stdio>
    #include <queue>
    #include <deque>
    
    class Test {
        public:
        Test();
        void pushANumber(int num);
        private:
        std::queue<int,std::deque<int> > *intQueue;
    };
    template.cpp:
    Code:
    #include "template.h"
    
    Test::Test (){
        std::queue<int,std::deque<int> > *intQueue = new std::queue<int,std::deque<int> >;
    }
    
    void Test::pushANumber(int num){
        intQueue->push(num);
    }
    
    int main (int argc, char** argv){
        Test *t = new Test();
        t->pushANumber(5);
        return 0;
    }
    This is just a simple example of what I'm trying to achieve and it also gives me segmentation fault. compiled with g++ :
    You're declaring and initializing a local variable "intQueue". The constructor should be:

    Code:
    Test::Test (){
        intQueue = new std::queue<int,std::deque<int> >;
    }
    Also, you need to "delete" what you "new", and furthermore, this class isn't safe to copy-construct or assign (eg: multiple objects would attempt to manage the same memory location), so disable them or else use a "smart-pointer".
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    24
    Well I managed to get it to work with the following alterations:

    template.h
    Code:
    #include <cstdio>
    #include <queue>
    #include <deque>
    
    class Test {
    	public:
    	Test();
    	void pushANumber(int num);
    	private:
    	std::queue<int,std::deque<int> > *intQueue;
    	std::queue<int,std::deque<int> > secondQueue; //No longer use a pointer
    };

    template.cpp
    Code:
    Test::Test (){
            // No longer need to do this
    	std::queue<int,std::deque<int> > *intQueue = new std::queue<int,std::deque<int> >;
    }
    
    void Test::pushANumber(int num){
    	//intQueue->push(num);
    	secondQueue.push(5);
    }
    
    int main (int argc, char** argv){
    	Test *t = new Test();
    	printf("This is some rubbish.\n");
    	//t->pushANumber(5);
    	t->pushANumber(6);
    	printf("This is some rubbish.\n");
    	return 0;
    }
    This is my misunderstanding of how the memory is allocated. I needed a queue created for every instance of my class I instantiate with 'new'. I assumed that a template is seen as an object which can be allocated HEAP memory or dynamic memory. It must be that a pointer to the Template is allocated on the stack, which points to the heap. I can live with that just as long as I get a new queue for each instantiation.. which looks as though I do when I inspect using a debugger.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    >> // No longer need to do this

    Let me be a little clearer.

    Code:
    std::queue<int,std::deque<int> > *intQueue = new std::queue<int,std::deque<int> >;
    Here, "intQueue" refers to a new, local variable. Not the member! So just get rid of the declaration part.

    >> This is my misunderstanding of how the memory is allocated. I needed a queue created for every instance of my class I instantiate with 'new'. I assumed that a template is seen as an object which can be allocated HEAP memory or dynamic memory. It must be that a pointer to the Template is allocated on the stack, which points to the heap. I can live with that just as long as I get a new queue for each instantiation.. which looks as though I do when I inspect using a debugger.

    It rarely matters whether stack or heap variables are used. Both typically work fine (although the latter is arguably more error-prone). I'd say stick with the stack unless it's just completely unavoidable.
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    This is all you really need, BTW.

    Code:
    #include <queue>
    #include <deque>
    
    class Test {
        public:
        void pushANumber(int num);
        private:
        std::queue<int,std::deque<int> > intQueue;
    };
    
    void Test::pushANumber(int num){
        intQueue.push(num);
    }
    
    int main (int argc, char** argv){
        Test t;
        t.pushANumber(5);
        return 0;
    }
    In this case, you don't have to worry about the constructor, copy-constructor, or assignment operators. Much easier, no?
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  9. #9
    Registered User
    Join Date
    Jun 2009
    Posts
    24
    Quote Originally Posted by Sebastiani View Post
    You're declaring and initializing a local variable "intQueue". The constructor should be:

    Code:
    Test::Test (){
        intQueue = new std::queue<int,std::deque<int> >;
    }

    Also, you need to "delete" what you "new", and furthermore, this class isn't safe to copy-construct or assign (eg: multiple objects would attempt to manage the same memory location), so disable them or else use a "smart-pointer".
    D'oh !

    Thanks Sebastiani

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamically allocate 2D array of ints
    By steals10304 in forum C Programming
    Replies: 3
    Last Post: 10-24-2009, 08:31 PM
  2. Replies: 5
    Last Post: 03-23-2009, 03:44 PM
  3. allocate memory dynamically
    By rahulsk1947 in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 04:30 PM
  4. Dynamically allocate a pointer?
    By theJ89 in forum C++ Programming
    Replies: 3
    Last Post: 04-09-2006, 02:03 PM
  5. Dynamically allocate size of array for strings
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 05-04-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21