Quote:His code is extremely bad anyway, even for C.
If that is "extremely bad", please show us your solution written in C. We would like to learn great code. It needs to be written in C. Not C++, not D, C.
Quote:His code is extremely bad anyway, even for C.
Quote:Original post by dashurc
Is the XOR swap actually faster though??? I was always under the impression (from what I'd been taught) that it was useful on embedded systems when you were working under extremely tight memory constraints (where you might not even have memory to put the temp variable on the stack).
I've never actually profiled this stuff (and I really don't care to at the moment).
Quote:By Sneftel
"The compiler can do better" is one compelling argument. To the compiler, where possible, std::swap means "switch the register allocations", and masses-of-XOR means "masses of XOR". The big thing, though, is pipelining. a^=b unavoidably ties up both a and b for one clock cycle, and then unavoidably keeps a tied up for the next clock cycle. Since the next instruction needs to read back from a, this means that in the simplest possible case, the three instructions are completely serialized, taking six cycles.
Now consider the temp case. The first op, t=a, takes two cycles as well, but as soon as the first cycle is complete, a is no longer tied up, and a=b can start executing. Likewise, after one more cycle, b is no longer tied up, and b=t can start executing. In this same simple pipelined processor, the total is four cycles.
Quote:Original post by Trienco
In other words and as someone has said before: if you do anything more complicated than calling reverse(someString) in C++, you didn't understand the task ,-)
Quote:Original post by apatriarca
If with inline initialization you mean the initialization at declaration than it works on Visual Studio. Can you post an example of this strange behavior?
In C89 the variables should be declared at the start of the block, C99 have removed this limitation (but Visual Studio don’t support C99). I don’t know if there are compilers with 100% C99 support. It isn’t a very successful standard.
for (int x = 0; x < 10; x++) // THIS IS ILLEGAL C :({// Do something}int x = 0;for (x = 0; x < 10; x++) // Happy now{// Do something}
void blah(){printf("Foo\n");printf("Bar\n");int x = 0; //BOOM!! This should appear at the very top of the function apparently... C++ allows you to declare wherever but apparently C89/99/WTF Visual Studio is using doesn't}
char* foo = "bar"; // VS blew up; this compiled fine under gccchar* foo;foo = "bar"; // Happy now, providing char* foo is at the top of the function
Quote:Original post by ukdeveloper
Also, Visual Studio didn't like stuff like this (I don't have any means of testing this right now but I remember doing stuff like this and it didn't work):
*** Source Snippet Removed ***
And, IIRC but as I say I haven't had time to check this, VS didn't like:
*** Source Snippet Removed ***
Quote:a) Me still missing the point of C and not getting how it works (which seems to be radically different to C++ in a few ways; the syntax is very clumsy and I often find things either don't work at all or do work, but don't do what you expect and with little obvious reason for it)
Quote:Original post by ukdeveloper
a) Me still missing the point of C and not getting how it works (which seems to be radically different to C++ in a few ways; the syntax is very clumsy and I often find things either don't work at all or do work, but don't do what you expect and with little obvious reason for it)
Quote:b) VS being picky about ancient legacy code (wouldn't surprise me)
Quote:c) both of the above.
Quote:Original post by ukdeveloper
And, IIRC but as I say I haven't had time to check this, VS didn't like:
*** Source Snippet Removed ***
char* foo2 = "bar"; // VS blew up; this compiled fine under gccint main(){ char* foo = "bar"; // VS blew up; this compiled fine under gcc return 0;}
Quote:Original post by apatriarca
@ Zahlman: Your code don’t compile on my compilers. You have written char[] source but I think the only valid way to declare an array in C is putting the [] after the identifier. So it should be char source[]. I think is just a typo.
Quote:Original post by Trienco
"Good" old C has plenty of things to look out for. The most obvious is the annoying way you need to use typedefs, because structs/enums are pretty much anonymous.
So (from hazy memory):
struct Bla {};
won't work, instead you need to do
typedef struct {} Bla;
essentially typedef'ing the unnamed struct to Bla. Confusing compared to
struct Bla {} obj;
which in C++ declares the named struct and creates a global instance.
Also, you can't just use
Bla obj;
but need to use
struct Bla obj;
struct foo { int bar; };foo f; /* illegal, type foo undefined */struct foo f; /* fine */typedef struct foobar { int bar; } foo;foo f; /* fine */struct foobar f; /* also fine */struct foo f; /* illegal, type struct foo undefined */typedef struct { int bar; } foo; /* works on most compilers, however some old/weird compilers disallow anonymous structs */
Quote:Original post by Zahlman
EDIT: Yeah, I suppose actually moving the pointers used for iteration would be a good idea. And of course someone noticed before I could correct it. That's what you get for writing that much by way of explanation, I guess. X_X
for (; begin < end; ++begin, --end) { char c = *begin; *begin = *end; *end = c;}