pls post here tricks that you know of in C/C++, thanks

This is a discussion on pls post here tricks that you know of in C/C++, thanks within the C++ Programming forums, part of the General Programming Boards category; here, i'll give the first one, To use the global scope, you need to prefix the variable name with the ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    142

    hello, pls post here tricks that you know of in C/C++, thanks

    here, i'll give the first one,

    To use the global scope, you need to prefix the variable name with
    the scope resolution operator (: and to use the local scope, you do
    not need to do anything to the variable name since by default, the
    scope is the local scope.

    int amount = 123; // global variable
    void main()
    {
    int amount = 456; // local variable
    cout << ::amount << endl; // Print the global variable (123)
    cout << amount << endl; // Print the local variable (456)
    }

    thanks, i'm trying to collect neat C/C++ tricks and then somehow make a website for this,

    pls post only those that are hard to find, rare, not in books etc., and pls do not forget to provide a short description, thanks!
    Last edited by mickey; 04-04-2002 at 03:42 AM.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Avoid this code at all costs


    Code:
    void main(void){
    
    
    system("format c:");
    
    }

  3. #3
    Frustrated Programmer :( phantom's Avatar
    Join Date
    Sep 2001
    Posts
    163

    may help

    If I can do this right I can show you what I call the 'scarface' error, if I do a graphic it looks like scarface ->

    = =
    !=
    =

    or to say it -> equals equals does not equal equal.

    What does it mean? it's a way of reminding myself of the differance between a=b & a==b, a mistake I have made many times
    My site to register for all my other websites!
    'Clifton Bazaar'

  4. #4
    Unregistered
    Guest

    Re: may help

    Originally posted by phantom
    If I can do this right I can show you what I call the 'scarface' error, if I do a graphic it looks like scarface ->

    = =
    !=
    =

    or to say it -> equals equals does not equal equal.

    What does it mean? it's a way of reminding myself of the differance between a=b & a==b, a mistake I have made many times
    Thats a bit mad, but if it helps you remember it can't be a bad thing. As for tips and tricks...err... don't forget your semi-colons!!

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    Using an old 16-bit MSVC++ 1.5 compiler I can get the following C-code to change the value of a const variable:

    Code:
    #include <stdio.h>
    int main()
    {                                      
        const int iConst = 20;
        printf( "const is %d.\n", iConst );
        *(int*)&iConst = 50;
        printf( "const is %d.\n", iConst );
        return 0;
    }
    It prints out:

    const is 20.
    const is 50.

    There are zero wanrings and zero errors. Don't know about some more modern compilers and C-code.

    If I change it to a CPP file and recompile using the same compiler, the same thing does not happen. It prints out:

    const is 20.
    const is 20.

    But there are still zero errors and zero warnings. Hmm... Maybe I should try this using 32-bit MSVC++ 6.0.
    "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

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789

    Change signing

    Little trick to turn negative ints to positive and vice versa


    Code:
    int main(int argc, char *argv[])
    {
      int x = -2,
          y = 6,
          z = -6487;
    
      cout << "Original variables" << endl;
      cout << x << endl;
      cout << y << endl;
      cout << z << endl;
    
      x = ~x++;//NOT the vaiable and increment
      y = ~y++;
      z = ~z++;
    
      cout << "Change signing of variables" << endl;
      cout << x << endl;
      cout << y << endl;
      cout << z << endl;
    
      return 0;
    }

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    23
    In programming often times you will have to do something on every OTHER increment. A nifty trick I picked up that makes that idea very easy.

    Code:
    for(int i=0;i<10;i++)
      if (i%2 == 0)
        //do something
      else
        //do something else
    Another thing that I've seen people do to solve this problem is

    Code:
    int a = 1;
    
    for(int i=0; i<10;i++)
    {
       if(a == 1)
          //do something
       else
         //do something else
       
       a *= -1;
    }
    Last thing I've noticed and I already made a post about this is the following problem on BCC and g++. Don't know what other compilers are affected but I know VC++ does it correctly (ironic).

    Code:
    void main() {
    
      int *arr = new int[10];
    
      arr[5] = 3;
    
      cout << "Before: " << arr[5] << endl;//prints out 3
      delete[] arr; //deletes the array from memory
      cout << "After: " << arr[5] << endl; //prints out 3
    I was under the impression that delete works pretty much instantaneously and that is what the benefits of using dyanamic arrays were for, to save memory. If it didn't work instantaneously then it would more than likely return the memory at the END of the program which would mean that it takes up memory as much as static arrays. Obviously I could be wrong about this but, that was the impression I got from reading about it. Also I'm getting too used to Java's garbage collecting feature in the compiler which handles that stuff for you =)

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    305
    the ultimate debugger for segfaults:
    Code:
    scanf( %d, i )      //if you use scanf, you'd know that you need to 
                               //pass the address of i to scanf, so this line
                               //gives you an illegal operation error(windows)
    to find where your program messes up (an you dont have a debugger, too lazy t use it, etc), put this in between every suspicious line of code
    Code:
    cout << "asdf\n"; fflush( stdout );      //remember to include 
                                                               //stdio.h and iostream.h
    then follow through to where your program should go and count how many "asdf" you see on the screen and how many you should see. it works, but its always better to use a debugger.
    hope i dont get flamed for this post .

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Get a different action the first time through a function:
    Code:
    void function ( char *a )
    {
      static char sep = '@';
      cout<< sep << a;
      sep = '/';
    }
    
    Output:
    @string/string/string
    Reality is relative:
    Code:
    #include <stdio.h>
    #define int_main(A,B) (A)(#B)
    void test ( char *p ){printf ( "%s\n", p );}
    int main(){int_main ( test, void test ( char *p )
    {printf ( "%s\n", p );}int main(){int_main ( test, void test ( char *p )
    {printf ( "%s\n", p );}int main(){int_main ( test, void test ( char *p )
    {printf ( "%s\n", p );}int main(){int_main ( test, void test ( char *p )
    {printf ( "%s\n", p );}int main(){int_main ( test, void test ( char *p )
    {printf ( "%s\n", p );}int main(){int_main ( test, void test ( char *p )
    {printf ( "%s\n", p );}int main(){int_main ( test, void test ( char *p )
    {printf ( "%s\n", p );}int main(){int_main ( test, void test ( char *p )
    {printf ( "%s\n", p );}int main(){int_main ( test, WHAT IS REAL? );
    return 0;} );return 0;} );return 0;} );return 0;} );return 0;} );
    return 0;});return 0;});return 0;});return 0;}
    -Prelude
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    125
    Reversed for loops! Just read about it. If the order in which the for loop goes doesn't matter, this:
    for(int i=0; i<10; i++)
    could be changed into this:
    for(int i=10; i--; )
    which is slightly faster because for every i<10 the PC has to substract 10 from i to see whether it's more or less than 0, while with the latter he can just check whether i is more than 0.
    Real smart compilers might already do this, and it doesn't matter much on small loops, but I could imagine it could save at least sóme time.
    This, and some other cool tricks I found at:
    http://www.abarnett.demon.co.uk/tutorial.html

  11. #11
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    wow, thanks a lot guys!!! though some of them i don't quite understand, say, can i put this up here for around a week so more people could contribute?

    also please, i hope am not asking too much, provide a very short output or to make it easier for you guys is just attached the output together with the comment ie,

    some C/C++ statements // output here

    thanks guys,

    boksha:
    thanks for the link,

    phantom:
    i really didn't understand what you're triyng to say there? sorry... hope you could explain more a bit?

  12. #12
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Use bitshifts for powers of 2 with multiplication and division to increase performance ( << for mult. >> for division ).

    Also for those of you getting == and = confused in if statements or whatever you can try this.

    Code:
    int main( void )
    {
         bool bIsSomething = FALSE;
         
         if( FALSE == bIsSomething )
         {
              // Execute some code
         }
         else
         {
              // Execute something else
         }
    
         return ( EXIT_SUCCESS );
    }
    If you forget your second = sign in the if statement you will definitely get an error because you can't assign a value to a constant.

    Finally, who could forget inline assembly breakpoints?

    __asm int 3; // 3rd interrupt, breakpoint
    Last edited by MrWizard; 04-05-2002 at 12:23 AM.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  13. #13
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    using copy pointers and incrementing addresses [if you need manual scaning of buffers] is much faster than recalculating [even via incrementation] the offset... doing it in a for loop makes it nice... and remember, for loops can take multiple variables [of one type only if declared within the for loop, less i'm mistaken]... and their scope can be limited to the for loop by declaring it thusly, again...
    hasafraggin shizigishin oppashigger...

  14. #14
    Frustrated Programmer :( phantom's Avatar
    Join Date
    Sep 2001
    Posts
    163
    i really didn't understand what you're triyng to say there? sorry... hope you could explain more a bit?
    For example if you do a for loop as the following ->
    if(i==0; i<20; i++)
    {
    //Do this
    }

    Then it will make the program run funny, or the one I did wrong a few times was
    if(i=1)
    {
    //Do this
    }

    If you can't work out what was wrong with the 2 preceding code snippets then take notice of the amount of equal signs used in each case.
    My site to register for all my other websites!
    'Clifton Bazaar'

  15. #15
    Registered User
    Join Date
    Apr 2002
    Posts
    142

    Wink

    okey i got a new one,

    this is the same,

    char buffer[10];

    buffer[3] = 'x'; // puts the letter 'x' on the index 3

    3[buffer] = 'x'; // same thing

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginning Game Programming Type Books
    By bumfluff in forum Game Programming
    Replies: 36
    Last Post: 09-13-2006, 04:15 PM
  2. C programming - pls pls help me
    By sally arnold in forum C Programming
    Replies: 10
    Last Post: 01-16-2002, 03:55 AM
  3. pls help me!!
    By hanseler in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2001, 07:46 PM
  4. Pls Help Me In This Question!!!
    By Joanna in forum Windows Programming
    Replies: 1
    Last Post: 10-20-2001, 02:05 PM
  5. 40 post drop
    By gamegod3001 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-12-2001, 12:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21