Thread: Dynamically allocating memory...

  1. #16
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    That has beem pointed out earlier. (#2, #11). This works for g++ only.
    You have to use
    Code:
    node *nodes = new node*[size];
    Kurt

  2. #17
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by ZuK View Post
    That has beem pointed out earlier. (#2, #11). This works for g++ only.
    You have to use
    Code:
    node *nodes = new node*[size];
    Kurt
    And what do you want to allocate here? array of structs or array of pointers?

    It should be or

    Code:
    node *nodes = new node[size];
    or
    Code:
    node **nodes = new node*[size];
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #18
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It should be
    Code:
    std::vector<node*> nodes(size);
    I can't think of a good reason to use a dynamic array here.

    If you are getting errors with the vector, post your attempt and the errors you get.

  4. #19
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    alrighty, i'll give it a shot
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  5. #20
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    It compiled!!!


    But then it had a runtime error, segfault =( it didn't give me a readout of the variables though like it normally does, it pointed me to the vector header file. =(

    Here it is:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    struct node
    {
    	int data;
    	node *next;
    	node *prev;
    };
    
    struct circle
    {
    	node *start;
    };
    
    void createCircle(circle *circ, vector<int> &v);
    
    int main()
    {
    	//Main
    	circle *first;
    	first = new circle;
    	vector<int> numbers;
    	for(int i=0; i<10; i++)
    	{
    		numbers.push_back(i+1);
    	}
    	createCircle(first, numbers);
    	cin.ignore();
    	//End
    	return 0;
    }
    
    void createCircle(circle *circ, vector<int> &v)
    {
    	//create intitial node
    	node *start;
    	start = new node;
    	start->data = v.at(0);
    	//create the rest of the nodes
    	int size = v.size();
    	size--;
    	//data
    	vector<node*> nodes(size);
    	for(size; size>=0; size--)
    	{
    		nodes.at(size) = new node;
    	}
    	size = v.size();
    	size--;
    	//Insert Data
    	for(int i=0; i<size-1; i++)
    	{
    		nodes.at(i)->data = v.at(i);
    	}
    	//Connect nodes
    	circ->start = start;
    	start->next = nodes.at(0);
    	for(int i=1; i<size-1; i++)
    	{
    		nodes.at(i-1)->next = nodes.at(i);
    	}
    	//do prevs
    	start->prev = nodes.at(size-1);
    	for(size-2; size>=0; size--)
    	{
    		nodes.at(size)->prev = nodes.at(size-1);
    	}
    	//DONE
    }
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  6. #21
    Registered User
    Join Date
    May 2007
    Posts
    88
    Code:
    vector<node*> nodes(size);
    for(size; size>=0; size--)
    {
    	nodes.at(size) = new node;
    }
    Here nodes.at() throws an out of range exception. Not really much reason to use at() if you're not going to handle the exceptions.

  7. #22
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Not really much reason to use at() if you're not going to handle the exceptions.
    Actually, there is in that it will throw an exception every time instead of having undefined behavior which might silently break something. Even if you don't handle the exception it still indicates why the "crash" occured. It's actually probably better to always use at() unless you have some specific efficiency issue. Most people use operator[] because they are just used to array syntax (or are prematurely optimizing).

    In this case, nodes.at(size) is out of bounds. Valid indexes are 0 to size-1. You'll want to start looping at size-1. I'd also use a separate loop variable instead of re-using size for this.

  8. #23
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Personally I find at() a little obscure-looking (in addition to being longer), and prefer to use it only during debugging, if only for that reason.

  9. #24
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    well i know at() uses bounds checking which is why i prefer it as do most others, yeah it's obscure but if you were used to using .at() [] might look obscure too haha
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  10. #25
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    okay, so i used a new variable and all and it stills throws the exception =(

    Code:
    size = v.size();
    size--;
    for(int i=0; i < size; i++)
    {
         nodes.at(i) = new node;
    }
    ...
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  11. #26
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Are you sure it is throwing the exception there? You use at() several times, it is possible that it is being called from a different line of code.

  12. #27
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Yeah its throwing an unhandled exception and if i click continue then an assertion error, the part it is showing me is in the vector.h file, _XRan or something, out of range memory location
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  13. #28
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Right, but where in your code is the exception being thrown? Look down the call stack until you see some of your own code, and identify the call to at() that is causing the problem. You can also add cout statements between each for loop to see how many cause the error.

    You can also post more complete current code to make sure you implemented the changes properly.

  14. #29
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    alrighty, i'll give it a shot, thanks for the help!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 60
    Last Post: 01-09-2009, 01:09 PM
  2. Dynamically allocated memory
    By ^xor in forum Linux Programming
    Replies: 9
    Last Post: 06-28-2005, 11:42 AM
  3. allocating memory in constructor
    By Micko in forum C++ Programming
    Replies: 3
    Last Post: 08-25-2004, 07:45 AM
  4. Checking if memory has been dynamically allocated
    By Xzyx987X in forum C Programming
    Replies: 28
    Last Post: 03-14-2004, 06:53 PM
  5. Replies: 5
    Last Post: 11-24-2002, 11:33 PM