I once wanted to make something in code which would work the same as an array which doesn't need a predefined size, so I could add an infinite number of objects . (in other words, a kind of vector.) The code is a bit old, but it functioned. It was a linear list of objects, but the objects were added in the wrong order (the object which was added first would be the last object to be read) so I actually reversed the reading function so the objects would be read in the correct order. (but were saved in the wrong) As I said: it worked, but I thought the reversed reading-solution was a bad one, so I recently tried to fix this issue by making a new adding algorithm in which the objects would directly be added in the right order. My compiler doesn't complain about any errors, but as soon as I run the program, it crashes. I found out that the problem lies in the forgeLink(X info)
function, if I comment it away the program runs normally, so focus on that function. Here is the source code:
unsigned long long nmbr;
Link(X info,Link<X>*oldLink,unsigned no);
Link<X>::Link(X info,Link<X>*oldLink,unsigned no):
unsigned long long length;
void forgeLink(X info);
//void operator+=(Chain<X> chain,X array);
void Chain<X>::forgeLink(X info)
unsigned long long linkNo;
for(unsigned long long i=0;i<linkNo-1;i++)
for(unsigned long long i=0;i<this->length;i++)
void Chain<X>::operator+=(Chain<X> chain,X array)
for(unsigned long long i=0;i<sizeof(array);i++)
The comments in the code are a function which isn't finished yet, I was too lazy to remove them. And I found it useful to visualize the chain object by drawing it on a piece of paper, so I could maintain the positions of the various pointers.
Dynamic memory has always been my weak spot, so I guess it's an allocation issue. I'm also not completely sure if this topic doesn't belong to the beginners forum, but I'm not sure the people there are better experienced in dynamic allocation than I am, so I apologize if you think this topic doesn't belong here.
Comments on my way of coding or programming style are welcome too. Also, if something is unclear, don't be afraid of asking.