portability

Correct, portable way to interpret buffer as a struct

霸气de小男生 提交于 2019-11-29 21:00:45
The context of my problem is in network programming. Say I want to send messages over the network between two programs. For simplicity, let's say messages look like this, and byte-order is not a concern. I want to find a correct, portable, and efficient way to define these messages as C structures. I know of four approaches to this: explicit casting, casting through a union, copying, and marshaling. struct message { uint16_t logical_id; uint16_t command; }; Explicit Casting: void send_message(struct message *msg) { uint8_t *bytes = (uint8_t *) msg; /* call to write/send/sendto here */ } void

How to design a C / C++ library to be usable in many client languages? [closed]

旧巷老猫 提交于 2019-11-29 19:51:56
I'm planning to code a library that should be usable by a large number of people in on a wide spectrum of platforms. What do I have to consider to design it right? To make this questions more specific, there are four "subquestions" at the end. Choice of language Considering all the known requirements and details, I concluded that a library written in C or C++ was the way to go. I think the primary usage of my library will be in programs written in C, C++ and Java SE , but I can also think of reasons to use it from Java ME, PHP, .NET, Objective C, Python, Ruby, bash scrips, etc... Maybe I

C - get type alignment portably

这一生的挚爱 提交于 2019-11-29 19:24:42
问题 I'm writing really small interpreter for a very simple language, which allows for simple structure definitions (made of other structures and simple types, like int, char, float, double and so one). I want fields to use as little alignment as possible, so using max_align_t or something similar is out of question. Now, I wonder if there is a nicer way to get alignment of any single type other than this: #include <stdio.h> #include <stddef.h> #define GA(type, name) struct GA_##name { char c;

C++ integral constants + choice operator = problem!

倖福魔咒の 提交于 2019-11-29 19:16:43
问题 I have recently discovered an annoying problem in some large program i am developing; i would like to understand how to fix it in a best way. I cut the code down to the following minimal example. #include <iostream> using std::cin; using std::cout; class MagicNumbers { public: static const int BIG = 100; static const int SMALL = 10; }; int main() { int choice; cout << "How much stuff do you want?\n"; cin >> choice; int stuff = (choice < 20) ? MagicNumbers::SMALL : MagicNumbers::BIG; //

Why does glibc's strlen need to be so complicated to run quickly?

a 夏天 提交于 2019-11-29 18:36:09
I was looking through the strlen code here and I was wondering if the optimizations used in the code are really needed? For example, why wouldn't something like the following work equally good or better? unsigned long strlen(char s[]) { unsigned long i; for (i = 0; s[i] != '\0'; i++) continue; return i; } Isn't simpler code better and/or easier for the compiler to optimize? The code of strlen on the page behind the link looks like this: /* Copyright (C) 1991, 1993, 1997, 2000, 2003 Free Software Foundation, Inc. This file is part of the GNU C Library. Written by Torbjorn Granlund (tege@sics.se

What development tools do you carry on your USB drive? [closed]

三世轮回 提交于 2019-11-29 18:34:04
I've just bought a new 4GB USB thumb drive and I'm trying to decide what to put on it. I'm thinking about one of the webserver on a stick packages, a C/C++ IDE (leaning toward Code::Blocks; had Dev-C++ on my old USB drive) and Python. What development related tools do you carry around with you on yours? Update I've added categories. IDEs Code::Blocks Open source, cross platform C/C++ IDE Supports several compilers (that you must supply) but you can also download a version that includes MingW. (There's a FAQ question on their website explaining how to make it portable) Codelite -- Open-source,

Multithreading in C++ … where to start?

二次信任 提交于 2019-11-29 17:13:10
问题 I'd like to start learning multithreading in C++. I'm learning it in Java as well. In Java, if I write a program which uses multithreading, it will work anywhere. However in C++, doesn't multithreading rely on platform-specific API's? If so, that would seem to get in the way of portability. How can I do multithreading in C++ without causing portability issues? Is boost's thread library a good solution? As a sidenote - how is it even possible to implement multithreading as a library? Isn't

Is there a portable Perl? [closed]

走远了吗. 提交于 2019-11-29 16:58:12
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . Is there a portable Perl along the lines of portable Python? Something I could use (while learning the stuff) from my thumb drive? Oh, and I'm talking about Window XP. 回答1: I can heartily recommend Strawberry Perl. The Portable version is on Beta (the real meaning of Beta, though), click here to get it. EDIT:

Are IEEE float and double guaranteed to be the same size on any OS?

送分小仙女□ 提交于 2019-11-29 16:03:32
I'm working on a OS portable database system. I want our database files to be OS portable so that customers can move their database files to other kinds of OS's at their discretion. Because of this use case I need my data types to be consistent across OS's, and I'm wondering if IEEE float's and double's are guaranteed to be the same byte size on any OS? C++ says almost nothing about the representation of floating point types. [basic.fundamental]/8 says (Emphasis mine): There are three floating point types: float , double , and long double . The type double provides at least as much precision

Portable json module in jython

半腔热情 提交于 2019-11-29 14:59:19
I am making a python script with jython and I need to use the json module that dosent exist in jython 2.5 . Do any of you guys know a way to include a module as a single file that can be moved around with the script without installing it on the host's jython . I was planning on using the simple json module i found on pypi If it helps. omercnet Try http://opensource.xhaus.com/projects/jyson A fast JSON codec for jython 2.5, written in java. Jython 2.7.0 now includes the standard library json module, which is reasonably fast now that it has been ported to Java. I ran the JSON benchmarks in the