portability

How to use printf to display off_t, nlink_t, size_t and other special types?

百般思念 提交于 2019-11-26 09:44:28
问题 In my program, I stat the files they want and send the data over. The fields of a stat struct are all special types: struct stat { dev_t st_dev; /* ID of device containing file */ ino_t st_ino; /* inode number */ mode_t st_mode; /* protection */ nlink_t st_nlink; /* number of hard links */ uid_t st_uid; /* user ID of owner */ gid_t st_gid; /* group ID of owner */ dev_t st_rdev; /* device ID (if special file) */ off_t st_size; /* total size, in bytes */ blksize_t st_blksize; /* blocksize for

Change the current working directory in C++

て烟熏妆下的殇ゞ 提交于 2019-11-26 09:38:50
问题 How can I change my current working directory in C++ in a platform-agnostic way? I found the direct.h header file, which is Windows compatible, and the unistd.h , which is UNIX/POSIX compatible. 回答1: The chdir function works on both POSIX (manpage) and Windows (called _chdir there but an alias chdir exists). Both implementations return zero on success and -1 on error. As you can see in the manpage, more distinguished errno values are possible in the POSIX variant, but that shouldn't really

Building a 32-bit float out of its 4 composite bytes

a 夏天 提交于 2019-11-26 09:37:23
问题 I\'m trying to build a 32-bit float out of its 4 composite bytes. Is there a better (or more portable) way to do this than with the following method? #include <iostream> typedef unsigned char uchar; float bytesToFloat(uchar b0, uchar b1, uchar b2, uchar b3) { float output; *((uchar*)(&output) + 3) = b0; *((uchar*)(&output) + 2) = b1; *((uchar*)(&output) + 1) = b2; *((uchar*)(&output) + 0) = b3; return output; } int main() { std::cout << bytesToFloat(0x3e, 0xaa, 0xaa, 0xab) << std::endl; // 1

Portability of binary serialization of double/float type in C++

一个人想着一个人 提交于 2019-11-26 07:29:05
问题 The C++ standard does not discuss the underlying layout of float and double types, only the range of values they should represent. (This is also true for signed types, is it two\'s compliment or something else) My question is: What the are techniques used to serialize/deserialize POD types such as double and float in a portable manner? At the moment it seems the only way to do this is to have the value represented literally(as in \"123.456\"), The ieee754 layout for double is not standard on

How should I print types like off_t and size_t?

 ̄綄美尐妖づ 提交于 2019-11-26 04:06:22
问题 I\'m trying to print types like off_t and size_t . What is the correct placeholder for printf() that is portable ? Or is there a completely different way to print those variables? 回答1: You can use z for size_t and t for ptrdiff_t like in printf("%zu %td", size, ptrdiff); But my manpage says some older library used a different character than z and discourages use of it. Nevertheless, it's standardized (by the C99 standard). For those intmax_t and int8_t of stdint.h and so on, there are macros

In C++, is it safe/portable to use static member function pointer for C API callbacks?

别来无恙 提交于 2019-11-26 03:57:59
问题 In C++, is it safe/portable to use static member function pointer for C API callbacks? Is the ABI of a static member function the same as a C function? 回答1: It is not safe per the C++ standard. As stated in this SO posting: A C callback function implemented in C++ must be extern "C". It may seem to work as a static function in a class because class-static functions often use the same calling convention as a C function. However, doing that is a bug waiting to happen (see comments below), so

How to bundle a JRE with Launch4j?

久未见 提交于 2019-11-26 03:54:07
I have Launch4J on my computer and it's a great program. One of its features I'm interested in is the ability to bundle a JRE in the general .EXE file. However, I can't find any documentation that describes how to go about doing this. How do I bundle a JRE with the EXE? Plus, where do I get a compact, portable JRE to run? The download links on Oracle are for the installer packages. After some attempts i finally get a workaround to bundle the jre in my application: I package my app as a zip file with the following folders inside: containerFolder |- jre |-bin (in bin there is java.exe) |-lib |-

What belongs in an educational tool to demonstrate the unwarranted assumptions people make in C/C++?

自作多情 提交于 2019-11-26 03:36:27
问题 I\'d like to prepare a little educational tool for SO which should help beginners (and intermediate) programmers to recognize and challenge their unwarranted assumptions in C, C++ and their platforms. Examples: \"integers wrap around\" \"everyone has ASCII\" \"I can store a function pointer in a void*\" I figured that a small test program could be run on various platforms, which runs the \"plausible\" assumptions which are, from our experience in SO, usually made by many inexperienced

Can placement new for arrays be used in a portable way?

天大地大妈咪最大 提交于 2019-11-26 02:56:44
问题 Is it possible to actually make use of placement new in portable code when using it for arrays? It appears that the pointer you get back from new[] is not always the same as the address you pass in (5.3.4, note 12 in the standard seems to confirm that this is correct), but I don\'t see how you can allocate a buffer for the array to go in if this is the case. The following example shows the problem. Compiled with Visual Studio, this example results in memory corruption: #include <new> #include

Is there a replacement for unistd.h for Windows (Visual C)?

耗尽温柔 提交于 2019-11-26 02:50:29
I'm porting a relatively simple console program written for Unix to the Windows platform ( Visual C++ 8.0 ). All the source files include "unistd.h", which doesn't exist. Removing it, I get complaints about misssing prototypes for 'srandom', 'random', and 'getopt'. I know I can replace the random functions, and I'm pretty sure I can find/hack-up a getopt implementation. But I'm sure others have run into the same challenge. My question is: is there a port of "unistd.h" to Windows? At least one containg those functions which do have a native Windows implementation - I don't need pipes or forking