Hi,
sorry if this has been asked a few times, it's about automatic allocation of an array as in:
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!