Jump to content
xisto Community
Sign in to follow this  
kvarnerexpress

Dynamic Memory Allocation

Recommended Posts

I'm writing a didactic software that can store an undefined number of int in a dynamic list. I wrote a routine too that can delete an int and point the previous node to the next node after the one I've deleted.

But I've a problem: If I try to delete a number that isn't in the list, software crashes! Why? If the routine doesn't find the number it should simply do nothing

#include <stdio.h>#include <stdlib.h>void crealista();void visualizzalista();void eliminanumero();struct elenco {   int numero;   struct elenco *pun;   };int i,n;struct elenco *p,*paux, *paux2;main(){   char risp;	   crealista();   visualizzalista();}void crealista(){   printf("Quanti numeri vuoi inserire nella lista? ");   scanf("%i",&n);   if (n==0)      p = NULL;   else   {        p = (struct elenco *)malloc(sizeof(struct elenco));      printf("\nInserisci il valore: ");      scanf("%i",&p->numero);      paux = p;      for (i=1;i<=n-1;i++)      {         paux->pun = (struct elenco *)malloc(sizeof(struct elenco));         paux = paux->pun;         printf("\nInserisci il valore: ");              scanf("%i",&paux->numero);              if (i==n-1)                paux->pun = NULL;      }   }}void visualizzalista(){   char risp;	   system("cls");   paux = p;   while (paux != NULL)   {       printf("%i\n",paux->numero);      paux = paux->pun;   }   printf("\n\nVuoi eliminare un numero? (s/n): ");   scanf("%1s",&risp);   switch (risp) { 	case 's': eliminanumero();	break;	case 'n': exit(0);	break;	default:	printf("\n%i",risp);	printf("\n\nIl tasto scelto non permette azioni! Il programma viene terminato");	exit(0);}} void eliminanumero(){        int dato;        printf("Che numero vuoi eliminare? ");        scanf("%i",&dato);        paux = p;            while (paux != NULL)     {      if (paux == p && paux->numero == dato)      {       p = paux->pun;       free(paux);       break;      }      else if (paux->pun->numero == dato)      {       paux->pun = paux->pun->pun;       paux2 = paux->pun;       free(paux2);       break;      }      paux = paux->pun;     }visualizzalista();}

Mny thanks for the help

Share this post


Link to post
Share on other sites

I couldn't say what it is exactly, but I don't like the looks of the pointers (only because I find them confusing sometimes).

struct elenco *p,*paux, *paux2;
It just seems to me like you could do it without the pointers. I have found that alot of the time my unexplained crashes have something to do with memory management issues.

Generally, if I can't get one of my programs working, I try to do the same thing in a different way. That can often iron out bugs.

Also, you could try cleaning all the intermediate files (if you are using Visual C++) and then re-compiling. That's helped me before.

I can't really understand the Spanish (??) too well, but I think you may also find what's happening is that you have created an endless loop or something. This would normally make a program hang, rather than crash, but if every time the loop iterates new memory needs to be allocated or something, then the program would definately crash after a while.

Also, my best advice (although it sounds obvious) is to run the program in a Debug mode and step through line by line or set a lot of breakpoints. You get the idea.

Good luck mate, hope everything goes alright.

peace out!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.