Thread: sizeof() no good with array pointers? Or am I screwing this up?

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

    sizeof() no good with array pointers? Or am I screwing this up?

    Ok, I'm sending this function a pointer to an unsigned char array and I want to find the length of it within the function so I can loop and process it or write with fwrite(), etc. Funny, I can malloc the array in a function I passed the pointer to, but I can find how much was malloc'd later.

    Code:
    unsigned char *ELFData;
    
    BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
    //obviously there's a ton of message handling being done here. This is how I'm calling the functions from here though
    
    LoadFile(&ELFData, &hData, eFileName, ELFheader, 0);
    
    SaveFile(&ELFData, &hData, eFileName, ELFheader, 0);
    }
    
    int LoadFile(u8 **buffer, u8 **headerdata, char* filename, int headerlen, u8 endian){
        FILE *f;
        int i, filesize;
    	f = fopen(filename,"rb");
    	fseek(f,0,SEEK_END);
    	filesize = ftell(f);
            if (*buffer) { free(*buffer); *buffer = 0; }
    	if (!(*buffer = (unsigned char*)malloc(filesize))) { 
    	    MessageBox(NULL,"Unable to allocate buffer memory (loadfile,2).","Error",0);
            fclose(f); return 0;
    	}
            //etc
    }
    
    int SaveFile(u8 **buffer, u8 **headerdata, char* filename, int headerlen, u8 endian){
        FILE *f;
        int i;
        int filesize = sizeof(*buffer); //if there was 3 meg allocated to it by an earlier call to LoadFile, it still says '4' or whatever.
    }
    Last edited by Viper187; 06-06-2008 at 10:04 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That's because buffer is a pointer, not an array. (You cannot pass an array to a function, no matter how hard you try.) You need to pass back the size from the function that mallocs the memory.

  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    That's correct, you can't find out from a pointer how much memory was allocated. You'll have to pass a size along with the allocated memory.
    When you call sizeof() on a pointer, it will return you the sizeof that pointer.
    In this case, you have a string, so you can use strlen() to find the length. Provided that the string is terminated with a NULL. Though the best option is still to pass another argument, specifying the length of the buffer.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some good information on Array, strings, and pointers
    By Sshakey6791 in forum C++ Programming
    Replies: 4
    Last Post: 11-30-2008, 03:16 AM
  2. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  3. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Need help on pointer to VOID
    By dv007 in forum C Programming
    Replies: 19
    Last Post: 07-09-2002, 06:15 PM