Hi there, this is my first post here so please tell, if im doing something wrong. Ok so heres the the struct

typedef struct _CMD
{
	char nome[100];
	char arg[100];
	struct _CMD *prox;
}*CMD;
typedef struct _sCMD
{
	int cont;
	CMD inicio;
	CMD fim;
}*sCMD;

The function i use to fill this list:

void add(sCMD com,char str[100],char param[100])
{
	CMD novo = (CMD)malloc(sizeof(struct _CMD));
	strcpy(novo->nome,str);
	strcpy(novo->arg,param);
	novo->prox = NULL;
	com->cont++;

	if(com->inicio == NULL)
	{
		com->inicio = novo;
		com->inicio->prox = com->fim;
	}
	if(com->fim != NULL)
	{
		com->fim->prox = novo;
		com->fim = novo;
	}
	if((com->inicio != NULL) && (com->fim == NULL))
	{
		com->fim = novo;	
	}

	free(novo);
}

After reading into the list, i tried writing everything i had there to see if it worked, and it did. Now the problem is, imagine i had to find the X element of that list, i wrote this function for the deed:

char *find(sCMD com,int i)
{
	int j=0;
	static char str[100];
 	CMD aux = (CMD)malloc(sizeof(struct _CMD));
        aux = com->inicio;
	
	while (j<i)
	{
	aux=aux->prox;
	j++;
	}
	
	strcpy(str,aux->nome);
	free(aux);
	return str;
}

To test it i run this inside a while, to try displaying all the strings "nome" of each element of the list.

i=0;
	while(i < com->cont)
	{	
		printf("%d",i);
		puts(find(com,i));
		i++;
	}

This works for i =0 but after, i get a segmentation fault when calling the function find. further tests showed me it was when i tried aux=aux->prox, now i have no idea why, but the second time this function is called, the list it receives as a parameter is "corrupted", full of trash. Now i hope this isnt too incoherent. Thanks in advance for any attempt of helping me.

>when i tried aux=aux->prox
...aux becomes invalid because aux->prox is an uninitialized pointer. Any further use of aux, except to point it to another address, is doomed to failure.

Thanks for the reply, really got to get this working...

I had this function before to show me everything i had on the list:

void lista(sCMD com) 
{
	int i=0;
	CMD aux = (CMD)malloc(sizeof(struct _CMD));
        aux = com->inicio;
	while(i<com->cont){
		puts(aux->arg);
		aux=aux->prox;
		i++;
	}	
}

Its close to the same thing i think... and this one worked ok, the problem is when im passing the linked list into the function. The first time i do that its good, when i=1 i checked what was inside com->inicio->nome and it was correct, i checked again but this time inside the function find and it was trash, so im thinking if its corruped then the pointer is probably pointing to what? no clue but i think thats why i get the seg fault...

Ok, aparently this is fixed... By the way the way function add was working was if i didnt do free(novo); so i removed free(aux); from function find and it seems to be working so far... but i would still like to know why, if anyone knows and doesn't mind loosing the time to explain, it would be much appreciated. Thanks in advance.

This statement creates a heap and address of newly created heap is assigned to aux pointer variable. (This statement has no use in your code.)

CMD aux = (CMD)malloc(sizeof(struct _CMD));

Now, you are assiging an address of another heap to the aux pointer variable.

aux = com->inicio;

And at the end of function, you are freeing a heap which has search result.

free(aux);

Thanks for the reply. I think i understand it now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.