Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

cMADsc

Pointers and Arrays

This topic is 6127 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am currently taking a C++ class at the Uni. :-), I tend not to pay attention because it is being taught at a first grade level. Although, one thing did catch my attention. The other day the teacher was talking about arrays and pointers, and he stated it is best to use this method when iteratating through an array using pointers: Manually incrementing the pointers and the array =============================== int array[100]; int ptr_array = &array[0]; for(int i=0; i<100; i++, ptr_array++) ===================================== Although, I usualy would just do: for(int i=0; i<100; i++) *ptr_array++; ====================== The teacher says my method would create extra overhead time. Furthermore, I do not pay much attention to my teacher because a few days ago he chose to pass an array by VALUE. A very large array, and showed the students that u don''t need a data type in front of main and other strange things. Good thing everything you want to know is in a book! ----------------------------- "There are ones that say they can and there are those who actually do." "...u can not learn programming in a class, you have to learn it on your own."

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by cMADsc
The teacher says my method would create extra overhead time.

Both of you suck, if you''re trying to figure who''s the better "optimized" programmer.
int array[100];
int *ptr = &array, *end = &array[99];
for(; ptr != end; ++ptr)
// loop body here

That''s basically how iterators work.

quote:
Furthermore, I do not pay much attention to my teacher because a few days ago he chose to pass an array by VALUE.

Arrays are always passed by reference in C++. Always.

quote:
...[he] showed the students that u don''t need a data type in front of main and other strange things.

What do you mean data type "in front"? If main doesn''t have a return type (like any other function), it will fail to compile. If he specified return type void for main, or indeed any return type other than int, then he hasn''t caught up with the standard.

If, however, he declared it as int main(void), then he''s perfectly right.

Moral? Pay attention to your teacher, twerp.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
He was probably pointing out that doing a loop via pointer arithmetic is faster than array indexing, as in:

  
for (i=0; i<100; i++, ptr++)
// use *ptr


is faster than:

for (i=0; i<100; i++)
// use array[i]




which is usually true, depending on the optimization. Your two examples will generate virtually identical code, assuming a non-brain-dead optimizer:

  
for (i=0; i<100; i++, ptr++)
// use *ptr


for (i=0; i<100; i++)
// use *ptr++



Also, I believe your prof is correct on the two other points. Routines without a declared return are assumed to return int, as is the main procedure. And passing an array via a parameter will not pass it by value; arrays cannot be passed by value.

I''d suggest not discarding everything your prof tells you out of hand. If you believe he has stated something incorrect.y, look it up to double check. Remember, there''s always something to learn.

Share this post


Link to post
Share on other sites
"*ptr_array++;" increments the value stored in the first element of the array, NOT the pointer to the array.

lookup pointer dereferencing....

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

Share this post


Link to post
Share on other sites
Actually I went home and wrote a program with no data type in front of Main() and it compiled and ran,but I did get a warning.
Using MSVC 6.0. Doesn't the copy constructor get called when I am passing an array in as the parameters. Furthermore, good point OldGuy, there is always more to learn! Also, I expected better from you Oluseyi, since you are a moderator. Oh well, I would of used a vector anyway. Try it out for yourself.
===================================

#include < iostream.h>
main()
{
cout << " Oluseyi sucks donkey balls.\n";
}
===================================


-----------------------------
"There are ones that say they can and there are those who actually do."

"...u can not learn programming in a class, you have to learn it on your own."







Edited by - cMADsc on February 1, 2002 2:35:22 PM

Edited by - cMADsc on February 1, 2002 2:35:57 PM

Share this post


Link to post
Share on other sites
no data type, is assumed "signed int", period.

no arrays do not get copied, arrays are "basically" constant pointers.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
sorry jenova but you''re about "*ptr_array++;" It DOES increment the pointer increase, not the value at the pointer.

int a[] = {1, 5, 8, 9};
int *p = a;
cout << *(p++) << endl;
cout << *p++ << endl;

What''s the output?

1
5

Look up order of operations...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I think *end = &array[99] is wrong. The "end" iterator should not point to the last element, but just after. So (&array[99])+1

don''t leave off the return type on int functions, it is like swearing at a tea party. Plus it only works in C, though C++ compilers might let it pass.

Use ++i instead of i++. For built in types such as ints or pointers both have the same performance. For iterators and other types the i++ version requires an extra temporary. Plus it is more appealing to the eye, I switched over before I heard about the performance issue.

Share this post


Link to post
Share on other sites
Yes, knowing the precedence order of operators is important (or, if you are like me, with a bad memory, dogearing the page in the manual). The first time I saw this code like this:

  
char tobuf[100], frbuf[100], *t, *f;
strcpy(frbuf, "Hello, World");
t = tobuf;
f = frbuf;
while (*f)
*t++ = *f++;
*t = ''\0'';


I almost had a stroke trying to figure it out the first time. The key is knowing (or looking up) the precedence of the operators. In *f++, ++ has a higher precedence than *, which means it is applied first. However, it is a postfix operator, so the effects take place later (another way to think about it is that ++ increments the variable but returns the previous value). Then, the indirection operator gets the value pointed to by ''f''. The effect is that *f++ returns the value pointed to by f, THEN increments f to the next address.

Which comes to another point. The above is commonly accepted usage, and you should get used to it. But, you can produce some very odd-looking expressions in C++. Sometimes clarity is better than cleverness, so a rule of thumb is to try to avoid overly complex code unless absolutely necessary. I always get a chuckle when I break this rule, and come back several weeks/months later to try to figure out what my code is actually doing. I cuss out the damned programmer who that pile of garbage, and then my ears burn red...

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!