Pages: « 1 2
  Print  
Author Topic: Which should I use?  (Read 21603 times)
Offline (Male) Josh @ Dreamland
Reply #15 Posted on: June 15, 2010, 10:59:27 am

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2950

View Profile Email
Eh, char* and char*& are two different things. Array != &Array... &Array will, should the array for some reason be given a register despite its use, dump the pointer into the memory and give you a reference to it. Assuming you meant Array == Array&, that isn't true, either. If you never state Array = something; in the code taking Array&, it will modify the array without requiring return. Taking Array as a parameter would simply copy the pointer, and only the local copy in the new scope would be changed.

:troll:

Now, in the context of his code, GCC will probably be nice and make it such that (int Array) is the same as (int &Array). So fine.
...But they're still different. :troll:
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Male) Rusky
Reply #16 Posted on: June 15, 2010, 11:18:03 am

Resident Troll
Joined: Feb 2008
Posts: 954
MSN Messenger - rpjohnst@gmail.com
View Profile WWW Email
Code: (c++) [Select]
int array[6] = { 1, 2, 3, 4, 5, 6 };
int function() {}

array == &array, just like function == &function.
Logged
Offline (Male) Josh @ Dreamland
Reply #17 Posted on: June 15, 2010, 03:36:35 pm

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2950

View Profile Email
Sure, but good luck passing the array to a function and keeping that true.
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Male) MahFreenAmeh
Reply #18 Posted on: June 15, 2010, 05:42:57 pm

sysadmin
"Web Team"
Location: Austin, TX
Joined: Apr 2008
Posts: 13
AOL Instant Messenger - sirmxe
View Profile WWW Email
DERP
Logged
sys(tem)admin(istrator)
[java,c++,c,javascript,html,css,php,perl,ruby,python,sql]
if you've got ideas, let me hear them.
Offline (Male) Rusky
Reply #19 Posted on: June 15, 2010, 09:12:13 pm

Resident Troll
Joined: Feb 2008
Posts: 954
MSN Messenger - rpjohnst@gmail.com
View Profile WWW Email
Code: (c++) [Select]
int array[3] = { 1, 2, 3 };
void function(int arg[]) {
  // arg == &arg
}
function(array); // array == arg == &array == &arg
Logged
Offline (Male) Josh @ Dreamland
Reply #20 Posted on: June 15, 2010, 10:28:48 pm

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2950

View Profile Email
Sorry, but outside of the scope in which it is declared, postfix [] and prefix * are synonymous.

Code: (C++) [Select]
#include <stdio.h>
int array[3] = { 1, 2, 3 };
void function(int arg[]) {
  puts((void*)arg == (void*)&arg ? "true" : "fail");
}
int main() {
  return function(array), 0;
}

Quote
fail
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Male) Rusky
Reply #21 Posted on: June 16, 2010, 09:21:56 am

Resident Troll
Joined: Feb 2008
Posts: 954
MSN Messenger - rpjohnst@gmail.com
View Profile WWW Email
Ah. That's lame.
Logged
Offline (Male) RetroX
Reply #22 Posted on: June 27, 2010, 09:07:33 am

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
Your parser broke.
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Male) Josh @ Dreamland
Reply #23 Posted on: June 27, 2010, 09:27:58 pm

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2950

View Profile Email
Doesn't seem to have?
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Male) RetroX
Reply #24 Posted on: July 02, 2010, 12:14:45 pm

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
Strange; it was showing an HTML entity at the time that I saw it.
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Unknown gender) The 11th plague of Egypt
Reply #25 Posted on: September 04, 2010, 04:08:46 pm
Member
Joined: Dec 2009
Posts: 274

View Profile
A little tip. Instead of
Code: [Select]
int i;
for(i=0; i < width; i++)
you could declare you variable at the start of the cycle, like this
Code: [Select]
for(int i=0; i < width; i++)Also, you can do a lot of cool things, like
Code: [Select]
for(int i=0, j=5; i < width; i++, j--)It's called comma operator, and it lets you do more in less space.

Moreover, some people suggest ++i instead of i++. Does the same thing, but is more efficient,
and is safer for recursive functions.
« Last Edit: September 04, 2010, 04:11:16 pm by The 11th plague of Egypt » Logged
Offline (Male) Josh @ Dreamland
Reply #26 Posted on: September 05, 2010, 09:55:16 am

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2950

View Profile Email
Quote
Moreover, some people suggest ++i instead of i++. Does the same thing, but is more efficient,
and is safer for recursive functions.

I'm getting a bit tired of that myth.

int i = 0;
cout << i++ << endl;
cout << ++i << endl;

The above prints 0 2. If we were to look at the micro operations, i++ would send to RAM, then increment; ++i would increment, then send to RAM. I.e., the same process, but with the order changed. Neither is more efficient. I've never heard that one is safer than the other for recursive functions. It's a matter of understanding which one will behave in the best manner for the current algorithm.
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Male) Rusky
Reply #27 Posted on: September 05, 2010, 10:07:31 am

Resident Troll
Joined: Feb 2008
Posts: 954
MSN Messenger - rpjohnst@gmail.com
View Profile WWW Email
Pre-increment is more efficient for objects you don't want to make copies of, otherwise the only notable difference is the behavior.
Logged
Offline (Male) Josh @ Dreamland
Reply #28 Posted on: September 05, 2010, 09:00:09 pm

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2950

View Profile Email
-sigh-
Yes, I suppose if you take all optimizations away, and are using a non-scalar object, then yes, a pre-increment is more efficient.
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Pages: « 1 2
  Print