Why would they use this segment instead of this one?

This is a discussion on Why would they use this segment instead of this one? within the C++ Programming forums, part of the General Programming Boards category; Why use this segment Code: #include <iostream> using namespace std; int main () { int numbers[5]; int * p; p ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    1

    Why would they use this segment instead of this one?

    Why use this segment

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
      int numbers[5];
      int * p;
      p = numbers;  *p = 10;
      p++;  *p = 20;
      p = &numbers[2];  *p = 30;
      p = numbers + 3;  *p = 40;
      p = numbers;  *(p+4) = 50;
      for (int n=0; n<5; n++)
        cout << numbers[n] << ", ";
      return 0;
    }
    instead of this segment?

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
      int a[5];
      int *b;
      b = a;  *b = 10;
      b = a;  *(b+1) = 20;
      b = a;  *(b+2) = 30;
      b = a;  *(b+3) = 40;
      b = a;  *(b+4) = 50;
      for (int n=0; n<6; n++)
        cout << a[n] << ", ";
      return 0;
    }
    Does the first, seemingly more complicated block do something more than the second block? Even though both print the same thing?

    NOTE in the first block, p is equivalent to b in the second block. and "numbers" is equivalent to a.
    Last edited by hannahfox123; 06-18-2010 at 01:11 AM.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Obviously you would do it for educational purposes, to demonstrate various ways you can access an offset from a pointer.

    By the way, the second version iterates one element too many on output.

    In the real world, what you would do is
    Code:
    int numbers[] = { 10, 20, 30, 40, 50  };
    and leave all the dangerous pointer stuff out.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    The Autodidact Dante Wingates's Avatar
    Join Date
    Apr 2010
    Location
    Valhalla
    Posts
    56

    Thumbs down Too much needless instructions

    Simply because they are slow, and take too many lines to do something pretty simple and easy:

    Code:
    int main()
    {
      int a[5], *b = a;
      for(int i = 0; i < 5;)
          cout << (*(b+i) = (++i * 0xA)) << (char)0x20;
      return 0;
    }
    Also, why not simply use a pointer from the start?

    Code:
    int main()
    {
      int *a = new int[5];
      for(int i = 0; i < 5;)
          cout << (*(a+i) = ((i++ + 0x14 / 0x2 + i) * 0x5) - 0x28) << (char)0x20;
      return 0;
    }

    you could also do it using inline asm, then it would get even more ureadable, if that is your goal.


    As you can see, there is a lot of ways of making something more and more unreadable. Use your criativity.
    Last edited by Dante Wingates; 06-18-2010 at 04:23 AM.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    Quote Originally Posted by Dante Wingates View Post
    Simply because they are slow, and take too many lines to do something pretty simple and easy:

    Code:
      cout << (*(b+i) = (++i * 0xA)) << (char)0x20;
    This line causes undefined behaviour because some compilers will evaluate 'i' before the assigment and others will evaluate ++i after the assignment first

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 05:04 AM
  2. execute a code in data segment...
    By ravindragjoshi in forum Windows Programming
    Replies: 1
    Last Post: 06-13-2007, 12:43 AM
  3. shmget returning invalid segment size specified
    By BobS0327 in forum Linux Programming
    Replies: 6
    Last Post: 11-08-2006, 06:39 PM
  4. Declare an array in the BSS segment
    By ^xor in forum C Programming
    Replies: 1
    Last Post: 05-27-2005, 06:12 PM
  5. LISP (DrScheme) any one?
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-31-2004, 12:52 PM

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