Thread: strlen(buffer) on binary data

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    strlen(buffer) on binary data

    I wanted to write a function like this that would return a buffer containing a binary file:

    Code:
    char *getbin (char *filename) {
    	char *buffer;
    	struct stat finfo;
    	FILE *fstRO = fopen(filename, "rb");
    	if (fstRO == NULL) return NULL;
    	stat(filename,&finfo);
    	buffer=ec_malloc(finfo.st_size);
    	if (fread(buffer,1,finfo.st_size,fstRO) != finfo.st_size) {
    		fclose(fstRO); return NULL;}
    	fclose(fstRO); return buffer;
    }
    It works fine -- the only problem is it's impossible to determine how big the returned buffer is! strlen() returns the wrong number (hitting a 0, i guess). Do I have to change the function to return a typedef struct containing the buffer and the size, or is there some way to measure a buffer containing binary data?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    One way would be to "return" the size as well. This can be done by passing int *size or something like that into the function.

    That said, I'm not sure I'm in love with passing allocated memory out of this thing like you are doing.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    No you will have to pass a pointer as an additional parameter or else 'piggyback' it onto the buffer itself (I'd prefer the former, personally).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by FillYourBrain View Post
    One way would be to "return" the size as well. This can be done by passing int *size or something like that into the function.
    I like that suggestion, thanks.

    Quote Originally Posted by FillYourBrain View Post
    That said, I'm not sure I'm in love with passing allocated memory out of this thing like you are doing.
    Isn't that normal as long as I free it later? Is it better to pass a pointer to the function for allocation (why)?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    yeah that part is what I'm not sure about... It seems you just want a quick single call for loading the file. Fine, but I would usually prefer to get the size of the file first and pass the buffer into a call like this. But then I'm weird like that.

    I don't tell people that their approach is wrong though, and I'm not saying that here.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Incidentally, shouldn't you free the buffer if fread() returns something other than finfo.st_size? At the moment it seems to me that there is a memory leak there since only NULL is returned in that case, thus there is no way to free the buffer.

    EDIT:
    Oh, and are you sure you do not need to check if ec_malloc() returns a null pointer? The standard malloc() returns a null pointer if memory could not be allocated.
    Last edited by laserlight; 10-07-2008 at 08:22 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by laserlight View Post
    Incidentally, shouldn't you free the buffer if fread() returns something other than finfo.st_size?
    Yep. Thanks for noticing. And I should check buffer != NULL after the malloc...
    Last edited by MK27; 10-07-2008 at 08:52 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. store binary data in program...
    By Abda92 in forum C Programming
    Replies: 9
    Last Post: 03-23-2008, 10:33 AM
  2. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  3. How to write image data to binary PGM file format(P5)?
    By tommy_chai in forum C Programming
    Replies: 6
    Last Post: 11-03-2007, 10:52 PM
  4. Binary comparison
    By tao in forum Windows Programming
    Replies: 0
    Last Post: 06-28-2006, 12:10 PM
  5. Replies: 4
    Last Post: 06-14-2005, 05:45 AM