Thread: Contiguous Array version of Linked List

  1. #16
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Assuming there is already something in the array. It should cycle through the for loop, copy the element into the adjacent spot, then write over the spot directly infront of position..
    Need I say it again? Get rid of that *** +1!!!

    Here's what's happening. You get an array like this:
    Code:
    position = 3
    element_count = 4
    elementArray = {1, 2, 3, 4, x, x, ...}
    You shift 4 over, since it is the only element >position.
    Code:
    elementArray = {1, 2, 3, 4, 4, x, ...}
    And then you assign to elementArray[position+1].
    Code:
    elementArray = {1, 2, 3, 4, 123, x, ...
    While that works, I'm sure you intended to have
    Code:
    elementArray = {1, 2, 3, 123, 4, x, ...
    which is what you get without that +1.

    Just so you know, if you access uninitialized memory or whatever, you're not guaranteed to get a segmentation fault. (Though on Linux your chances of getting a seg fault are very high.) So just because it runs doesn't mean that it doesn't access uninitialized memory.

    Segmentation faults are good, by the way. At least they tell you that something's wrong. There's nothing worse than discovering a bug in code that you thought was solid, tested code . . . and then you think "Why on earth was that code working at all???" and then you have to modify tons of code because all of your code was assigning 1 to position instead of 0 . . .
    Last edited by dwks; 10-07-2007 at 02:26 AM.
    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.

  2. #17
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    [See the edits to my previous post, they're extensive.]

    Oh, by the way, don't use gets(). Among other things . . . Food for thought:
    Code:
    $ gcc -W -Wall -ansi -pedantic -g -c *.c
    Vector.c: In function ‘vectorCreate’:
    Vector.c:40: warning: control reaches end of non-void function
    Vector.c: At top level:
    Vector.c:33: warning: unused parameter ‘size’
    Vector.c: In function ‘vectorDelete’:
    Vector.c:68: warning: control reaches end of non-void function
    Vector.c: At top level:
    Vector.c:61: warning: unused parameter ‘this’
    Vector.c: In function ‘vectorSum’:
    Vector.c:95: warning: control reaches end of non-void function
    Vector.c: At top level:
    Vector.c:88: warning: unused parameter ‘result’
    Vector.c:88: warning: unused parameter ‘src1’
    Vector.c:88: warning: unused parameter ‘src2’
    Vector.c: In function ‘vectorDiff’:
    Vector.c:122: warning: control reaches end of non-void function
    Vector.c: At top level:
    Vector.c:115: warning: unused parameter ‘result’
    Vector.c:115: warning: unused parameter ‘src1’
    Vector.c:115: warning: unused parameter ‘src2’
    Vector.c: In function ‘vectorScalarProd’:
    Vector.c:148: warning: control reaches end of non-void function
    Vector.c: At top level:
    Vector.c:141: warning: unused parameter ‘result’
    Vector.c:141: warning: unused parameter ‘src’
    Vector.c:141: warning: unused parameter ‘scalar’
    Vector.c: In function ‘vectorInnerProd’:
    Vector.c:175: warning: control reaches end of non-void function
    Vector.c: At top level:
    Vector.c:168: warning: unused parameter ‘src1’
    Vector.c:168: warning: unused parameter ‘src2’
    Vector.c:168: warning: unused parameter ‘ip’
    $
    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.

  3. #18
    Registered User
    Join Date
    Oct 2007
    Posts
    11
    I see what you mean, but then position is pointing to the newly added element. It is supposed to be pointing to the same one.

    For instance:
    Code:
    elementArray = {1, 2, 3, 4, x, x, ...}
    elementArray = {1, 2, 3, 4, 4, x, ...}
    elementArray = {1, 2, 3, 3, 4, x, ...}
    elementArray = {1, 2, 3, 9, 4, x, ...}

    There is also an error somewhere with this->position--; being called. If I call listPosition() it seems to subtract one from position (as in it will iterate backwards, then loop back around to the tail). I don't see that happening anywhere in List.c

    I think the error is in listHead and/or listTail... They're being called by something... Damn this abstracted nonsense.

    As for gets() my compiler warns me every time, but I'm not even sure where it's being used. I know fgets() is a lot more stable, but how do you use it without a file?
    Last edited by ampersand11; 10-07-2007 at 02:47 AM.

  4. #19
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Perhaps the wonders of Doxygen will help you with this. It's really quite neat the way you can click on a function and it brings you there. But in this case you can go to the entry for listHead and see which functions it is referenced by:
    Code:
    List* listHead  	(  	List *   	 this  	 )   	
    
    Definition at line 148 of file List.c.
    
    Referenced by main(), vectorRead(), and vectorWrite().
    Ditto for listTail.
    Code:
    List* listTail  	(  	List *   	 this  	 )   	
    
    Definition at line 180 of file List.c.
    
    Referenced by main().
    Anyway, you can have a look at it if you like. It's a .tar.gz masquerading as a .txt, as usual -- 66.3 KB.

    As for gets() my compiler warns me every time, but I'm not even sure where it's being used. I know fgets() is a lot more stable, but how do you use it without a file?
    Use stdin for the file, of course. Just remember to strip the newline that it returns if you want to use it directly where gets() was being used before. See the FAQ for details.
    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.

  5. #20
    Registered User
    Join Date
    Oct 2007
    Posts
    11
    Ahh, looks interesting. Very useful, thank you.

    I'll take a look at my friends listHead / listTail tomorrow. I figure it's just being set to the head or tail at really weird times due to the if condition.
    As for now, it's 5am and I've been at this for hours. I think I'll go for the "fresh look in the morning" approach.

    Again, thank you for all your help. It's been greatly appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of linked list
    By jjeanie in forum C Programming
    Replies: 2
    Last Post: 05-07-2007, 10:08 PM
  2. Replies: 5
    Last Post: 11-04-2006, 06:39 PM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM