pros and cons, auto array allocation

This is a discussion on pros and cons, auto array allocation within the C Programming forums, part of the General Programming Boards category; Hi, sorry if this has been asked a few times, it's about automatic allocation of an array as in: Code: ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    82

    pros and cons, auto array allocation

    Hi,

    sorry if this has been asked a few times, it's about automatic allocation of an array as in:

    Code:
    char array[BUF];
    This is a runtime allocation. Pros and cons: It does not require freeing, it's also shorter to type, so it does invite a little laziness. Reading in the lines of a file? with BUF at 1024 or 2048, with luck on your side, will allow most files to be read (in general situations). The buffer size, here BUF - as it must be - is a macro, i.e. must be a literal, not a variable.

    Clearly the more elegant way is static allocation.
    Code:
    char *array=malloc(BUF*sizeof(char));
    . Pros and cons: Needs freeing after usage. Longer to type. BUF can be a variable (to exploit that you probably will use realloc() later also). Clearly adds more complication to the code.

    Just wondering if there are any more pros and cons for these two out there. My expectation is that for small arrays, runtime allocation is fine. If BUF gets too big, that implies carelessness about memory and also a little sloppiness i.e. hoping for the best in terms of max line size in above example.

    (I ask myself if this isn't really a style issue, in which case I might have offended somebody ... but oh well, caution to the wind!)

    Any comments, best practices? Thanks in adv!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    Quote Originally Posted by stabu
    It does not require freeing, it's also shorter to type, so it does invite a little laziness.
    The "shorter to type" thing is only a very minor consideration. The "does not require freeing" thing is good. Being lazy is good, when you can afford it.

    Quote Originally Posted by stabu
    The buffer size, here BUF - as it must be - is a macro, i.e. must be a literal, not a variable.
    With respect to the 1999 edition of the C standard, BUF can be a variable, but then you get a variable length array instead.

    Quote Originally Posted by stabu
    Clearly the more elegant way is static allocation.
    I think you mean dynamic allocation. As for being elegant: as you later point out, it "clearly adds more complication to the code", which implies that it is not elegant.

    Quote Originally Posted by stabu
    My expectation is that for small arrays, runtime allocation is fine.
    They both involve runtime allocations, but anyway, you are right to say that when the array is small it is fine. When the array is too large, it may not be able to fit on the stack, and you would have to use a dynamic array anyway (or declare the array static, if feasible).
    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

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    82
    thanks for the answer laserlight, some pointers there too to things I need to investigate ... I'm lazy about looking to into the standards! :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 08-18-2008, 05:31 PM
  2. Pros and Cons of blocking versus non-blocking sockets?
    By abachler in forum Networking/Device Communication
    Replies: 4
    Last Post: 05-08-2008, 06:52 AM
  3. Computers. Pros. Cons.
    By Sentaku senshi in forum Tech Board
    Replies: 13
    Last Post: 08-21-2002, 09:43 AM

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