Pages: [1] 2
  Print  
Author Topic: Which should I use?  (Read 6296 times)
Offline (Unknown gender) freezway
Posted on: March 30, 2010, 07:46:15 PM

Member
Joined: Dec 2009
Posts: 220

View Profile
Ok, I'm learning C++ and it has occured there are about 3 ways to do the same thing. Here are the 3 functions i wrote up. Is there any difference in their function? are any faster? slower? more standards compliant? are magical?
Code: [Select]
void printArray(int Array, int width, int height)
{
    int i,j;
    for(i=0; i < width; i++)
    {
        for(j=0; j < height; j++)
        {
            cout << Array[i][j];
        }
        cout << endl;
    }
}

void printArray(int& Array, int width, int height)
{
    int i,j;
    for(i=0; i < width; i++)
    {
        for(j=0; j < height; j++)
        {
            cout << Array[i][j];
        }
        cout << endl;
    }
}

void printArray(int * Array, int width, int height)
{
    int i,j;
    for(i=0; i < width; i++)
    {
        for(j=0; j < height; j++)
        {
            cout << *(Array + (i*width) + j)
        }
        cout << endl;
    }
}

Logged
if you drop a cat with buttered toast strapped to its back, which side lands down?
joshdreamland: our languages are based on the idea that it's going to end up FUBAR
/kick retep998
Offline (Male) Josh @ Dreamland
Reply #1 Posted on: March 30, 2010, 07:58:08 PM

Prince of all Goldfish
Developer
Location: Ohio, United States
Joined: Feb 2008
Posts: 2946

View Profile Email
You meant int** for the first parameter to the first function?
Anyway, yes, there is a difference between all of those.

In a function parameter,
int a; specifies that "a" is one integer, local to this function. The memory is copied (not a very costly procedure; there's no improving it) for use by the function.
int &a; signifies that "a" is passed as a reference, but treated like an integer all the same. This time, the address is copied, which can be of equal or lesser size than the actual integer. This allows the function to edit the contents of "a" in the scope from which it was passed. This is the only time you should use & in parameters.
int *a; is pointer to int; it's an integer marking an integer in memory, and must be dereferenced explicitly with * or implicitly with [] for use.
int **a; is a pointer to a pointer to an int; it's an integer that points to the address of the integer you want. You can dereference it with any combination of [] and *.
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 (Unknown gender) freezway
Reply #2 Posted on: March 30, 2010, 08:14:44 PM

Member
Joined: Dec 2009
Posts: 220

View Profile
You meant int** for the first parameter to the first function?

No. I didn't. I seem to remember seeing a Cpp tutorial that did that, Is it even legal?
Logged
if you drop a cat with buttered toast strapped to its back, which side lands down?
joshdreamland: our languages are based on the idea that it's going to end up FUBAR
/kick retep998
Offline (Male) Josh @ Dreamland
Reply #3 Posted on: March 30, 2010, 08:18:36 PM

Prince of all Goldfish
Developer
Location: Ohio, United States
Joined: Feb 2008
Posts: 2946

View Profile Email
Well, that is, but Array[][] isn't if array is just int.
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 (Unknown gender) freezway
Reply #4 Posted on: March 30, 2010, 08:23:12 PM

Member
Joined: Dec 2009
Posts: 220

View Profile
oh, whoops, forgot those []'s. ok, this is good, much less confusing thx.
Logged
if you drop a cat with buttered toast strapped to its back, which side lands down?
joshdreamland: our languages are based on the idea that it's going to end up FUBAR
/kick retep998
Offline (Male) RetroX
Reply #5 Posted on: March 30, 2010, 09:12:08 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
Just a random thing to mention: add a space between the items in the array.

Also, although people don't seem to realize it, references/pointers use memory, too (4 or 8 bytes, depending on whether or not the system is 32-bit or 64-bit).  However, considering how an integer is already 32 bytes, and you're using a 2-dimensional array, using a pointer make more sense.  Any array can be assumed as a pointer, and vis versa.  Additonally, this is just me, personally, but unsigned integers should be used in unsigned cases.  size_t is the same size as a pointer, and an unsigned integer, and it's what you want to use for arrays and such.

This is ideal:
Code: [Select]
void print_array(int **array, int width, int height)
  {
  for (size_t i=0;i<width;i++)
    {
    for (size_t j=0;j<height;j++)
      {
      std::cout << (i>0 && j>0 ? " " : "") << array[i][j];
      }
    std::cout << std::endl;
    }
  }
« Last Edit: March 30, 2010, 09:13:39 PM by RetroX » 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 (Female) serprex
Reply #6 Posted on: March 31, 2010, 01:27:22 PM
Smooth ER
Developer
Joined: Apr 2008
Posts: 106

View Profile WWW
size_t is not defined to be the size of a pointer. size_t is only defined to be the type returned by sizeof. C doesn't define intersections between pointers and integers
Logged
Offline (Male) RetroX
Reply #7 Posted on: March 31, 2010, 07:36:19 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
size_t is not defined to be the size of a pointer. size_t is only defined to be the type returned by sizeof. C doesn't define intersections between pointers and integers
Sorry; what I meant is that on 32-bit systems, size_t is 32-bit (4 bytes), and pointers are also 32-bit (4 bytes).  It's 8 bytes for 64-bit systems.

Same size as a pointer.
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) MahFreenAmeh
Reply #8 Posted on: June 09, 2010, 06:02:51 PM

sysadmin
"Web Team"
Location: Austin, TX
Joined: Apr 2008
Posts: 15
AOL Instant Messenger - sirmxe
View Profile WWW Email
you should throw your computer out the window.
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 #9 Posted on: June 09, 2010, 09:55:48 PM

Resident Troll
Joined: Feb 2008
Posts: 960
MSN Messenger - rpjohnst@gmail.com
View Profile WWW Email
So basically, your three examples do the exact same thing using different syntax. Use whichever looks nicest.
Logged
Offline (Unknown gender) luiscubal
Reply #10 Posted on: June 10, 2010, 12:29:16 PM
Member
Joined: Jun 2009
Posts: 452

View Profile Email
You're missing the [][] in all cases, not just the first.

In this case, all three are basically the same. However, note that when *calling* the function, there are differences(array vs &array. when using plain integers instead of int** or int[][] there's also the problem of using constants with reference members).
Finally, if instead of int[][] we were talking about some big data member, such as a huge class/struct, then using the * or & would be use less memory and would probably be faster.
Logged
Offline (Male) RetroX
Reply #11 Posted on: June 14, 2010, 08:02:32 AM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
int& does not make an array; it makes a reference to a single integer.  References shouldn't be used for arrays.
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) Rusky
Reply #12 Posted on: June 14, 2010, 10:03:38 AM

Resident Troll
Joined: Feb 2008
Posts: 960
MSN Messenger - rpjohnst@gmail.com
View Profile WWW Email
array == &array
Logged
Offline (Male) retep998
Reply #13 Posted on: June 14, 2010, 01:29:15 PM

Member
Location: Where else?
Joined: Jan 2010
Posts: 248
MSN Messenger - retep998@charter.net AOL Instant Messenger - retep998 Yahoo Instant Messenger - retep998
View Profile Email
array == &array
:troll:
Logged
Offline (Male) Rusky
Reply #14 Posted on: June 14, 2010, 02:54:33 PM

Resident Troll
Joined: Feb 2008
Posts: 960
MSN Messenger - rpjohnst@gmail.com
View Profile WWW Email
O_o
Logged
Pages: [1] 2
  Print