• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 296
  • Last Modified:

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

0
GPicasso
Asked:
GPicasso
1 Solution
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now