Pointer + struct w/arrays confusion

This is a discussion on Pointer + struct w/arrays confusion within the C Programming forums, part of the General Programming Boards category; This stuff drives me nuts. I'm trying to figure out how to reference this struct properly passing a pointer to ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    161

    Pointer + struct w/arrays confusion

    This stuff drives me nuts. I'm trying to figure out how to reference this struct properly passing a pointer to it between functions and manipulating it...

    Typedef for the struct:
    Code:
    typedef unsigned char u8;
    typedef unsigned short int u16;
    typedef unsigned int u32;
    typedef unsigned long long int u64;
    
    //These are the vars included in the header of the binary results files.
    typedef struct CODE_SEARCH_RESULTS_INFO {
        char dmpFileName[MAX_PATH];
        int Endian;
        int SearchSize;
        u32 DumpSize;
        u32 ResCount;
        u32 ResHigh; //highest result file address
        u32 MapFileAddy[MAX_MAPPED_AREAS]; //file address
        u32 MapMemAddy[MAX_MAPPED_AREAS]; //memory address
    } CODE_SEARCH_RESULTS_INFO;
    
    //dump data and results during searches
    typedef struct _RAM_AND_RES_DATA {
        int Access;
        u8 *NewRAM;
        u8 *OldRAM;
        FILE *NewFile;
        FILE *OldFile;
        u8 *Results;
        CODE_SEARCH_RESULTS_INFO OldResultsInfo;
        CODE_SEARCH_RESULTS_INFO NewResultsInfo;
    } RAM_AND_RES_DATA;
    Example use:
    Code:
    RAM_AND_RES_DATA RamInfo; memset(&RamInfo,0,sizeof(RAM_AND_RES_DATA));
    //malloc RamInfo.NewRAM, etc here or send to function to load data into etc //
    //when finished:
    FreeRamInfo(&RamInfo);
    The confusion part:
    Code:
    int FreeRamInfo(RAM_AND_RES_DATA *RamInfo)
    {
        if (RamInfo->NewRAM) { free(RamInfo->NewRAM); RamInfo->NewRAM = NULL; }
        if (RamInfo->OldRAM) { free(RamInfo->OldRAM); RamInfo->OldRAM = NULL; }
        if (RamInfo->NewFile) { fclose(RamInfo->NewFile); RamInfo->NewFile = NULL; }
        if (RamInfo->OldFile) { fclose(RamInfo->OldFile); RamInfo->OldFile = NULL; }
        if (RamInfo->Results) { free(RamInfo->Results); RamInfo->Results = NULL; }
        memset(&RamInfo.OldResultsInfo, 0, sizeof(CODE_SEARCH_RESULTS_INFO));
        memset(&RamInfo.NewResultsInfo, 0, sizeof(CODE_SEARCH_RESULTS_INFO));
        return 0;
    }
    Is FreeRamInfo anywhere near right? I plan to use the same struct in multiple parts of the app (but only come elements some of the time), so I want a shortcut to free up whatever was allocated etc. I'd also like to send the pointer of say RamInfo.NewRAM to a FileLoad function and malloc from there, but I always had issues trying to do that. I don't know if I was doing it wrong, or if malloc just has to take place locally. The only way I ever got it to work was by making RamInfo a global, which I'm trying to avoid now.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Doesn't seem to be any issues there. You are aware that if you want to change something inside a function, you must pass a pointer to that something.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. struct pointer
    By t014y in forum C Programming
    Replies: 5
    Last Post: 01-26-2009, 02:50 PM
  2. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  3. Pointer to different struct based on results
    By Twig in forum C Programming
    Replies: 4
    Last Post: 03-24-2007, 09:10 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM

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