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
    Posts
    5,439
    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:
    int main(void){srand(time(0));for(double l=rand(),l0=0,l00=0;;l0+=0.1){for(double l000=0;l000
    <1;l000+=.001,l+=((double)rand()/RAND_MAX)/0x64,l00+=((sin(l*0x8*atan(l0)*l000-(l0*0x8*atan
    (l)))*0.5)+0.5)){l00-=floor(l00);for(size_t l0000=0,l00000=(size_t)(0x50*(l00));l0000<l00000;++l0000
    )putchar(0x20);putchar(0x61+(int)((double)rand()/RAND_MAX*0x1a));putchar('\n');}}return 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
    Posts
    5,439
    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:
    int main(void){srand(time(0));for(double l=rand(),l0=0,l00=0;;l0+=0.1){for(double l000=0;l000
    <1;l000+=.001,l+=((double)rand()/RAND_MAX)/0x64,l00+=((sin(l*0x8*atan(l0)*l000-(l0*0x8*atan
    (l)))*0.5)+0.5)){l00-=floor(l00);for(size_t l0000=0,l00000=(size_t)(0x50*(l00));l0000<l00000;++l0000
    )putchar(0x20);putchar(0x61+(int)((double)rand()/RAND_MAX*0x1a));putchar('\n');}}return 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
    Posts
    5,439
    >> // 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:
    int main(void){srand(time(0));for(double l=rand(),l0=0,l00=0;;l0+=0.1){for(double l000=0;l000
    <1;l000+=.001,l+=((double)rand()/RAND_MAX)/0x64,l00+=((sin(l*0x8*atan(l0)*l000-(l0*0x8*atan
    (l)))*0.5)+0.5)){l00-=floor(l00);for(size_t l0000=0,l00000=(size_t)(0x50*(l00));l0000<l00000;++l0000
    )putchar(0x20);putchar(0x61+(int)((double)rand()/RAND_MAX*0x1a));putchar('\n');}}return 0;}

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Posts
    5,439
    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:
    int main(void){srand(time(0));for(double l=rand(),l0=0,l00=0;;l0+=0.1){for(double l000=0;l000
    <1;l000+=.001,l+=((double)rand()/RAND_MAX)/0x64,l00+=((sin(l*0x8*atan(l0)*l000-(l0*0x8*atan
    (l)))*0.5)+0.5)){l00-=floor(l00);for(size_t l0000=0,l00000=(size_t)(0x50*(l00));l0000<l00000;++l0000
    )putchar(0x20);putchar(0x61+(int)((double)rand()/RAND_MAX*0x1a));putchar('\n');}}return 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