Size of list not correct

It appears there is a logic error in my program
Mysteriously my list size ends up being 4195568
No matter how big my list is it's always the same size naturally my code looks perfect to me.

#include <stdio.h>
#include <stdlib.h>
#include "salist.h"

/*typedef struct SList SList;
typedef int* PtrToArray;
typedef int* Position;
typedef int ElementType;
struct SList{
	PtrToArray storage;
	int size;
};
*/

void Insert(ElementType element, SList list)
{
	int i;
	if (list.storage)
	{
		list.storage = realloc( list.storage, (sizeof(ElementType) * ++list.size) );
		for (i=list.size-1; i>0; i--)
		{
			list.storage[i] = list.storage[i+1];

		}
		list.storage[0] = element;

	}
	else
	{
		list.storage = malloc(sizeof(ElementType));
		list.storage[0] = element;
		list.size = 1;
	}



}
Position Find(ElementType element, SList list)
{
	Position r = 0;
	int i=0;
	while(list.storage[i] != element && i <list.size)
	{
		i++;
	}
	if (i<list.size)
	{
		while( i > 0)
		{
			list.storage[i] = list.storage[--i];
		}
	list.storage[0] = element;
	r = list.storage;
	}
	return r;
}
void Delete(ElementType element, SList list)
{
	int i = 0;
	while (list.storage[i] != element)
	{
		i++;
	}
	while(i<list.size)/*move every remaining element in the array back one*/
	/*reallocate the array to be one smaller than it was*/
	{
		list.storage[i++]=list.storage[i];

	}
	list.storage = realloc( list.storage, (sizeof(ElementType) * --list.size) );

}

void PrintList(SList list)
{
	int i;
	for (i = 0; i<list.size;i++)
	{
		printf("%d\n",list.storage[i]);
	}
}
void debug(SList list)
{
	printf("%d\n",list.size);
}

Open in new window

#include "salist.h"
#include <stdio.h>
#include <stdlib.h>

int main()
{
	int i;
	SList list;
	for (i = 0; i < 3; i++)
	{
		Insert(i, list);
	}
	//PrintList(list);
	debug(list);
	return 0;
}

Open in new window

#ifndef SALIST_H_
#define SALIST_H_

typedef int *PtrToArray;
typedef int *Position;
typedef int ElementType;

struct SList{
	PtrToArray storage;
	int size;
};

typedef struct SList SList;

void PrintList( SList list);
void Insert( ElementType element, SList list);
void Delete( ElementType element, SList list);
Position Find(ElementType element, SList list);
void debug(SList list);


#endif /* SALIST_H_ */

Open in new window

GPicassoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
The list variable in main was never initialized.

And the Insert function modifies a copy of the list, not the original list.

So, after the for loop, nothing has changed to the list, and it is still uninitialized. This means that it could be anything - and apparently, in your case, that means a size of 4195568.


You need to :

(a) properly initialize the list before trying to use it
(b) pass the list by pointer to the functions, so the original list can be modified (and not a copy of the list)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GPicassoAuthor Commented:
gotcha, I should know better then that.  Understand the concept but am not smart enough to implement it.
Looks like I will have to make a similar modification to my find function as well.


It seems to really not like this.
salist.c: In function ‘Insert’:
salist.c:22:69: error: invalid type argument of unary ‘*’ (have ‘int’)
salist.c:22:3: warning: passing argument 1 of ‘realloc’ makes pointer from integer without a cast
/usr/include/stdlib.h:485:14: note: expected ‘void *’ but argument is of type ‘int’
salist.c:23:10: error: invalid type argument of unary ‘*’ (have ‘int’)
salist.c:25:4: error: invalid type argument of unary ‘*’ (have ‘int’)
salist.c:25:24: error: invalid type argument of unary ‘*’ (have ‘int’)
salist.c:28:3: error: invalid type argument of unary ‘*’ (have ‘int’)
salist.c:33:18: warning: assignment makes integer from pointer without a cast
salist.c:34:3: error: invalid type argument of unary ‘*’ (have ‘int’)
salist.c:35:3: error: invalid type argument of unary ‘*’ (have ‘int’)


How do I do this properly?
void Insert(ElementType element, SList* list)
{


	int i;
	if (*list->storage)
	{
		*list->storage = realloc( *list->storage, (sizeof(ElementType) * (*list->size + 1)) );
		for (i=*list->size-1; i>0; i--)
		{
			*list->storage[i] = *list->storage[i+1];

		}
		*list->storage[0] = element;

	}
	else
	{
		*list->storage = malloc(sizeof(ElementType));
		*list->storage[0] = element;
		*list->size = 1;

	}



}

Open in new window

0
TommySzalapskiCommented:
The -> operator is evaluated before the * operator. So what you have is basically
*(list->storage)
You just want list->storage (technically (*list).storage would work too, but the first one is better).

The -> is used with pointers so you don't need to dereference it.
0
ssnkumarCommented:
If you are compiling your code using gcc, then use -Wall compiler option - that will show all the warnings.
Some of these warnings are actually potential errors and will help you to see the problem.
0
evilrixSenior Software Engineer (Avast)Commented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.