a funny test^_^

This is a discussion on a funny test^_^ within the C++ Programming forums, part of the General Programming Boards category; hi,guys, i saw a interesting program today,the source code is: Code: #include <iostream> using namespace std; char a[] = "abcdefg"; ...

  1. #1
    Registered User zouyu1983's Avatar
    Join Date
    Nov 2006
    Location
    Fuzhou University, Fujian, China
    Posts
    35

    Wink a funny test^_^

    hi,guys, i saw a interesting program today,the source code is:
    Code:
    #include <iostream>
    using namespace std;
    
    char a[] = "abcdefg";
    char b[] = "1234567";
    char c[] = "980123456789";
    
    int main()
    {
    
    	strcpy(a, c);
    	strcpy(b, c);
    	cout << a << endl << b << endl << c << endl;
    	return 0;
    }
    now , i let's have a test, please give the output of the program without computer and post it, then compile the program and run it in your computer, is your answer right?

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    Yes, it is.
    ...and what was this a test of, exactly? Undefined behavior?
    Last edited by SlyMaelstrom; 11-25-2006 at 11:09 PM.
    Sent from my iPad®

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    5,843
    The code yields undefined behaviour, which means that anything is allowed to happen, whether we describe it or not.

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    afaik, the output is undefined (does the standard make any guarantees of variables being stored in contiguous memory?)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Registered User zouyu1983's Avatar
    Join Date
    Nov 2006
    Location
    Fuzhou University, Fujian, China
    Posts
    35
    just give the ouput of the program
    the program runs correctly

  6. #6
    Registered User zouyu1983's Avatar
    Join Date
    Nov 2006
    Location
    Fuzhou University, Fujian, China
    Posts
    35
    the output is unique
    and the program will not induce a runtime error

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    Quote Originally Posted by JaWiB
    (does the standard make any guarantees of variables being stored in contiguous memory?)
    I do get the feeling that where ever s/he got that test from, they were expecting the variables to be defined contiguously.
    Quote Originally Posted by grumpy
    which means that anything is allowed to happen
    I'm not sure you could say anything could happen. In fact, on any standard operating system you could really limit the result to one of two things, I suppose.
    Quote Originally Posted by zouyu1983
    and the program will not induce a runtime error
    Says who? Are you going to run it a 1000 times on a 1000 systems to be sure of that?
    Last edited by SlyMaelstrom; 11-25-2006 at 11:18 PM.
    Sent from my iPad®

  8. #8
    Registered User zouyu1983's Avatar
    Join Date
    Nov 2006
    Location
    Fuzhou University, Fujian, China
    Posts
    35
    the standard make guarantee of the global variables being stored contiguously in the static area of the memory
    Last edited by zouyu1983; 11-25-2006 at 11:20 PM.

  9. #9
    Registered User zouyu1983's Avatar
    Join Date
    Nov 2006
    Location
    Fuzhou University, Fujian, China
    Posts
    35
    Quote Originally Posted by SlyMaelstrom
    Says who? Are you going to run it a 1000 times on a 1000 systems to be sure of that?
    you can try it ^_^, and i am sure the output is unique without induring any problem

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    Quote Originally Posted by zouyu1983
    the standard make guarantee of the variables being stored contiguously in the static area of the memory
    I'd have to see where it said that in the standard; However, I have no reason not to believe you. However, in assembly, if memory is defined to be placed on full word boundaries, then there could be straggling bytes in between. However, the only assembly I really know is 360 Assembler. X86 may work differently for all I know.
    Sent from my iPad®

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    5,843
    Quote Originally Posted by zouyu1983
    the standard make guarantee of the global variables being stored contiguously in the static area of the memory
    The standard makes no such guarantee. There is certainly no specific guarantee, in this case, that a, b, and c will be adjacent to each other in memory.

    You are making the mistake of assuming that, because you see a particular response on your OS with your compiler, that response is somehow standard or universal.

  12. #12
    Registered User zouyu1983's Avatar
    Join Date
    Nov 2006
    Location
    Fuzhou University, Fujian, China
    Posts
    35
    Quote Originally Posted by grumpy
    The standard makes no such guarantee. There is certainly no specific guarantee, in this case, that a, b, and c will be adjacent to each other in memory.

    You are making the mistake of assuming that, because you see a particular response on your OS with your compiler, that response is somehow standard or universal.
    you mean that in ANSI C++ , there is no guarantee aboult it?
    maybe i make mistakes.
    i will review the standard.
    anyway, thanks^_^

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,336
    Quote Originally Posted by draft c99 standard
    When two pointers are compared, the result depends on the relative locations in the
    address space of the objects pointed to. If two pointers to object or incomplete types both
    point to the same object, or both point one past the last element of the same array object,
    they compare equal. If the objects pointed to are members of the same aggregate object,
    pointers to structure members declared later compare greater than pointers to members
    declared earlier in the structure, and pointers to array elements with larger subscript
    6.5.7 Language 6.5.8ഊWG14/N869 Committee Draft — January 18, 1999 87
    values compare greater than pointers to elements of the same array with lower subscript
    values. All pointers to members of the same union object compare equal. If the
    expression P points to an element of an array object and the expression Q points to the
    last element of the same array object, the pointer expression Q+1 compares greater than
    P. In all other cases, the behavior is undefined.
    What this means is that pointer comparison is only defined within the same object.
    Essentially, this means
    if ( &a[0] < &b[0] )
    is undefined because you're comparing pointers to two different objects.

    Being undefined, it means you can make no statement about whether a comes before b, or how much padding there might be between a and b, or anything else useful.

    So your program is undefined.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    5,843
    Quote Originally Posted by zouyu1983
    you mean that in ANSI C++ , there is no guarantee aboult it?
    Yep. In ANSI C++ as well as in ISO C++ (the ANSI C++ standard is also the ISO C++ standard).
    Quote Originally Posted by zouyu1983
    maybe i make mistakes.
    As do we all.

  15. #15
    Registered User zouyu1983's Avatar
    Join Date
    Nov 2006
    Location
    Fuzhou University, Fujian, China
    Posts
    35
    i'm sorry, the output of the program is undefined
    now give the answer i test

    in windows xp sp2 vc.net 2003, the output is:
    98012345980123456789
    980123456789
    6789

    in windows xp sp2 g++, the output is
    98012345980123456789
    980123456789
    6789

    but in linux + g++, the output is
    6789
    980123456789
    980123456789

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Funny Windows Error Codes (I rofled...)
    By Sentral in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 06-01-2006, 09:38 AM
  2. funny things
    By Benzakhar in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-10-2004, 10:06 AM
  3. Object Oriented - Funny story
    By MethodMan in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 09-26-2002, 02:21 PM
  4. Funny
    By Imperito in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 08-21-2002, 05:41 PM
  5. For some reason, it prints funny characters ???
    By Nutshell in forum C Programming
    Replies: 8
    Last Post: 01-14-2002, 03:27 PM

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