Thread: why's it crash?????

  1. #1
    My diaper's full....... stevey's Avatar
    Join Date
    Nov 2001
    Posts
    746

    Question why's it crash?????

    can anyone tell me why the following code crashes my computer on reaching the end of the file ?????
    its just reading from a binary file with the data structure as shown. it prints -

    etc

    (null) EGGSHELL CREAM 02 006529 0350 900805 007.99
    (null) EGGSHELL SUNRISE 03 006789 1250 900618 008.00
    (null) EGGSHELL LILAC 02 006015 0400 900115 007.50
    (null) EGGSHELL PINK 00 001250 1000 900725 007.00
    (null) EGGSHELL OFF WHITE 02 008025 0500 900623 007.99
    (null) EGGSHELL PALE GOLD 00 001023 0150 900426 007.00
    (null) EGGSHELL SUPERWHITE 04 008957 1500 900418 007.99
    (null) EGGSHELL MAGENTA 00 002394 0500 900428 007.89
    (null) EGGSHELL LEMON 00 007500 0500 900621 007.00
    end of file

    then crashes.
    all the data is ok except the first field , why is this printed as null ???

    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdlib.h>
    #include <time.h>
    #include <isam.h>
    #include <common.h>




    FILE *fp_stock_master;
    FILE *fp_out;


    RECORD_DATA *stock_rec;

    int main()
    {

    if((fp_stock_master=fopen("A:STCKMAST.DAT","rb"))= =NULL)
    {
    printf("ERROR OPENING STOCK MASTER FILE\n");
    exit(1);
    }
    else
    printf("OPENING STOCK MASTER FILE\n");



    do
    {
    if(fread(stock_rec,sizeof(RECORD_DATA),1,fp_stock_ master)!=1)
    {
    if(feof(fp_stock_master))
    { printf("end of file");}
    else if(!feof(fp_stock_master)) /*else reading error*/
    {printf("\nERROR READING CUSTOMER MASTER FILE - PROGRAM TERMINATED");exit(1); }
    break;
    }


    if(!feof(fp_stock_master) )

    printf("%6s %19s %02d %06ld %04d %06s %06.2f\n",stock_rec->key,stock_rec->desc,
    stock_rec->supp_code,stock_rec->free_stock,stock_rec->min_stock,
    stock_rec->movement_date,stock_rec->price);


    }
    while(!feof(fp_stock_master));

    fclose(fp_stock_master);
    return 1;
    }

    /*
    typedef struct
    {
    char key[7];
    char desc[20];
    short supp_code;
    long free_stock;
    short min_stock;
    char movement_date[7];
    float price;
    } RECORD_DATA;

    this struct is in a header file */
    Steve

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > RECORD_DATA *stock_rec;
    Because this isn't pointing at any valid memory - you need to allocate memory.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    I don't have enormous amounts of time right now to carefully look through your code, but I can give you a couple of things that might help.

    The reason that it prints "(null)" is, that is the way that most recent compilers print a pointer when, you guessed it, the pointer is NULL. Restep through your code and see where you want to assign a value to the pointer. That could be the problem of messed up assigning.

    The main reason for a lot of runtime errors (crashing) is also pointer related. Are you trying to initialize a pointer that doesn't exist? That happens a lot with what you are trying to do.

    I'll take a better look at in the next couple days and I'll get back to you (if somebody doesn't beat me to it).

    --Garfield the Great
    1978 Silver Anniversary Corvette

  4. #4
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    > > RECORD_DATA *stock_rec;
    Because this isn't pointing at any valid memory - you need to allocate memory. <

    Arg!!! Salem beat me to it. Well, anyway, Salem is right. Like I said, you are trying to assign to an invalud pointer.

    --Garfield the Slow
    1978 Silver Anniversary Corvette

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    20
    I have a question!

    The RECORD_DATA structure (for the stock master file(INDEXED)) is declared in the header file "ISAM.H", therefore can't you just write in your code:

    RECORD_DATA *stock_rec

    Will this not access the structure in the header file without need to allocate memory?
    If not how do you do this?

    There is a function to read the stock file included in the header file, the prototype being:

    static RECORD_DATA *search_file(char *product_code);

    To use this would you declare it in your code with the other functions and then use it as follows:

    RECORD_DATA *stock_rec;

    if((stock_rec = search_file(part_number)) != NULL)
    {
    /*record found*/
    }

    Would this work?

    A very confused Kitten

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're confusing 'declaring' a structure instance with 'defining' one.

    Code:
    Define:
    
    struct mystruct 
    {
       int x;
       float y;
       char *z;
    };
    
    Declare:
    
    struct mystruct *mPtr;
    Ok, so in the first part, we've declared a structure and it's contents. Now when we want to use one, we have to declare an instalce of it. In this case, we've elected to declare 'mPtr' as a pointer to a 'mystruct' structure.

    When you declare a pointer, it points to some random space out, and you have to make it useable, by giving it a value.

    Note that a pointer has no real space of it's own (it actually has a bit of space, but ignore that for now). You have to allocate space.

    To allocate a block of memory for it to point at, we commonly use 'malloc'.

    mPtr = malloc( sizeof( struct mystruct ) );

    Now then, mPtr will end up "pointing at" an allocate block of memory who's size is "sizeof( struct mystruct )".

    This means the allocated space is the size of a normal 'struct mystruct' instance.

    You can now use this block of memory.

    Quzah.

  7. #7
    My diaper's full....... stevey's Avatar
    Join Date
    Nov 2001
    Posts
    746
    thanx folks - problem solved - bad habit i've got allocating(or not)
    memory.
    sorry, i tried to reply straight away to Salem, but my modem won't work - i think its cos i crashed my OS so much !!!

    what confused me i think is cos i got away with it before for a previous problem, its interesting that the rest of the fields are ok,
    and it only crashes when reaching eof - i actually had a version of my prog which didn't crash, but the 1st field was null.

    so kitten
    i take it youre doing 4240 p3 ??
    as mentioned above i didn't allocate memory for my solution to this and got away with it !!! i guess this is cos i only accessed the price info, you notice on the bit of my file that i copied, the price was read ok !!!!!!

    anyway, probably lost many marks for it, cos its obviously bad practice !!!

    steve

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. Hooking Crash?
    By Elysia in forum Windows Programming
    Replies: 9
    Last Post: 03-15-2008, 01:13 PM
  3. Can not debug a crash
    By hannibar in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2007, 10:02 AM
  4. Dynamic array sizing causes crash
    By Mithoric in forum C++ Programming
    Replies: 3
    Last Post: 12-30-2003, 07:46 AM
  5. FYI: asctime(gmtime(&mytime)) = crash!
    By anonytmouse in forum C Programming
    Replies: 2
    Last Post: 09-29-2003, 02:24 AM