C++Guns – RoboBlog

11.05.2017

C++ Guns - Why isn't there a swap function in Fortran and C?

Filed under: Allgemein — Tags: , , — Thomas @ 09:05

Because Fortran and C sucks. To implement a generic swap function which works for any type and any custom type, one needs templates. Think about usually variables which store a value. Templates are variables which store types. But Fortran and C do not have template techniques. So one must implement a swap function for every […]

26.01.2016

Evaluation of logical operations

Filed under: Allgemein — Tags: , , — Thomas @ 21:01

Spass mit dem Standard. Aus dem Fortran 90/95 Standard [1] Kapitel 7.1.7.6. Evaluation of logical intrinsic operations The rules given in 7.2.4 specify the interpretation of logical intrinsic operations. Once the interpretation of an expression has been established in accordance with those rules, the processor may evaluate any other expression that is logically equivalent, provided […]

29.05.2015

warning: conversion to 'float' from 'int' may alter its value [-Wconversion] (Bug of the day 7)

Filed under: Allgemein — Tags: , , — Thomas @ 22:05

Another bug of the day 😀 You can do the following float var = float(vari); // yes I know I'm loosing precision int var = int(std::floor(varf)) // yes I know interger can overflow int var = qFloor(varf) // Qt version Same for double. warning: comparison between signed and unsigned integer expressions [-Wsign-compare] Tricky one. Usually […]

20.05.2015

-1 (Bug of the day 5)

Filed under: Allgemein — Tags: , — Thomas @ 19:05

vector vecB; vecB.resize(vecA.size()-1); for(size_t i = 0; i < vecA.size(); ++i) { ... vecB[i] = value ... } Na, wer findet ihn? Bei so etwas findet ich QT's QVector wieder besser als den Standard c++ vector. Qt macht zumindest bei Debug Einstellung eine BereichsĂźberprĂźfung. Ausser es wird die at() Funktion statt den operator() benutzt. Dann […]

16.05.2015

integer overflow debugger trap

Filed under: Allgemein — Tags: , — Thomas @ 08:05

Benutzt man 16bit Integer statt 32bit um Speicher zu sparen und seine cache misses zu optimieren, läuft man Gefahr den Zahlenbereich von -32768 bis +32767 zu verlassen. So wie es fĂźr Floatingpoint Zahlen ÜberprĂźfungen auf over/underflow etc gibt, die ein Signal werfen bzw den Compiler anspringen lassen, so gibt es das auch fĂźr Integer. Ist […]

18.02.2015

rosettacode - Simple moving average

Filed under: Allgemein — Tags: , , — Thomas @ 12:02

http://rosettacode.org/wiki/Averages/Simple_moving_average#C.2B.2B The original rosetta C example shows the usage of variable function arguments. The C++ example shows how to implement a circular buffer. They are confusing and inefficient. My Version shows how to implement a simple moving average using modern c++ techniques. #include < iostream > #include < vector > class SMA { public: SMA(int […]

02.02.2015

more crazy C

Filed under: Allgemein — Tags: — Thomas @ 09:02

#include #define CACHE 256 enum { h_unknown = 0, h_yes, h_no }; unsigned char buf[CACHE] = {0, h_yes, 0}; int happy(int n) { int sum = 0, x, nn; if (n < CACHE) { if (buf[n]) return 2 - buf[n]; buf[n] = h_no; } for (nn = n; nn; nn /= 10) x = nn […]

02.01.2015

raw speed of C using OO with C++

Filed under: Allgemein — Tags: , , — Thomas @ 20:01

Programs written in C are very very fast. Those written in C++ can be slower due to the fact that copying object is not alway easy as copying an array of numbers. One have to call the copy constructor maybe with side effects like allocate some space. The language C++ is full backwards compatible to […]

16.11.2014

Spass mit Pointer

Filed under: Allgemein — Tags: , — Thomas @ 20:11

"man kann sich so derbe den kopf weg schiessen damit" int global; char *func() { return (char*)&global; } int main() { char *x = func(); int *RocketPtr = (int*)x; *RocketPtr = 1337; cout

20.10.2014

howto allocate array in C++ and pass it back to fortran

Filed under: Allgemein — Tags: , , — Thomas @ 12:10

test.cpp #include < stdlib.h > // fortran can only call C routines extern "C" { // we need the address of the fortran pointer thus a pointer of a pointer. void myC_func(int **arr) { // we cannot use new() here because fortran deallocate() use free() instead of delete[] *arr = (int*) malloc(12*sizeof(int)); for(int i=0; i […]

Older Posts »

Powered by WordPress