Find the answer to your Linux question:
Results 1 to 3 of 3
So a while back when I was having some fun programming a small game in SDL, I noticed that code would compile in GCC and G++, but not MSV studio ...
  1. #1
    Linux Newbie rudie_rage's Avatar
    Join Date
    Jun 2007
    Location
    Canada
    Posts
    133

    Proper etiquette in C

    So a while back when I was having some fun programming a small game in SDL, I noticed that code would compile in GCC and G++, but not MSV studio 6.0.

    Looking into the errors a little further, the problem lied in some variable length arrays I had.

    apparently:

    int x;
    char array[x];

    compiles in some compilers, but not others unless x is declared to be a const int.

    Now in a way this makes perfect sense to me, since you cant have the length of an array changing at your every whim. array[10] cant become array[20] just because you will it so.

    But at the same time, I didnt think changing x would change the array length. for example:

    int x=10;
    char array[x];
    x=20;

    now array[x] should _still_ have 10 elements in it. changing x should not have altered the array one bit.

    So is the MS compiler being picky about const variables justified? Or is GCC too lenient with the syntax?

    on one hand changing the variable should not change the array, but on the other having a variable that can always be used to reference the last element of the array is mighty handy in almost every case.

    Just my random thought of the day. Not a life-altering question, just wondering what is actually involved in small syntax decisions like this.
    Living the digital dream....
    Disclaimer: I may be wrong since I was once before.
    Breathe out so I can breathe you in ~~Everlong

  2. #2
    Just Joined!
    Join Date
    Feb 2007
    Location
    Winnipeg, MB
    Posts
    14

    Lightbulb Arrays

    As I recall, the Microsoft compiler isn't strictly ANSI-compliant. Also, it used to be that 'int myArray[x];' wouldn't work, I think it must be something that was introduced in newer compilers.

    If you want something that should work on all systems:

    int x = 5;
    int *myArray = new int[x];

    //... use the array as normal...

    //You need to manually delete stuff when you use new
    delete myArray;

    //setting deleted stuff to 0 (or NULL) is considered good style
    myArray = 0;

    now array[x] should _still_ have 10 elements in it. changing x should not have altered the array one bit.
    Modifying x after the array is declared has no effect on the array size.

    You may also want to look into the STL vector class, which is basically a resizable array.
    Last edited by myrdos; 06-25-2007 at 10:15 PM. Reason: Fixed syntax.

  3. #3
    Linux Newbie rudie_rage's Avatar
    Join Date
    Jun 2007
    Location
    Canada
    Posts
    133
    Quote Originally Posted by myrdos View Post
    If you want something that should work on all systems:

    int x = 5;
    int *myArray = new int[x];

    //... use the array as normal...

    //You need to manually delete stuff when you use new
    delete myArray;

    //setting deleted stuff to 0 (or NULL) is considered good style
    myArray = 0;
    That code... Its so elegantly gorgeous.
    Theres definately beauty in properly formatted syntax, but when you spend the time and effort to go over the top not only in formatting but small details like this, it almost brings a tear to the eye.

    Call me crazy, but theres doing something, and then theres doing something right. The latter is much more satisfying, even if the end product works the same.

    Oh, and back to the topic at hand, thank you for the information. I'll do some more reading
    Living the digital dream....
    Disclaimer: I may be wrong since I was once before.
    Breathe out so I can breathe you in ~~Everlong

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...