Thread: Strange structure array issue...

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

    Strange structure array issue...

    hi!

    i created two structure arrays in my program, one is working well, but the other one is being stupid :-(

    here's what's happening, i created 2 structure arrays:
    Code:
    	Paciente listaPaciente[250];
    	Medico listaMedico[10];
    at first i only had Medico listaMedico[10]; in my program and everything worked :-)
    but since i created Paciente listaPaciente[250]; every time i try and run the program it crashes :-(

    but the weird thing is, when i change the listaPaciente[]; array size to, for example, listaPaciente[100]; the program runs nicely :-S

    Can any one tell me why is this happening?
    "Artificial Intelligence usually beats natural stupidity."

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Maybe you are blowing the stack? On Windows its like 1MB or something.

  3. #3
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    how come?
    i just created a array for 250 elements, and that array is empty. the program crashes when i run it
    "Artificial Intelligence usually beats natural stupidity."

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What is the type Paciente? I'm guessing it's a struct of some sort, and as such, it can take quite a few bytes per entry.

    What compiler/environment are you running this in? Depending the environment, the stack may be quite small, and in that case, 250 structs of a few hundred bytes can easily blow the stack up. With no stack the application WILL crash in the most dramatic ways.

    --
    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.

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

    yap it's a struct:
    Code:
    typedef struct
    {
    	int dia, mes, ano;
    }Data;
    
    typedef struct
    {
    	char tipoConsulta[10];
    	Data dataConsulta;
    	int medico;
    	char especial[100];
    	char desc[300];
    }Consulta;
    
    typedef struct paciente
    {
    	char nome[200];
    	char morada[300];
    	int contacto;
    
    	int numBenif;
    	char medicoFamilia[200];
    
    	Consulta historico[10];
    }Paciente;
    i'm using VS2008, and in my project i have to use a struct array with 250 elements :-(
    "Artificial Intelligence usually beats natural stupidity."

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you paciente has about 818 bytes of the size
    so 250 structs will take ~ 204K of memory
    your consulta is also big enough...

    better use dynamic allocations for these arrays...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    dame!

    this is a 2part school project, the 1part has to bee done with standard arrays, and in the 2part of the project we must use dynamic arrays :-(

    is there anything i can do to reduce the memory size?
    "Artificial Intelligence usually beats natural stupidity."

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    as a last option - you can make them global...

    AS a intermediate solution - I would replace all char arrays in your structs to pointers and allocate strings as you know the length
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    i changed the char arrays in the struct struct to pointers:
    Code:
    typedef struct
    {
    	char *tipoConsulta;
    	Data dataConsulta;
    	int medico;
    	char *especial;
    	char *desc;
    }Consulta;
    and know it works :-)
    i'm not sure that i can't use global variables in my project, so what are consequences in using pointers?
    "Artificial Intelligence usually beats natural stupidity."

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    so what are consequences in using pointers?
    You will have a lot more work managing your memory allocations...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic structure with array and array count
    By Nazgulled in forum C Programming
    Replies: 14
    Last Post: 06-08-2007, 10:10 PM
  2. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  3. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  4. structure array
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-20-2002, 08:30 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM