Jul 24, 2008

Dynamic Memory Allocation

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > C/C++ Programming

free web hosting

Dynamic Memory Allocation

kvarnerexpress
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

 

 

 


Reply

mukund
can u just post an english version of this program?

I can then debug and give it to u

Reply

switch
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!

 

 

 


Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:


Searching Video's for dynamic, memory, allocation
advertisement



Dynamic Memory Allocation



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE