Array declaration

This is a discussion on Array declaration within the C Programming forums, part of the General Programming Boards category; Newbie questions: In the following code snippet I define an array pt with ntri *3 elements. ntri is a command ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    8

    Thumbs up Array declaration

    Newbie questions:

    In the following code snippet I define an array pt with ntri*3 elements. ntri is a command line parameter and therefore not available at runtime. Surely the compiler cannot know what size pt is. Something tells me I should malloc that array - is this necessary and to what size? I'm from a FORTRAN background and used to dynamic allocation of arrays. Any direction would be appreciated.

    Code:
    typedef struct 
    {
    	float x,y,z;
    } point;
    
    int main(int argc, char *argv[])
    {
    	int ntri=atoi(argv[1]);
    	point pt[ntri*3];
    
    //do something with pt ... 
    
    	return 0;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Standard C (including C99) allows only constant values in the expression in []. Some C compilers DO allow variable values in there, but that's not standards compliant, so if you want your code to be portable to many compilers, then you're better off using malloc - that works even in C89, which doesn't even allow
    Code:
    const int n = 8;
    int array[n];
    ...
    The other potential drawback of using the construct you describe is that if for some reason atoi returns either a negative value or a LARGE value [and you have no checks in your code to validate that the value is "small and positive"] is that the stack would overflow and the application crash. With malloc, you get back NULL if you use more space than available - and it's often a much larger space than the stack can accomodate in the first place, so you have a better chance of that working in the first place.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    8
    Thanks Mats

    That's what I suspected. I'm using gnu c, that didn't complain, even with -Wall and I don't have lint, which would probably have picked that up. This is going to be a long learning process. I'll try googling for a freeware C99 compiler.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Eh? C99 has variable-length arrays, just not extern ones. I don't know if you're planning to use this array elsewhere, or not, but what you have so far is fine.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    The other potential drawback of using the construct you describe is that if for some reason atoi returns either a negative value or a LARGE value [and you have no checks in your code to validate that the value is "small and positive"] is that the stack would overflow and the application crash.
    Just a side question here: do VLAs have to be allocated on the stack?

    Some people seem to think not. http://bytes.com/groups/c/770297-c99...ay#post3070032

    On the other hand, it's true that it would be quite inefficient for compilers to implement VLAs using the heap, at least in such a way that you cannot have memory leaks.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,315
    Quote Originally Posted by dwks
    Just a side question here: do VLAs have to be allocated on the stack?

    Some people seem to think not. http://bytes.com/groups/c/770297-c99...ay#post3070032
    I am not sure, but I suspect that they have to be allocated on the stack. Consider:
    Quote Originally Posted by C Standard, 1999 edition, section 6.2.4 paragraph 4
    An object whose identifier is declared with no linkage and without the storage-class specifier static has automatic storage duration.
    Quote Originally Posted by C Standard, 1999 edition, section 6.7.5 paragraph 3
    A full declarator is a declarator that is not part of another declarator. The end of a full declarator is a sequence point. If the nested sequence of declarators in a full declarator contains a variable length array type, the type specified by the full declarator is said to be variably modified.
    Quote Originally Posted by C Standard, 1999 edition, section 6.7.5.2 paragraph 10
    All declarations of variably modified (VM) types have to be at either block scope or function prototype scope. Array objects declared with the static or extern storage-class specifier cannot have a variable length array (VLA) type. However, an object declared with the static storageclass specifier can have a VM type (that is, a pointer to a VLA type). Finally, all identifiers declared with a VM type have to be ordinary identifiers and cannot, therefore, be members of structures or unions.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 08:01 PM
  3. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 09:57 AM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-12-2004, 12:15 AM
  5. array of pointer objects
    By Death_Wraith in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 08:06 PM

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