What is new Bytef[int] in C++?
I have a code snippet below, I'm not quite sure what the last line does. More specifically what's a Bytef[]?
FILE* read_file_handle = fopen(read_filename, "rb");
fseek(read_file_handle, 0, SEEK_END);
size_t no_bytes_to_read = ftell(read_file_handle);
Bytef* read_buffer = new Bytef[no_bytes_to_read];
What is the Bytef[] do? And, if anyone knows, when porting that to PHP, how would I do that? I thought it might be an array, but such a variable 开发者_Go百科has never been defined and with the new keyword it just wouldn't make sense.
Can anyone help?
Hosh
EDIT:
Okay so thanks to the user Default it seems to be something defined within zlib.
It's defined as typedef Byte FAR Bytef;
in this file on line 124
Anyone know what the type of Bytef is according to that?
Byte has been defined as a char (typedef unsigned char Byte;
) and FAR has been defined: #define FAR
Any help?
new Bytef[no_bytes_to_read]
simply allocates an array of Bytef
objects with a length of no_bytes_to_read
.
If you are porting to PHP, as you say, you may want to review PHP arrays. I'm no PHP expert, but it appears that because arrays are implemented as ordered maps, you cannot preallocate - so there is no direct translation of that line.
Additionally, as Default pointed out in the link in his comment, a Bytef
(which appears to be a part of the zlib library) is just another name for a Byte
, which is itself just a typedef for an unsigned char
.
To address your edited question - #define FAR
simply creates that symbol. It does nothing in this case - the preprocessor simply strips it. So ultimately Bytef
is just another name for an unsigned, 8-bit byte.
Bytef
is a custom type, defined in one of the header files you've included. Most likely it's a typedef for unsigned char
or char
; however you'll need to check your includes to be sure of what it is.
new Bytef[no_bytes_to_read]
dynamically allocates an array of no_bytes_to_read
Bytef
objects. As for what a Bytef
is, it's not a standard type, so it's got to be a custom type defined somewhere in your codebase.
Bytef
seems to be a custom class. note that it may be anything, from a typedef to a class, it may also be a simple c-type struct, or an enum, or a #define.
basically, the code is allocating an array of Bytef
object, you have to look into the code to see what Bytef
means. since it is a type, it may well be defined inside a header file included in the current file.
It is array of Bytef
s. Dynamic allocation of arrays happens using operator new[]
in C++. And read_buffer
is pointer to it's first element.
Bytef* read_buffer = new Bytef[no_bytes_to_read];
Declares a pointer of class Bytef which will point to the array allocated by the new [] operator.
You could find this thread useful: http://www.gamedev.net/topic/485109-cpython-compression/
Bytef is defined in the zlib source code. I downloaded the latest version of zlib when I am typing this message: zlib-1.2.11. Inside this directory (after unpacking),
find . -type f | xargs grep Bytef
many lines ignored
......
./zconf.h: typedef Byte FAR Bytef;
......
more lines ignored
Note, Byte is not a type in C or C++ although byte is. So I have to read more lines of code. FAR is a macro defined in zlib. The shorter search pattern for grep will result in many more lines. I add a little bit more filters.
find . -type f | xargs grep Byte | sed '/Bytef/d' | sed '/test/d' | grep typedef
./ChangeLog:- Correct Macintosh avoidance of typedef Byte in zconf.h
//**This is what you want:
./zconf.h:typedef unsigned char Byte; /* 8 bits */
./zconf.h: typedef Byte const *voidpc;
./zconf.h: typedef Byte FAR *voidpf;
./zconf.h: typedef Byte *voidp;
./zconf.h.cmakein:typedef unsigned char Byte; /* 8 bits */
./zconf.h.cmakein: typedef Byte const *voidpc;
./zconf.h.cmakein: typedef Byte FAR *voidpf;
./zconf.h.cmakein: typedef Byte *voidp;
./zconf.h.in:typedef unsigned char Byte; /* 8 bits */
./zconf.h.in: typedef Byte const *voidpc;
./zconf.h.in: typedef Byte FAR *voidpf;
./zconf.h.in: typedef Byte *voidp;
You can see, C uses a lots of typedefs. Sometime it is mind spinning. I am showing this method how to query a source project, so you can figure out the answer for similar type of problems.
FAR is another MACRO defined in zconf.h, but it is not that easy to understand. A lots of def and undef which basically makes FAR as nothing.
When you include zlib.h, new Bytef[10] is the same as new unsinged char[10]. You can even add FAR. In the example code by the authors of zlib used:
unsigned char in[CHUNK];
instead of Bytef in[CHUNK]. Here CHUNK is another MACRO. You use a lots of MACROS in C.
I've been using a boost scoped_array for my ByteF buffer to take care of memory clean up. It is working well
boost::scoped_array<Bytef> buf(new unsigned char[ no_bytes_to_read]);
When passing this buffer to compress or uncompress make sure to add 'get()' to access the internal raw memory.
int ret = compress(buf.get(), ...
精彩评论