tausch()
-
bzw. auf die compiler einstellungen :p
-O0
0x0804848b <main+57>: mov -0x8(%ebp),%edx 0x0804848e <main+60>: mov -0xc(%ebp),%eax 0x08048491 <main+63>: cmp %eax,%edx 0x08048493 <main+65>: je 0x80484bc <main+106> 0x08048495 <main+67>: mov -0x8(%ebp),%eax 0x08048498 <main+70>: mov %eax,%edx 0x0804849a <main+72>: mov -0xc(%ebp),%eax 0x0804849d <main+75>: xor %edx,%eax 0x0804849f <main+77>: mov %eax,-0x8(%ebp) 0x080484a2 <main+80>: mov -0xc(%ebp),%eax 0x080484a5 <main+83>: mov %eax,%edx 0x080484a7 <main+85>: mov -0x8(%ebp),%eax 0x080484aa <main+88>: xor %edx,%eax 0x080484ac <main+90>: mov %eax,-0xc(%ebp) 0x080484af <main+93>: mov -0x8(%ebp),%eax 0x080484b2 <main+96>: mov %eax,%edx 0x080484b4 <main+98>: mov -0xc(%ebp),%eax 0x080484b7 <main+101>: xor %edx,%eax 0x080484b9 <main+103>: mov %eax,-0x8(%ebp)
0x080484d6 <main+132>: lea -0xc(%ebp),%eax 0x080484d9 <main+135>: mov %eax,0x4(%esp) 0x080484dd <main+139>: lea -0x8(%ebp),%eax 0x080484e0 <main+142>: mov %eax,(%esp) 0x080484e3 <main+145>: call 0x804842b <tausch> 0x0804842b <tausch>: push %ebp 0x0804842c <tausch+1>: mov %esp,%ebp 0x0804842e <tausch+3>: sub $0x10,%esp 0x08048431 <tausch+6>: mov 0xc(%ebp),%eax 0x08048434 <tausch+9>: mov (%eax),%eax 0x08048436 <tausch+11>: mov %eax,-0x4(%ebp) 0x08048439 <tausch+14>: mov 0x8(%ebp),%eax 0x0804843c <tausch+17>: mov (%eax),%edx 0x0804843e <tausch+19>: mov 0xc(%ebp),%eax 0x08048441 <tausch+22>: mov %edx,(%eax) 0x08048443 <tausch+24>: mov 0x8(%ebp),%edx 0x08048446 <tausch+27>: mov -0x4(%ebp),%eax 0x08048449 <tausch+30>: mov %eax,(%edx) 0x0804844b <tausch+32>: mov $0x1,%eax 0x08048450 <tausch+37>: leave 0x08048451 <tausch+38>: ret
mir gefällts wie immer super
-
du xorst ptrdiff_t)a)^((ptrdiff_t)b ?
wie soll denn da je was vernünftiges bei rauskommen?
-
aus der coder hölle schrieb:
kommt auf die cpu an oder
Kommt auf den Coder an.
#include <stdio.h> #define tausch(a,b) {char *c=a; a=b; b=c;} int main (void) { char *a="ABC"; char *b="DEF"; printf("1:(a|b): (%s|%s)\n",a,b); tausch(a,b); printf("2:(a|b): (%s|%s)\n",a,b); }
Keep it small and simple, baby.
-
Vorallem zeigt es ein fehlendes Verständnis der Sprache, ergo 6.
-
Torben schrieb:
Allerdings bekam ich dafür einen Rüffel der Art "setzen 6!".
Kann mir jemand sagen, wie man das besser macht.Ich empfehle nachzufragen. Bist du sicher, dass du die Aufgabenstellung richtig wiedergegeben hast? Waren a und b vielleicht Arrays, und keine Zeiger?
aus der coder hölle schrieb:
#include <stddef.h> #define swapPointer(a,b) if(a!=b){\ a = (void*)(((ptrdiff_t)a)^((ptrdiff_t)b));\ b = (void*)(((ptrdiff_t)b)^((ptrdiff_t)a));\ a = (void*)(((ptrdiff_t)a)^((ptrdiff_t)b));\ } ...
Wieder einmal ein Musterbeispiel für portablen Code.
-
Shade Of Mine schrieb:
Vorallem zeigt es ein fehlendes Verständnis der Sprache, ergo 6.
Dieses Forum ist niveaumässig sowieso unterste Schublade.
-
@logisch, ne?
#define tausch(a,b) {char *c=a; a=b; b=c;}
das ist scho c89 tauglich oder
@µngbd
wieso soll das nicht portabel sein?du meinst so wie hier?
#define swapPointer(a,b) __asm__ __volatile__( \ "cmp %%ebx,%%eax\n" \ "je OUT\n" \ "xor %%ebx,%%eax\n" \ "xor %%eax,%%ebx\n" \ "xor %%ebx,%%eax\n" \ "OUT:\n" \ :"=a"(a),"=b"(b) \ :"a"(a), "b"(b) \ );
was mit -O0 :p ergibt
0x0804848c <main+58>: mov -0xc(%ebp),%eax 0x0804848f <main+61>: mov -0x10(%ebp),%ebx 0x08048492 <main+64>: cmp %ebx,%eax 0x08048494 <main+66>: je 0x804849c <main+74> 0x08048496 <main+68>: xor %ebx,%eax 0x08048498 <main+70>: xor %eax,%ebx 0x0804849a <main+72>: xor %ebx,%eax 0x0804849c <main+74>: mov %eax,-0xc(%ebp) 0x0804849f <main+77>: mov %ebx,-0x10(%ebp)
kann ja keiner wissen das der compiler mit meinen ideen nicht umgehen kann, wobei auch die paar zeilen schon scheiße sind, aber das mal ein anderes thema...
-
aus der coder hölle schrieb:
@logisch, ne?
#define tausch(a,b) {char *c=a; a=b; b=c;}
das ist scho c89 tauglich oder
Sollte man meinen.
-
logisch, ne? schrieb:
aus der coder hölle schrieb:
@logisch, ne?
#define tausch(a,b) {char *c=a; a=b; b=c;}
das ist scho c89 tauglich oder
Sollte man meinen.
dachte immer das ist so eine gcc extension?
-
aus der coder hölle schrieb:
@µngbd
wieso soll das nicht portabel sein?Weil du Zeiger zu Ganzzahlen castest?
TC2 7.17 schrieb:
The types are
ptrdiff_t
which is the signed integer type of the result of subtracting two pointers;
...
-
aus der coder hölle schrieb:
dachte immer das ist so eine gcc extension?
Was ist eine GCC-Extension? Makros? Variablen am Anfang von Blöcken? Initialisierung von Variablen bei der Deklaration?
-
#define tausch(a,b) {char *c=a; a=b; b=c;}
Das ist natürlich C89 und auch C99 und auch K&R-C. Aber an diesem Beispiel kann man gut ein anderes Problem beobachten, das mit Makros in den meisten Sprachen besteht. Nämlich der Name der temporären Variable
c
. Wenn einer der beiden Parameter schonc
heisst, kommt man in Schwierigkeiten.Man könnte versuchen, Abhilfe zu schaffen, indem man einen möglichst unwahrscheinlichen Namen statt
c
nimmt, aber das ist offensichtlich keine Lösung. Aus dem Grund ist es ganz allgemein besser, für solche Fälle Inline-Funktionen zu nehmen, dazu hätte es gereicht, dem Vorschlag von Torben eininline
voranzustellen. Andernfalls müsste man ein Makrosystem einführen, das garantiert unbenutze Symbole generieren kann.
-
ja habs verstanden, das euch ein xor-swap auf ints und pointer im speziellen nicht gefällt
-
aus der coder hölle schrieb:
ja habs verstanden, das euch ein xor-swap auf ints und pointer im speziellen nicht gefällt
Es geht nicht darum, ob es uns gefällt, sondern ob wir annehmen dürfen, dass ein C-Compiler sowas in ein lauffähiges Programm übersetzen kann. Mit zwei int's wäre das kein Problem, denn Each of the operands shall have integer type.
-
µngbd schrieb:
#define tausch(a,b) {char *c=a; a=b; b=c;}
Das ist natürlich C89 und auch C99 und auch K&R-C. Aber an diesem Beispiel kann man gut ein anderes Problem beobachten, das mit Makros in den meisten Sprachen besteht. Nämlich der Name der temporären Variable
c
. Wenn einer der beiden Parameter schonc
heisst, kommt man in Schwierigkeiten.Dann mach es so:
#define tausch(a,b) {char *v##__LINE__=a; a=b; b=v##__LINE__;}
-
logisch, ne? schrieb:
Dann mach es so:
#define tausch(a,b) {char *v##__LINE__=a; a=b; b=v##__LINE__;}
Schlaue Idee. Leider kann es immer noch eine Variable geben, die v123 heisst, wenn die Zahl zufällig stimmt. Wenn man dieses Problem vollständig elimieren will, muss man einen neuen Präprozessor einführen:
http://en.wikipedia.org/wiki/Hygienic_macro
-
µngbd schrieb:
Schlaue Idee. Leider kann es immer noch eine Variable geben, die v123 heisst, wenn die Zahl zufällig stimmt.
Wie hoch schätzt Du die Wahrscheinlichkeit ein, dass das zu Problemen führt?
-
logisch, ne? schrieb:
Wie hoch schätzt Du die Wahrscheinlichkeit ein, dass das zu Problemen führt?
Mit
gcc -Wshadow
? Null.
Sonst? EOD.
-
Ich kann's mir doch nicht verkneifen:
Shriram Krishnamurthi schrieb:
Many macro systems, such as that of C, are not hygienic. Programmers sometimes try to circumvent this by
using hideous identifier names, such as __macro_result_. This is not a solution!1. Not only is a it painful to have to program this way, small typos would greatly increase development
time, and the macro would be much harder to decipher when a programmer tries to modify or correct
it later.2. This solution is only as good as the programmer’s imagination; the problem still persists, lying in wait
for just the right (wrong!) identifier to be bound in the context of use. Indeed, while a programmer
may choose a sufficiently obscure name from the perspective of other programmers, not all source is
written by humans. A tool generating C code (such as a Scheme-to-C compiler) may happen to use
exactly this naming convention.3. This name is only obscure “upto one level”. If the macro definition is recursive, then recursive in-
stances of the macro may interfere with one another.
4. If you use this macro to debug the source that contains the macro (e.g., compiling the C compiler
using itself), then your carefully-chosen “obscure” name is now guaranteed to clash!
-
Dein Programm war ja mal totaler Unsinn. Hast du überhaupt eine Ahnung, was * und & bedeuten?
int tausch(char *a, char *b) { char *d; d=b; b=a; a=d; return 0; } int main(int argc, char *argv[]) { char * a="ABC"; char * b="DEF"; printf("1:(a|b): (%s|%s)\n",a,b); tausch(a,b); printf("2:(a|b): (%s|%s)\n",a,b); return 0; }
P.S.: Man gibt 0 zurück, falls alles in Ordnung ist.