Thread: Dynamically allocating memory...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434

    Dynamically allocating memory...

    So i wrote a function which creates a circularly linked list. But for some reason it cant allocate memory dynamically =(

    What am i doin wrong?

    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
    	node *nodes[size];
    	for(size; size>0; size--)
    	{
    		nodes[size] = new node;
    	}
    	size = v.size();
    	size--;
    	//Insert Data
    	for(int i=0; i<size-1; i++)
    	{
    		nodes[i]->data = v.at(i);
    	}
    	//Connect nodes
    	circ->start = start;
    	start->next = nodes[0];
    	for(int i=1; i<size-1; i++)
    	{
    		nodes[i-1]->next = nodes[i];
    	}
    	//do prevs
    	start->prev = nodes[size-1];
    	for(size-2; size>0; size--)
    	{
    		nodes[size]->prev = nodes[size-1];
    	}
    	//DONE
    }
    Thanks!! =D
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    There's quite a few things wrong with that code, and the best suggestion I can think of is go back and review from whatever source you're using to learn this stuff.

    Code:
    first = new circle;
    This would work if "first" was a pointer

    Code:
    int size = v.size();
    	size--;
    	//data
    	node *nodes[size];
    This is nonstandard. The size of an array needs to be a constant value, or you need to dynamically allocate the array. Besides, it kind of defeats the purpose of a list to allocate an array, then copy its contents into the list.

    And if you're using std::vector for the data anyway, it seems pointless to make your own container

    Finally, you need to have a delete for each new, or you'll leak memory
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    I'm not using source, i'm doing this myself.

    Well i fixed the pointer thing, that was just a stupid error on my part, that's what i meant to type. I'm not going to worry about the memory leak just yet until i can get the memory allocated in the first place

    How do i dynamically allocate the memory then?

    The circular linked list object is the whole purpose of the program and the computer needs to be able to dynamically allocate the memory. So this is just my concept of doing it, can you even allocate the memory dynamically? Should i create a vector instead of an array for the nodes pointers?

    I'll try it
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  4. #4
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    And i just get more errors with using a vector =P

    How can i get this thing to work?
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  5. #5
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    I'm sure SOMEONE has to have faced this problem before...

    i would think this sorta thing takes place perdy often in programming...


    Anyone?

    Bitteschon?

    =D
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Since I really cannot follow what you're trying to do I can only try to give you a few hints why your program crashes.
    Code:
    	int size = v.size();
    	size--;
    	//data
    	node *nodes[size];
    	for(size; size>0; size--)
    	{
    		nodes[size] = new node;
    	}
    you have never created node[0].
    Code:
    for(int i=0; i<size-1; i++)
    	{
    		nodes[i]->data = v.at(i);
    	}
    boom. you dereference the nonexistant node[0].
    Code:
    	for(int i=1; i<size-1; i++)
    	{
    		nodes[i-1]->next = nodes[i];
    	}
    again you dereference node[0].
    Code:
    	start->next = nodes[0];
    start->next points to ????
    Code:
    	for(size-2; size>0; size--)
    	{
    		nodes[size]->prev = nodes[size-1];
    	}
    and again.
    Kurt

  7. #7
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Okay so i gotta change the size>0 to size>=0, then what?

    I'm trying to allocate a dynamic amount of memory for the structures defined at the top of the page. The user could enter or the program could choose to enter a certain amount of nodes, the function has to be able to adapt to that.


    THanks!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You might want to have a look at this tutorial: http://www.cprogramming.com/tutorial/lesson15.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Yeah i how to setup and traverse the lists and all, and i fixed the mistakes mentioned earlier, but i still get the same errors. The problem is that i need to dynamically allocate it and i cant figure out how to =(

    Thanks everyone, and DWKS, always a help! =D
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  10. #10
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i havent been following this thread but from the looks of it youve had feedback on your code. can you post the updated version (if any) of your code?

    you said you get 'errors'.. you mean actual errors, ie runtime/compiletime errors? if so what are they?

  11. #11
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    If you want to create an array of nodes in runtime, you'd probably have to
    Code:
    node** nodes;
    nodes = new node*[size];
    rather than
    Code:
    node *nodes[size]
    As previously mentioned, you can't STATICALLY define an array with a variable, which is what you're doing.
    Last edited by cunnus88; 05-04-2007 at 08:31 PM.

  12. #12
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    alrighty, i gotta get out my other computer, i'll post it up in a jiff =)
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  13. #13
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    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
    	node *nodes[size];
    	for(size; size>=0; size--)
    	{
    		nodes[size] = new node;
    	}
    	size = v.size();
    	size--;
    	//Insert Data
    	for(int i=0; i<size-1; i++)
    	{
    		nodes[i]->data = v.at(i);
    	}
    	//Connect nodes
    	circ->start = start;
    	start->next = nodes[0];
    	for(int i=1; i<size-1; i++)
    	{
    		nodes.[i-1]->next = nodes[i];
    	}
    	//do prevs
    	start->prev = nodes[size-1];
    	for(size-2; size>=0; size--)
    	{
    		nodes[size]->prev = nodes[size-1];
    	}
    	//DONE
    }
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    3:53 is a jiff?

    The first thing I noticed with your code is that you're passing a vector by value. Usually it's a much better idea to pass STL containers as references or pointers because it can be quite expensive to copy them.

    When I compile your code, I get two warnings and one error. Here's the error:
    Code:
    nodes.[i-1]->next = nodes[i];
    It seems you have an extra '.'.

    The warnings are on these lines:
    Code:
    	for(size; size>=0; size--)
    	for(size-2; size>=0; size--)
    The first part of those for loops does nothing and can be left out.

    Once I removed the bothersome '.', your program compiled correctly and ran without segfaulting.

    [edit] I ran your program quickly though GDB.
    Code:
    GNU gdb 5.2.1
    Copyright 2002 Free Software Foundation, Inc.
    GDB is free software, covered by the GNU General Public License,
    welcome to change it and/or distribute copies of it under certain
    Type "show copying" to see the conditions.
    There is absolutely no warranty for GDB.  Type "show warranty" fo
    This GDB was configured as "i686-pc-mingw32"...
    (gdb) break 33
    Breakpoint 1 at 0x401549: file tempjunior2.cpp, line 33.
    (gdb) r
    Starting program: tempjunior2.exe
    
    Breakpoint 1, main () at tempjunior2.cpp:33
    33              cin.ignore();
    (gdb) p first
    $1 = (circle *) 0x3d24b0
    (gdb) p *$
    $2 = {start = 0x3d25e0}
    (gdb) p $.start
    $3 = (node *) 0x3d25e0
    (gdb) p *$
    $4 = {data = 1, next = 0x3d2770, prev = 0x3d2630}
    (gdb) p $.next
    $5 = (node *) 0x3d2770
    (gdb) p *$
    $6 = {data = 1, next = 0x3d2748, prev = 0x411c23}
    (gdb) p *$->next
    $7 = {data = 2, next = 0x3d2720, prev = 0x3d2770}
    (gdb) p *$->next
    $8 = {data = 3, next = 0x3d26f8, prev = 0x3d2748}
    (gdb) p *$->next
    $9 = {data = 4, next = 0x3d26d0, prev = 0x3d2720}
    (gdb) p *$->prev
    $10 = {data = 3, next = 0x3d26f8, prev = 0x3d2748}
    (gdb) p *$->next
    $11 = {data = 4, next = 0x3d26d0, prev = 0x3d2720}
    (gdb) p *$->next
    $12 = {data = 5, next = 0x3d26a8, prev = 0x3d26f8}
    (gdb) p *$->next
    $13 = {data = 6, next = 0x3d2680, prev = 0x3d26d0}
    (gdb) p *$->next
    $14 = {data = 7, next = 0x3d2658, prev = 0x3d26a8}
    (gdb) p *$->next
    $15 = {data = 8, next = 0xbaadf00d, prev = 0x3d2680}
    (gdb) p *$->next
    $16 = {data = 1768763235, next = 0x3377676e, prev = 0x2e332f32}
    (gdb) p *$->next
    Cannot access memory at address 0x3377676e
    (gdb)
    It looks like your loop was created correctly up until the element with data 8.

    Looking at your code again, I would guess that that is because of this:
    Code:
    	size = v.size();
    	size--;
    	//Insert Data
    	for(int i=0; i<size-1; i++)
    You're not looping as far as maybe you could. With 10 elements in v, the loop executes with i from 0 to 7, inclusive.

    Also, don't forget to terminate the last link with pointers to NULL or to the list's head. [/edit]
    Last edited by dwks; 05-05-2007 at 02:49 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Thanks, that little '.' musta got in there while i was copying and pasting =P

    Haha, well it still won't compile for me (MSVC++ Express).

    Problem is with the line:
    Code:
    node *nodes[size]
    =(

    I get 3 errors with that line. But other than that the program seems to be alright.
    "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