ugh! What am I doing wrong now? (pointers/fread/malloc issue)

This is a discussion on ugh! What am I doing wrong now? (pointers/fread/malloc issue) within the C Programming forums, part of the General Programming Boards category; Ok, I've done something similar with loading a file into an array of unsigned chars, but I'm not getting this ...

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

    ugh! What am I doing wrong now? (pointers/fread/malloc issue)

    Ok, I've done something similar with loading a file into an array of unsigned chars, but I'm not getting this to work for some reason. Obviously, I suck with pointers. I'm trying to load the whole file into an array of unsigned ints.


    Code:
    typedef unsigned int u32;
    
    u32 *ELFData32; //global declaration
    
    LoadELF32(&ELFData32, eFileName);  //function call
    
    u32 LoadELF32(u32 **buffer, char* filename)
    {
        FILE *f;
        int i;
        u32 filesize;
    	f = fopen(filename,"rb");
    	if (!(f)) { MessageBox(NULL,"Unable to open file (LoadFile32,1).","Error",0); return 0; }
    	fseek(f, 0, SEEK_END);
    	filesize = ftell(f);
        if (*buffer) { free(*buffer); *buffer = 0; }
    	if (!(*buffer = (u32*)malloc((filesize >> 2) - 1))) { 
    	    MessageBox(NULL,"Unable to allocate buffer memory (LoadELF32,1).","Error",0);
            return 0;
    	}
    	fseek(f, 0, SEEK_SET);
    	fread(*buffer, sizeof(u32), (filesize >> 2) - 1, f);
    	return filesize;
    }
    Last edited by Viper187; 06-13-2008 at 10:42 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    > if (*buffer) { free(*buffer); *buffer = 0; }
    If buffer is an uninitialised pointer, this will attempt to free garbage the first time you call it.

    > *buffer = (u32*)malloc((filesize >> 2) - 1)
    You may be reading some unsigned ints, but you still need to allocate the required number of bytes.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    161
    Quote Originally Posted by Salem View Post
    >
    Code:
    if (*buffer) { free(*buffer); *buffer = 0; }
    If buffer is an uninitialised pointer, this will attempt to free garbage the first time you call it.

    >
    Code:
    *buffer = (u32*)malloc((filesize >> 2) - 1)
    You may be reading some unsigned ints, but you still need to allocate the required number of bytes.
    How do I check if the pointer is initialized then so it won't try to free garbage? I always did it that way. Compiled and didn't crash the program, so I figured it was ok;

    Am I not allocating the right amount?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    You're fortunate that your pointer is global, so it defaults to being NULL when you start.

    > Am I not allocating the right amount?
    No, you need to allocate the number of bytes.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    In other words, don't forget to use "* sizeof u32" or some such.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,609
    Quote Originally Posted by Viper187 View Post
    How do I check if the pointer is initialized then so it won't try to free garbage? I always did it that way. Compiled and didn't crash the program, so I figured it was ok
    A good suggestion is to always initialize your pointers to NULL (unless you are initializing them to something else) and ALWAYS set them to NULL after freeing them.
    That way, you can see if they contain a valid memory address by checking != NULL.
    (And it's safe to free a NULL pointer.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Viper187 View Post
    Compiled and didn't crash the program, so I figured it was ok;
    Welcome to the world of Undefined Behavior. Just because things compile and don't crash the first time, doesn't mean they won't later on. If you know something's not supported, don't do it, even it appears to work fine!

    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. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. pow function returns wrong value
    By ckbales in forum C Programming
    Replies: 5
    Last Post: 02-01-2003, 08:46 PM
  5. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM

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