Thread: Uncommented code makes for a bad day

  1. #1
    Registered User Landslyde's Avatar
    Join Date
    Sep 2014
    Location
    Dallas, TX
    Posts
    3

    Uncommented code makes for a bad day

    I'm about one week into Jumping Into C++, an e-book I purchased from this website. While I have struggled with the information on pointers, re-reading the sections a few times, it started making a little sense. However, today I ran in to a bit of code in the book that left me scratching my head.

    Code:
    int *growArray (int* p_values, int *size)
    {
       *size *= 2;
       int *p_new_values = new int[ *size ];
       for ( int i = 0; i < *size; ++i )
       {
       p_new_values[ i ] = p_values[ i ];
       }
       delete [] p_values;
       return p_new_values;
    }
    This function is from page 151. And my question about is:

    What, exactly, does *= mean? I don't get it. And there's nothing in the book that explains it to me. This is not easy, wrapping my mind around pointers and references. Commenting the code would have made all of this easier to grasp. I'm new at this, but one of the first things I learned was a good programmer comments his code so that the next person looking at it has some idea of what's going on. And since this is a book of instruction, I believe commenting the code examples throughout would have made the book much more user friendly.
    Anyways, will someone please help me with *=

    Thanks,
    Landslyde

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,398
    Code:
    a *= b
    Means the same thing as

    Code:
    a = a * b
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User Landslyde's Avatar
    Join Date
    Sep 2014
    Location
    Dallas, TX
    Posts
    3
    Of course it does. It's just a multiplication sign. What threw me was that I'm right in the middle of pointers, and seeing *= made me think it was some kind of pointer declaration. And I got lost really fast. Like I said, I'm new at this...but tomorrow's another day

    Thank you, brewbuck

    Landslyde

  4. #4
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by Landslyde View Post
    Of course it does. It's just a multiplication sign. What threw me was that I'm right in the middle of pointers, and seeing *= made me think it was some kind of pointer declaration. And I got lost really fast. Like I said, I'm new at this...but tomorrow's another day

    Thank you, brewbuck

    Landslyde
    I think the only time a * operator (used as pointer) is to the right of the thing it's binding to is when you have a pointer to a type without an identifier:

    Code:
       iSize = sizeof( int* ); /// iSize is assigned with size of a pointer to int
    Other than that it'll be on the left of whatever it binds to:

    Code:
       int* a, b; /// a is pointer to int, b is int
    There might be other cases where it will go to the right, but I can't think of one.

    Anyway, I recommend choosing whether to put the * operator beside the type-name or beside the identifier. Consistency will help you identify things in your own code at a glance (hopefully ).
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  5. #5
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by brewbuck View Post
    Code:
    a *= b
    Means the same thing as

    Code:
    a = a * b
    Not in C++.

  6. #6
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by Yarin View Post
    Not in C++.
    Is this a reference to operator overloading? Because I was under the same impression.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,778
    Probably, but that's the normal semantics, so you better have good reason to differ when overloading the operator.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by laserlight View Post
    Probably, but that's the normal semantics, so you better have good reason to differ when overloading the operator.
    To troll the poop out of anyone attempting to decipher your code. The best way to write secure code is to obfuscate it with the most over-the-top and ridiculous things you can think of. That isn't programming in BrainF***

  9. #9
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    Quote Originally Posted by MutantJohn View Post
    The best way to write secure code is to obfuscate it with the most over-the-top and ridiculous things you can think of.
    That was sarcasm, yes? If so, you'd best say so or else someone might read your post and actually believe it.

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The code posted would seem to have an error. It attempts to access index positions from the original array p_values that are likely out of bounds with regards to that array. The value of i in the for loop should really only go up to *size / 2.
    Code:
    int *growArray (int* p_values, int *size)
    {
       *size *= 2;
       int *p_new_values = new int[ *size ];
       for ( int i = 0; i < *size; ++i )
       {
       p_new_values[ i ] = p_values[ i ];
       }
       delete [] p_values;
       return p_new_values;
    }
    I suppose you could have had a dynamically allocated array of 10 positions say and called the above function with a size argument of 5 instead of the full 10 and it would work... creating a new array also of size 10 but with only the first 5 of the original array's values copied over.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  11. #11
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by antred View Post
    That was sarcasm, yes? If so, you'd best say so or else someone might read your post and actually believe it.
    Oh, I was being super sarcastic.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. no error of compilation but my code makes codeblocks bug
    By funnydarkvador in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2013, 12:31 AM
  2. Using less malloc and free makes code _slower_
    By chlorine in forum C Programming
    Replies: 21
    Last Post: 08-12-2011, 09:03 AM
  3. Advice requested, Code makes sense to me, not compiler
    By andrew.bolster in forum C Programming
    Replies: 53
    Last Post: 01-06-2008, 01:44 PM
  4. What Makes A CPU Get Hot?
    By SMurf in forum Tech Board
    Replies: 12
    Last Post: 10-15-2006, 05:36 AM
  5. This makes me SO mad!!
    By minesweeper in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 12-06-2002, 06:34 PM