Debug Assertion Failed!

This is a discussion on Debug Assertion Failed! within the C Programming forums, part of the General Programming Boards category; hi... i'm getting this windows error when my program "enters" a function: Debug Assertion Failed! (...) File: f:\dd\vctools\crt_bld\self_x86\crt\src\fgets.c Expression: (string ...

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

    Debug Assertion Failed!

    hi...

    i'm getting this windows error when my program "enters" a function:
    Debug Assertion Failed!
    (...)
    File: f:\dd\vctools\crt_bld\self_x86\crt\src\fgets.c
    Expression: (string != NULL) || (count == 0)
    (...)
    and i have to close the program afterwards :-(

    i think its about this part of the code:
    Code:
    	char *especial = '\0';
    	char *desc = '\0';
    	char *tipoConsulta = '\0';
    
    	(...)
    
    	printf("Qual a Especialidade: ");
    	fgets(especial,sizeof(especial),stdin); //<----------- crashes here
    
    	printf("Descricao:");
    	fgets(desc,sizeof(desc),stdin);
    can anyone help me solve this?
    "Artificial Intelligence usually beats natural stupidity."

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You are reading a string of length 4 into a 1 byte field.

    Code:
    #include <stdio.h>
    
    int main (int argc, const char * argv[]) {
         char *especial ; 
         printf("especial is length &#37;d", sizeof(especial) );
         return 0;
    }
    Todd
    Last edited by Dino; 03-23-2008 at 05:43 PM.
    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!

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Also, depending on your compiler, the '\0' may be placed in read-only stack storage.

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

  4. #4
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    hi Todd

    i'm using VS2008...

    what do you mean:
    the '\0' may be placed in read-only stack storage
    what are the alternatives for what i'm trying to do?
    "Artificial Intelligence usually beats natural stupidity."

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Hundreds.

    But the one you probably want is to declare especial as a character array of at least 2 bytes long, and then when you tell fgets how many maximum bytes to read, tell it the length of your char array, not the length of the pointer to the char array.

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

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    Quote Originally Posted by IndioDoido
    Code:
    char *especial = '\0';
    char *desc = '\0';
    char *tipoConsulta = '\0';
    Does that even work at all? I would think this would implicitly convert the character constant '\0' into a null pointer...

    The problem is that you are declaring pointers that don't point to anything. You need to allocate space for those strings somewhere! Either declare them as char arrays on the stack:
    Code:
    char especial[50];
    char desc[50];
    char tipoConsulta[50];
    (replace 50 with whatever size you want them to be), or allocate some memory on the heap:
    Code:
    char *especial = malloc(50 * sizeof(char));
    char *desc = malloc(50 * sizeof(char));
    char *tipoConsulta = malloc(50 * sizeof(char));
    Just remember, if you allocate space on the heap, you have to free it when you're done with the strings:
    Code:
    free(especial);
    free(desc);
    free(tipoConsulta);
    Oooo, also, I forgot to mention: If you declare your strings as char pointers (char *) and allocate space on the heap, you can't use sizeof() to get the size of your strings. You need to keep track of it yourself.
    Last edited by arpsmack; 03-23-2008 at 05:58 PM.

  7. #7
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    ok, i'm going to declare them as char arrays, as i've done earlier:
    Code:
    char especial[50], desc[200], tipoConsulta[10];
    but i need to do something like this:
    Code:
    tipoConsulta = "Urgente";
    and it doesn't work...

    sorry about all these doubts, but i'm really lame programming :-(
    "Artificial Intelligence usually beats natural stupidity."

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    Look at all the functions available to you in string.h.

    string.h Reference

    In particular, strcpy() will help you here.

  9. #9
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    ohhhhh! i totaly forgot about strcpy() :-S
    i'm really lame! lol

    i'm going to try that ;-)
    "Artificial Intelligence usually beats natural stupidity."

  10. #10
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    that issue is solved :-D

    but i'm having a new problem :-(
    i'm trying to copy variable data into a struct array, but i get errors in the process:
    Unhandled exception at 0x6e79f689 (msvcr90d.dll) in gestao.exe: 0xC0000005: Access violation writing location 0x00000000.
    Code:
    typedef struct
    {
    	int dia, mes, ano;
    }Data;
    
    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; //<------- data do save
    }Medico;
    
    typedef struct paciente
    {
    	char nome[50];
    	char morada[80];
    	int contacto;
    
    	int numBenif;
    	int medicoFamilia;
    	
    	int totalC;
    	Consulta *historico; //<-------- data to save
    }Paciente;
    the program crashes when i copy the variables data to the struct array:
    Code:
    	if(tipo == 1)
    		tempP[j].historico[y].dataConsulta = dataHoje; //<------
    	else
    	{
    		tempP[j].historico[y].dataConsulta.dia = dia; //<------
    		tempP[j].historico[y].dataConsulta.mes = mes; //<------
    		tempP[j].historico[y].dataConsulta.ano = ano; //<------
    	} 
    
    	strcpy(tempP[j].historico[y].tipoConsulta, tipoConsulta); //<------
    	tempP[j].historico[y].medico = idM; //<------
    	strcpy(tempP[j].historico[y].especial, especial); //<------
    	strcpy(tempP[j].historico[y].desc, desc); //<------
    	
    	tempP[j].totalC = tempP[j].totalC + 1;
    
    	tempM[i].dados[x].idPaciente = nBenif; //<------
    	tempM[i].dados[x].tempAprox = tempo; //<------
    	strcpy(tempM[i].dados[x].tipoConsultas, tipoConsulta); //<------
    Why is that?
    "Artificial Intelligence usually beats natural stupidity."

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    Make sure you know the difference between an array of structs, and an array of struct pointers, because they aren't the same.

    If you are actually working with an array of structs, then the memory for those structs is allocated for you, either on the stack, or in the data segment somewhere (if you declared it globally).

    If you are working with an array of struct pointers, only the memory for the pointers is allocated for you. The pointers themselves aren't actually pointing at anything and have no allocated memory associated with them. If all you have is a struct pointer, then you need to allocate memory for the struct, and then store a pointer to that memory in your struct pointer.

    For example, take your struct:
    Code:
    typedef struct paciente
    {
    	char nome[50];
    	char morada[80];
    	int contacto;
    
    	int numBenif;
    	int medicoFamilia;
    	
    	int totalC;
    	Consulta *historico;
    }Paciente;
    You cannot do this:
    Code:
       Paciente *createPaciente(void) {
          Paciente *someGuy; // pointer allocated on the stack
          someGuy->historico->medico = 5; // NO, can't do this!
          ...
          return someGuy;
       }
    The reason you can't do that is because someGuy and historico are just pointers. They aren't pointing to anything yet so you can't possibly dereference them or store values in them. Instead you need to do this:
    Code:
       Paciente *createPaciente(void) {
          Paciente *someGuy; // pointer allocated on the stack
    
          someGuy = malloc(sizeof(Paciente)); // allocate space for a new Paciente and
                                              // make someGuy point to it
    
          someGuy->historico = malloc(sizeof(Consulta));
          someGuy->historico->medico = 5; // OK, this is fine now
          ...
          return someGuy;
       }
    Does that make things clearer?

  12. #12
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    hi arpsmack!

    sorry about my confusion on arrays of structs and array of struct pointers.

    thanks for the explanation. it's more clear to me now. but i have a problem, i'm not allowed to work with allocated memory :-( in the following case i have to use an array of structs:
    Code:
    Paciente listaPaciente[250];
    Medico listaMedico[10];
    
    void inserirConsulta(Paciente tempP[], Medico tempM[], int nP, int nM)
    {
    (...)
    }
    i'm only using an array of struct pointers in Consulta *historico; from the Paciente struct.
    how do i accomplish that in this case?
    Last edited by IndioDoido; 03-23-2008 at 08:22 PM.
    "Artificial Intelligence usually beats natural stupidity."

  13. #13
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    Okay I think I see. The 'historico' field of Consulta is supposed to be an array of Consultas?

    If that's the case, and you aren't allowed to allocate memory using malloc(), then you'll have to define your struct like this:
    Code:
    typedef struct paciente
    {
    	char nome[50];
    	char morada[80];
    	int contacto;
    
    	int numBenif;
    	int medicoFamilia;
    	
    	int totalC;
    	Consulta historico[50]; // or however many you need
    }Paciente;
    The only way around a fixed size like that would be to allocate memory dynamically.

    On the downside, this will make your Paciente struct rather large.

  14. #14
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    yes that it!

    but if i could use malloc() how would i use it 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)
    {
    (...)
    }
    by the way, what is the difference between allocating memory using malloc() and allocating memory dynamically?
    "Artificial Intelligence usually beats natural stupidity."

  15. #15
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    They're the same thing. Allocating something on the stack or in the data segment is called "static allocation" since the compiler sets it up for you and it can't change at runtime.

    malloc() however allows you to allocate a variable sized block of memory at runtime, so its referred to as "dynamic allocation".

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

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