"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 << global;
}
Output : 1337
"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 << global;
}
Output : 1337
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 < 12; i++) {
(*arr)[i] = i;
}
}
}
test.F90
! https://gcc.gnu.org/onlinedocs/gcc-4.6.2/gfortran/Working-with-Pointers.html
program main
use iso_c_binding
implicit none
interface
subroutine my_routine(arr) bind(c,name='myC_func')
import :: c_ptr
! If a pointer is a dummy-argument of an interoperable procedure,
! it usually has to be declared using the VALUE attribute.
! void* matches TYPE(C_PTR), VALUE, while TYPE(C_PTR) alone matches void**.
type(c_ptr), intent(out) :: arr
end subroutine
end interface
type(c_ptr) :: cptr
integer,pointer :: fptr(:)
! allocate and fill cptr
call my_routine(cptr)
! convert it to fortran pointer
call c_f_pointer(cptr, fptr, [12])
if(associated(fptr)) write(*,*) "associated"
write(*,*) fptr
deallocate(fptr)
end program main
g++ -Wall -Wextra -c test.cpp gfortran -Wall -Wextra -fcheck=all test.F90 test.o valgrind ./a.out ==5256== Memcheck, a memory error detector ==5256== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al. ==5256== Using Valgrind-3.9.0 and LibVEX; rerun with -h for copyright info ==5256== Command: ./a.out ==5256== associated 0 1 2 3 4 5 6 7 8 9 10 11 ==5256== ==5256== HEAP SUMMARY: ==5256== in use at exit: 0 bytes in 0 blocks ==5256== total heap usage: 22 allocs, 22 frees, 11,874 bytes allocated ==5256== ==5256== All heap blocks were freed -- no leaks are possible ==5256== ==5256== For counts of detected and suppressed errors, rerun with: -v ==5256== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
Sehr schöne anschauliche Erklärung:
int main() {
const char *a = "I can feel segfaults";
printf("%c\n", (char*) * (char*) * (a+strlen(a)));
return 0;
}
Folgender Code:
#include < iostream >
#include < stdint.h >
using namespace std;
class Base {
public:
int32_t a;
};
class Derived : public Base {
public:
int32_t b;
};
class VirtualDerived : virtual public Base {
public:
int32_t c;
};
int main() {
cout << "sizeof Base " << sizeof(Base) << "\n";
cout << "sizeof Derived " << sizeof(Derived) << "\n";
cout << "sizeof VirtualDerived " << sizeof(VirtualDerived) << "\n";
return 0;
}
ergibt auf einer 32Bit Maschine folgende Ausgabe
sizeof Base 4 sizeof Derived 8 sizeof VirtualDerived 12
Die Größe der VirtualDerived Klasse ist also nicht einfach die Summer zweier 4Byte Variablen, sondern auch noch zusätzlich ein 4Byte Pointer auf die Base Klasse.
Auf einer 64Bit Maschine sind es entsprechend 16Byte
sizeof Base 4 sizeof Derived 8 sizeof VirtualDerived 16
Leute, ist sag es schon die ganze Zeit: Fortran ist eine tote Sprache.
Und damit meine ich nicht nur diese verbuggten Compiler. Oder dass es ewig braucht bis man ein neues Feature vom Compiler unterstützt wird. Im Fortran 2003 Standard sind zwar schön viele OO Ansatze festgelegt, aber ich kenne kein Compiler der den Standard nach 10 Jahre komplett unterstützt. Andere Sprachen sind schon 2 Schritte weiter.
Nein, diesmal habe ich viel bessere Argumente auf Lager. In dem Paper [1] wurden 800000 Software Projekte und 13000 Programmierer ausgewertet. Ich möchte nur zu einem Bild aus diesem Paper meine Meinung loswerden. Dieses da:
Also, man sieht hier die Wahrscheinlichkeit, mit der man die Programmiersprache wechselt, wenn ein neues Projekt angefangen wird. Weis bis blau sind niedrige Wahrscheinlichkeiten und rot bis schwarz hohe.
Die Punkte auf der Diagonalen zeigen, dass man die Sprache NICHT wechselt.
Was haben wir denn da alles:
Rote Punkte sehe ich bei allesn *Basic Sprachen. Auch bei der C-Familie mit Java, Perl, PHP, Python bleiben die Programmierer bei ihrer Sprache.
Welche Sprachen bekommen Zuwachs? Das sieht man schön an den vertikalen Balken. Also Java, C/C++ und meinetwegen noch PHP. Das sind also die view Großen wohin die Programmierer wechseln, nachdem sie erkannten, dass ihre vorherige Wahl Schrott war. Tja, wagt es nicht das Wort gegen C++ zu erheben ;) Die Sprache ist sowas von geil und wird den ganzen anderen Schrott längerfristig schon tot trampeln ;)
Wohin rennen denn die Fortran Leute? JAVA. Ach ich lach mich kaputt. Das sind doch total verschiedene Welten.
Ich wette die Auftraggeber wollten parallelisierte hoch performante Software. Haben aber keine Programmierer gefunden der was aufn Kasten hat, und der Rest kam mit der Sprache nicht klar. Also wurde es wieder Java. Das ist ja bekannt und jeder kann es. Und jetzt ärgern die sich mit ihrere total aufgeblasenen, lahmen Software rum, HäHä.
Die *Basic sprachen sind auch alle hinfällig. Zwar wechseln die Leute hier nicht, aber es kommen auch keine neuen hinzu. Die Sprache stirbt also aus, wenn auch alle *Basic Programmierer ausgestorben sind. Sehr einfach Das ;)
[1] Empirical Analysis of Programming Language Adoption; Leo A. Meyerovich; Ariel S. Rabkin
Für eine .a lib:
ar -ru test.a 1.o 2.o
Für eine große .o Datei:
ld -r objectList -o destObject
First read http://gcc.gnu.org/install/
Make sure you have all Prerequisites installed!
Download all files to ~/opt/downloads
Install all packages to ~/opt/
Download gcc 4.8 from ftp://ftp.gwdg.de/pub/misc/gcc/snapshots/LATEST-4.8/ and unpack it.
wget ftp://ftp.gwdg.de/pub/misc/gcc/snapshots/LATEST-4.8/gcc-4.8-20120930.tar.bz2
tar xjf gcc-4.8-20120930.tar.bz2
cd gcc-4.8-20120930
Download several necessary librarys into the gcc source directory and unpack them.
They will be built together with GCC.
wget ftp://ftp.gmplib.org/pub/gmp-5.0.5/gmp-5.0.5.tar.bz2
tar xjf gmp-5.0.5.tar.bz2
ln -s gmp-5.0.5 gmp
wget http://www.mpfr.org/mpfr-current/mpfr-3.1.1.tar.bz2
tar xjf mpfr-3.1.1.tar.bz2
ln -s mpfr-3.1.1 mpfr
wget http://www.multiprecision.org/mpc/download/mpc-1.0.1.tar.gz
tar xzf mpc-1.0.1.tar.gz
ln -s mpc-1.0.1 mpc
Now configure and build
mkdir $HOME/opt/downloads/gcc-4.8-build
cd $HOME/opt/downloads/gcc-4.8-build
../gcc-4.8-20120930/configure --prefix=$HOME/opt/gcc-4.8 --enable-threads --enable-languages=c,c++,fortran
make
make install
Troubleshooting
/usr/bin/ld: cannot find crt1.o: No such file or directory
/usr/bin/ld: cannot find crti.o: No such file or directory
These files are usually locates in /usr/lib32/ and/or /usr/lib/x86_64-linux-gnu/
Add this to your .bashrc
export LIBRARY_PATH=/usr/lib/x86_64-linux-gnu/:$LIBRARY_PATH
Farbe in der Konsole ist nicht schwer.
Beispiele in C und Fortran:
#include < unistd.h >
void resetFont() {
if (isatty (fileno (stdout)))
std::cout << "\e[0m";
}
void setFontRed() {
if (isatty (fileno (stdout)))
std::cout << "\e[1;31m";
}
void setFontGreen() {
if (isatty (fileno (stdout)))
std::cout << "\e[32m";
}
subroutine resetFont()
implicit none
! this stuff is to print an escape sequenz and omit a newline
write(*,'(A)', advance='no') achar(27)//"[0m"
end subroutine
subroutine setFontRed()
implicit none
write(*,'(A)', advance='no') achar(27)//"[1;31m"
end subroutine
subroutine setFontGreen()
implicit none
write(*,'(A)', advance='no') achar(27)//"[32m";
end subroutine
Da Fortran per default keine Escape Sequenzen interpretiert (man muss das per Compiler Flag einschalten), kann man die achar() Funktion nutzen. Sie wandelt ein Integer auch in nicht-druckbare ASCII Zeichen um.
Auf diese Weise kann man sich auch eine gotoxy() Funktion bauen.
int array[2][2]={{0,1}, {2,3}};
printf("element: %i\n", *( *( array + (0,1)) + (0,1) ) ) ;
kater@ktux:~$ bin/gcc/bin/gcc -Wall test.c
test.c: In function ‘main’:
test.c:87:43: warning: left-hand operand of comma expression has no effect [-Wunused-value]
test.c:87:52: warning: left-hand operand of comma expression has no effect [-Wunused-value]
Welche Zahl wird ausgegeben? 0, 1, 2 oder 3?
Tipp 1:
Der erste *operator entscheidet ob man in {0,1} oder {2,3} Array landet. Der zweite *operator entscheidet dann zwischen der 0, 1 bzw 2,3.
Tipp 2:
a = (1,2); wird zu a = 2;
Thx @BjoernC
Powered by WordPress