Language extension

This is a discussion on Language extension within the Contests Board forums, part of the Community Boards category; >what do you mean here? Code: int *a = malloc(5 * sizeof *a); int *p = &a[2]; /* Assuming success ...

  1. #31
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >what do you mean here?
    Code:
    int *a = malloc(5 * sizeof *a);
    int *p = &a[2];
    
    /* Assuming success */
    a = realloc(a, 10 * sizeof *a);
    printf("%d\n", *p);
    The pointer p may not point to valid memory after the call to realloc, so the printf call is unpredictable. It may work as expected or invoke undefined behavior. The only way to ensure that this doesn't happen is to reassign p before calling printf:
    Code:
    int *a = malloc(5 * sizeof *a);
    int *p = &a[2];
    
    /* Assuming success */
    a = realloc(a, 10 * sizeof *a);
    p = &a[2];
    printf("%d\n", *p);
    >are you the one who will judje our solutions? Do you have that much time? OR are you going to get paid for that?
    I wouldn't mind getting paid. But I'll find the time to judge.
    My best code is written with the delete key.

  2. #32
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >but the same problem would appear with the other method of malloc+free...
    Yes, that's true. But I prefer having a guarantee such as "this method will invalidate any pointers into the memory" instead of something potentially problematic like "this method might invalidate any pointers into the memory". The two approaches are similar in size, but the former gives a definite guarantee as to what will happen while the latter is wimpy and error prone.

    It's a stylistic choice though, so if you prefer realloc then more power to you.
    My best code is written with the delete key.

  3. #33
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Finally got the beginner done. Yeah bug free and everything. I just need to make a few more changes. First make all the code the same. Sometimes it is ! and then sometimes it is ==NULL. Almost there

  4. #34
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    How do I turn in my code

  5. #35
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Whoops, sorry about that. Email it to happyfrosty@hotmail.com
    My best code is written with the delete key.

  6. #36
    Registered User
    Join Date
    May 2004
    Posts
    6
    When is the closing date for the competition?

  7. #37
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    When is the closing date for the competition?
    Sign-up for the contest will last one month.
    Looks like today would be the last day for sign-ups.

  8. #38
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >When is the closing date for the competition?
    When everyone that signed up before today submits. But you don't have to be signed up to submit an entry.
    My best code is written with the delete key.

  9. #39
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Prelude
    >dont u think even the one u classified as a beginner category is way ahead for a beginner.
    It's just simple textual substitution. I think anyone who can read from a file and parse a simple string should have little trouble with it.
    You think so? Really?
    Code:
    int main( void )
    {
        int x[BUFSIZ];
    
        printf("%[10]",(3+1-7*4/11+200)[x]));
        return 0;
    }
    You mean to say it's simple string parsing for it to work with the results of something on the IOCCC which conformed to the standard? The above was just a quick example, but I'm sure there's way worse that would break it.

    Actually, I'd really like to see it done:
    Code:
    #include <stdio.h>
    #define W 1000
    int main( void )
    {
        int x = 0, y = , z[W];
        for(x=0,y=W;x<W;x++,y--)
            printf("%d",(z[z[w-x+y]])[z]);
    
        return 0;
    }
    Now with pure text subsitution, show me that all of those are in bounds at all times.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #40
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >You mean to say it's simple string parsing for it to work with the results of something on the IOCCC which conformed to the standard?
    You're referring to the intermediate task. But technically it's still textual substitution, even if the process of setting it up is more difficult. And for the sake of the contest I don't require the commutativity of subscripting to be taken into account, though if a solution does it then it would likely be good enough to win.

    >Now with pure text subsitution, show me that all of those are in bounds at all times.
    Well, that example would fail to compile, but assuming it was written in haste, the principle is the same. It's far from simple, but still text replacement.
    Last edited by Prelude; 06-24-2004 at 07:28 AM. Reason: I keep saying associative when I mean commutative.
    My best code is written with the delete key.

  11. #41
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Prelude
    >Now with pure text subsitution, show me that all of those are in bounds at all times.
    Well, that example would fail to compile, but assuming it was written in haste, the principle is the same. It's far from simple, but still text replacement.
    You can also have macro, string names, function calls in combination with [ ].

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #42
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >You can also have macro, string names, function calls in combination with [ ].
    So you're saying that it's impossible?
    My best code is written with the delete key.

  13. #43
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yeah. Pretty much. Unless you have a program with no user input at all, it'd be impossible to know for sure that functions which return a value which is used to index an array would return a value in range. You could always FORCE a fit, by wrapping a modulus of the array size around it some how, but I still think you couldn't do it due to the complexity of potential array name, brackets, value ordering.

    [edit]You'd have to actually have a program with no input whatsoever, and it couldn't use any random values, and all poniters would of course have to be initialized, as well as variables before using them, otherwise you could pull that "unknown" value... [/edit]

    [edit2]
    Actually...

    You might be able to do it if you wrote a program that would replace all [ ] chunks of code which would replace all operatins on arrays with function calls, and it would instead of being an actual array, be a dynamicly allocated chunk of memory which the only operations done on it were by said "operator overloading" functions.

    But if you're doing that, you may as well just write your own damn language...
    [/edit2]

    Quzah.
    Last edited by quzah; 06-24-2004 at 07:10 AM.
    Hope is the first step on the road to disappointment.

  14. #44
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    I disagree. This isn't a syntactic checker and it certainly isn't expected to do it at compile-time. The idea is to replace the subscript with a suitable assertion that will halt the program with a debug statement before there's any undefined behavior. Using a table of existing arrays and sizes, the parser should be able to tell what is an array subscript and what isn't (a simulated array with pointers isn't, btw). Because the index would be used more than once in the replacement text, a global variable for saving it can be defined by the preprocessor.

    In light of all that, it's not as impossible as you suggest, especially since I don't require a one pass algorithm. The hardest part is the parsing, and as I already said, I don't expect entries to be complete in that they handle every legal construct, just the most common/reasonable ones.
    My best code is written with the delete key.

  15. #45
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I don't expect entries to be complete in that they handle every legal construct, just the most common/reasonable ones.
    Then they'd be incomplete, and not a full working extension. If they can't parse the example I've given, and anything like it, then they can't be considered a full working extension.
    Code:
    char array[SIZE];
    0[array+functioncall()]=functioncall()[array];
    In otherwords, I bet I can break any "working" end result, using full ANSI compliant, no-warnings, no-errors, -pedantic code to do so.

    Quzah.
    Hope is the first step on the road to disappointment.

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

Similar Threads

  1. What language did they make Java in?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 07-03-2005, 04:18 PM
  2. Strange loop
    By D@rk_force in forum C++ Programming
    Replies: 22
    Last Post: 12-18-2004, 01:40 PM
  3. assembly language...the best tool for game programming?
    By silk.odyssey in forum Game Programming
    Replies: 50
    Last Post: 06-22-2004, 01:11 PM
  4. Language of choice after C++
    By gandalf_bar in forum A Brief History of Cprogramming.com
    Replies: 47
    Last Post: 06-15-2004, 01:20 AM
  5. Languages dying
    By Zewu in forum A Brief History of Cprogramming.com
    Replies: 31
    Last Post: 07-29-2003, 10:08 AM

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