Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Dynamic Memory Allocation
kvarnerexpress
post Dec 16 2005, 05:32 PM
Post #1


Super Member
*********

Group: Members
Posts: 407
Joined: 13-December 04
Member No.: 2,696



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

CODE
#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
Go to the top of the page
 
+Quote Post
mukund
post Dec 19 2005, 02:51 PM
Post #2


Member [Level 2]
*****

Group: Members
Posts: 78
Joined: 14-March 05
Member No.: 4,492



can u just post an english version of this program?

I can then debug and give it to u
Go to the top of the page
 
+Quote Post
switch
post Dec 26 2005, 01:07 AM
Post #3


Premium Member
********

Group: Members
Posts: 178
Joined: 13-October 04
From: NSW, Australia
Member No.: 1,713



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).
CODE
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!
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Accessing Physical Memory(3)
  2. Discussion On Dynamic Programming(3)


 



- Lo-Fi Version Time is now: 29th August 2008 - 11:29 PM