Debug Assertion Failed!

This is a discussion on Debug Assertion Failed! within the C Programming forums, part of the General Programming Boards category; ok. thanks again for the explanation. to use malloc() in this case: Code: typedef struct paciente { char nome[50]; char ...

  1. #16
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    ok. thanks again for the explanation.

    to use malloc() in this case:
    Code:
    typedef struct paciente
    {
    	char nome[50];
    	char morada[80];
    	int contacto;
    
    	int numBenif;
    	int medicoFamilia;
    	
    	int totalC;
    	Consulta *historico; //<------------ with a array of struct pointers where
    }Paciente;
    
    Paciente listaPaciente[250]; //<------------ and a array of structs here
    Medico listaMedico[10];
    
    void inserirConsulta(Paciente tempP[], Medico tempM[], int nP, int nM)
    {
    (...)
    }
    is this the correct way?
    Code:
    {
          tempP[j].historico = malloc(sizeof(Consulta));
          tempP[j].historico->medico = x;
          ...
          free tempP[j].historico;
    }
    "Artificial Intelligence usually beats natural stupidity."

  2. #17
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    That would definitely allocate you a Consulta and assign a value to the medico field, but I don't think you'd want to free it that early. Once you free it the data stored there is no longer reliable. Sometimes figuring out when you can free your memory can be pretty tricky.

    By the way, free is a function, so remember to call it like this:
    Code:
    free(tempP[j].historico);

  3. #18
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    ok, i'll remenber that ;-)

    sorry, but i have another question (zZzZz...), in this case i'm going to insert data to the first field of the array of struct pointers:
    Code:
    {
          tempP[j].historico = malloc(sizeof(Consulta));
          tempP[j].historico->medico = x;
          ...
    }
    if i need to insert more data to the array, how do i do that without overwriting the stored data?
    Last edited by IndioDoido; 03-23-2008 at 10:07 PM.
    "Artificial Intelligence usually beats natural stupidity."

  4. #19
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    Well if its an array, you're probably going to want to allocate enough space for more than one Consultas.
    Code:
    {
          tempP[j].historico = malloc(50 * sizeof(Consulta));
          tempP[j].historico[0].medico = x;
          ...
    }
    50 could of course be any value you want, or it could be a variable. Then you can use the pointer as if it were an array. For the example I gave, since I allocated enough space for 50 Consultas, I could access everything from historico[0] to historico[49].

  5. #20
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    but what i want to do is, for each client i want to save on this medical history every time he goes to the doctor.

    cliente = tempP
    history = historico

    isn't this possible?
    Code:
    {
          tempP[j].historico = malloc(sizeof(Consulta));
          tempP[j].historico[0].medico = x;
          ...
    }
    because doing this:
    Code:
    {
          tempP[j].historico = malloc(50 * sizeof(Consulta));
          tempP[j].historico[0].medico = x;
          ...
    }
    i would only have space for 50 history records, correct?

    if so, it would be better to use a array of structs like this:
    Code:
    typedef struct paciente
    {
    	char nome[50];
    	char morada[80];
    	int contacto;
    
    	int numBenif;
    	int medicoFamilia;
    	
    	int totalC;
    	Consulta historico[50]; //<---- array of structs
    }Paciente;
    
    (...)
    tempP[j].historico[y].medico = x;
    (...)
    "Artificial Intelligence usually beats natural stupidity."

  6. #21
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    Well since patients don't normally go to the doctor hundreds and hundreds of times, and random access probably isn't that important in the patient's history, you could probably do it nicely with a linked list.

    Then every time a patient shows up for a visit, you add a new Consulta to the head of the list. As an added benefit, you would have all their visits sorted in chronological order starting with the most recent first.

  7. #22
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    wow!
    and how do i do that???
    "Artificial Intelligence usually beats natural stupidity."

  8. #23
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    Here's a pretty good tutorial on linked lists in C: http://richardbowles.tripod.com/cpp/...t/linklist.htm

    I like that it has plenty of diagrams and code samples to go with them.

    Basically, your historico field will become a pointer to the 'head' of your list, and every Consulta in the list should have a pointer to the next Consulta in the list.

  9. #24
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    tanks arpsmack, you've been a great help ;-)

    i'm going to take a look at the tutorial you gave and learn more about link lists. but for now i'm going to use an array of structs for the patients history for now.

    once again thanks allot for the help.
    "Artificial Intelligence usually beats natural stupidity."

  10. #25
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194

    working with an array of struct pointers

    everytime i insert a new record to my array, the data goes to the right place but the previous data will be replaced with junk :-(

    example:
    i save data to the position array[3], but the data from array[0], array[1] and array[2] will de replaced with junk.

    can anyone tell me what i'm doing wrong?

    Code:
    x = tempM[i].nConsultas;
    (...)
    tempM[i].dados = malloc(sizeof(DadosConsultas));
    
    tempM[i].dados[x].idPaciente = nBenif;
    tempM[i].dados[x].tempAprox = tempo;
    strcpy(tempM[i].dados[x].tipoConsultas, tipoConsulta);
    
    tempM[i].nConsultas = tempM[i].nConsultas + 1;
    "Artificial Intelligence usually beats natural stupidity."

  11. #26
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    what I see - you allocate dados as 1 struct, but use it as array. What are the types used in this code snipplet?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #27
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    What are the types used in this code snipplet?
    Is this what you need?

    Code:
    typedef struct
    {
    	int dia;
    	int hora;
    }Horario;
    
    typedef struct
     {
    	char tipoConsulta[10];
    	Data dataConsulta;
    	int medico;
    	char especial[50];
    	char desc[100];
    }Consulta;
    
    typedef struct
    {
    	int idPaciente;
    	int tempAprox;
    	char tipoConsultas[10];
    }DadosConsultas;
    
    typedef struct medico
    {
    	char nome[50];
    	char morada[80];
    	int contacto;
    
    	char especialidade[50];
    	int numPacientes;
    	int idMedico;
    	int nConsultas;
    
    	Horario horarioTrab[5];
    	DadosConsultas *dados; //<----------------
    }Medico;
    
    typedef struct paciente
    {
    	char nome[50];
    	char morada[80];
    	int contacto;
    
    	int numBenif;
    	int medicoFamilia;
    	
    	int totalC;
    	Consulta historico[20];
    }Paciente;
    Code:
    Medico listaMedico[10];
    Paciente listaPaciente[250];
    
    void inserirConsulta(Paciente tempP[], Medico tempM[], int nP, int nM)
    {
         (...)
         x = tempM[i].nConsultas;
         (...)
         tempM[i].dados = malloc(sizeof(DadosConsultas));
    
         tempM[i].dados[x].idPaciente = nBenif;
         tempM[i].dados[x].tempAprox = tempo;
         strcpy(tempM[i].dados[x].tipoConsultas, tipoConsulta);
    
         tempM[i].nConsultas = tempM[i].nConsultas + 1;
    
    }
    Last edited by IndioDoido; 03-24-2008 at 01:56 PM.
    "Artificial Intelligence usually beats natural stupidity."

  13. #28
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Todd Burch View Post
    Also, depending on your compiler, the '\0' may be placed in read-only stack storage.

    Todd
    1. Stack is NEVER read only.
    2. char *something = '\0'; does the same as char *something = NULL - we set the pointer to NULL, and guess what: fgets() in debug mode checks that string should not be NULL.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #29
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Thanks.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  15. #30
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Is this what you need?
    So I was correct in my assumption that you allocate one struct and use it as array. Are you planning to fix it?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. debug assertion failed!
    By chintugavali in forum C++ Programming
    Replies: 5
    Last Post: 12-21-2007, 04:05 AM
  2. Visual Studio 2005 Debug Assertion Failed
    By natmas in forum C++ Programming
    Replies: 7
    Last Post: 07-17-2007, 05:28 PM
  3. debug assertion failed !
    By blue_gene in forum C++ Programming
    Replies: 2
    Last Post: 05-09-2004, 12:23 PM
  4. Debug Assertion Failed!
    By Ray Schmidt in forum C++ Programming
    Replies: 3
    Last Post: 02-21-2003, 09:58 PM
  5. Debug Assertion Failed
    By minesweeper in forum Windows Programming
    Replies: 5
    Last Post: 12-11-2002, 05:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21