default array size for variable in gcc compiler
hi,
i am running a program where i have to read the text in a flat file into runtime variable. i did something like: Code:
char chr[], FileText[]; my problem is that there may be (at least theoratically) no defined size for this array which is going to hold complete file. is there some better way to do it or should i just go on and give some extra large number like "char FileText[200000]" or something. i personally don't think it could be a good idea as that might cause some memory issue even if the file is small as large quantity of memory have already been allocated to it. please help me as how it is done in other professional programs. Thanks --mlaich |
The builtin arrays must have their size determined at run time. So you will will either need to dynamically allocate the memory or fix the size in your program.
Questions to ask...
If you don't need to read in the entire file before processing then you can work with much less memory requirements. However if you need the whole file to be read in then I'd suggest that you allocate the memory using something like malloc (since your using c) and then play around with it. graeme. |
hi,
i have to read the whole file but since it have to be atleast read only, i can read the size of the file. i found something similar at "http://www-h.eng.cam.ac.uk/help/tpl/languages/C/teaching_C/node21.html" about the malloc stuff. i think i have to start like that only... thanks --mlaich |
Bottom line: when you do this:
Code:
char chr[] Anyway, the first thing is to understand the difference in C/C++ between "declaration" and "definition": http://publications.gbdirect.co.uk/c...claration.html http://www.lysator.liu.se/c/c-faq/c-10.html http://www.sun.com/971124/cover-linden/cchap.html The second issue is "What should I do"? If you're coding in C, you basically have two alternatives: 1. Declare a static array: Code:
#define MAX_ELEMENTS 20000 Code:
char *myarray; Code:
#include <vector> http://www.yolinux.com/TUTORIALS/Lin...alC++STL.html; PS: You can always "stat()" the file, get the file size, and make your array (at least) that big. |
thanks...
as my program (in C) is more along the lines of string handling and processing, i think i will stuck with the malloc stuff. one small doubt... as we are allocating the size of the array at run time, suppose the size is change (increasing from 300 to 400) while it already contains the data of length 298, would it have any problem on the data (of 298 length) already present in the memory. thanks for the links anyway... mlaich |
I'm curious about the idiomatic use of the noun "doubt" (as opposed, say, to "I have a question", or simply *asking* the question). Are you originally from Montana?
ANYWAY: 1. Yes, trying to read 400 bytes into a 300 element character array would be Bad (if that's what you're asking). That's precisely the reason that "#define MAX_ELEMENTS ..." is important if you have a static array: Code:
#define MAX_ELEMENTS 300 |
If possible, I would use C++ because it's much easier to write reliable code involving variable-sized arrays and variable-sized strings when you have its features to work with.
If you are constrained to C, at least make the best of it by defining subroutines for manipulating this data-structure, which is concealed from the view of the rest of the program by placing it inside of that unit... not as "extern." Pursuing this "poor man's object-oriented" line of thinking, consider that your "array of strings" does not have to be physically "an array" at all. All that it has to be is a collection of string-values accessed by means of an integer representing its ordinal position within the collection. There are many, many ways that you could represent such a thing. It is absolutely harmless to choose to use a set of subroutine/function calls to provide access to that collection, rather than stuffing the entire program with references to a C "array." What you need is...
Quote:
|
If possible, I would use C++ because it's much easier to write reliable code involving variable-sized arrays and variable-sized strings when you have its features to work with.
If you are constrained to C, at least make the best of it by defining subroutines for manipulating this data-structure, which is concealed from the view of the rest of the program by placing it inside of that unit... not as "extern." Pursuing this "poor man's object-oriented" line of thinking, consider that your "array of strings" does not have to be physically "an array" at all. All that it has to be is a collection of string-values accessed by means of an integer representing its ordinal position within the collection. There are many, many ways that you could represent such a thing. It is absolutely harmless to choose to use a set of subroutine/function calls to provide access to that collection, rather than stuffing the entire program with references to a C "array." What you need is...
Quote:
|
If possible, I would use C++ because it's much easier to write reliable code involving variable-sized arrays and variable-sized strings when you have its features to work with.
If you are constrained to C, at least make the best of it by defining subroutines for manipulating this data-structure, which is concealed from the view of the rest of the program by placing it inside of that unit... not as "extern." Pursuing this "poor man's object-oriented" line of thinking, consider that your "array of strings" does not have to be physically "an array" at all. All that it has to be is a collection of string-values accessed by means of an integer representing its ordinal position within the collection. There are many, many ways that you could represent such a thing. It is absolutely harmless to choose to use a set of subroutine/function calls to provide access to that collection, rather than stuffing the entire program with references to a C "array." What you need is...
Quote:
|
If possible, I would use C++ because it's much easier to write reliable code involving variable-sized arrays and variable-sized strings when you have its features to work with.
If you are constrained to C, at least make the best of it by defining subroutines for manipulating this data-structure, which is concealed from the view of the rest of the program by placing it inside of that unit... not as "extern." Pursuing this "poor man's object-oriented" line of thinking, consider that your "array of strings" does not have to be physically "an array" at all. All that it has to be is a collection of string-values accessed by means of an integer representing its ordinal position within the collection. There are many, many ways that you could represent such a thing. It is absolutely harmless to choose to use a set of subroutine/function calls to provide access to that collection, rather than stuffing the entire program with references to a C "array." What you need is...
Quote:
|
If possible, I would use C++ because it's much easier to write reliable code involving variable-sized arrays and variable-sized strings when you have its features to work with.
If you are constrained to C, at least make the best of it by defining subroutines for manipulating this data-structure, which is concealed from the view of the rest of the program by placing it inside of that unit... not as "extern." Pursuing this "poor man's object-oriented" line of thinking, consider that your "array of strings" does not have to be physically "an array" at all. All that it has to be is a collection of string-values accessed by means of an integer representing its ordinal position within the collection. There are many, many ways that you could represent such a thing. It is absolutely harmless to choose to use a set of subroutine/function calls to provide access to that collection, rather than stuffing the entire program with references to a C "array." What you need is...
Quote:
|
Quote:
Quote:
|
hi,
thanx for comments, i think i really don't need necessarily read the major big files completely. as far the small files are concerned, i think some thing like Code:
struct stat buffer; actually, my major constraint is not C or C++ (I really have 0 experience in C++, for C it IS nearby 0, but something bigger :)), but that i have to use flat file and not database. thanks --mlaich |
Don't forget to check and make sure that "status" is zero (success)! ;-)
|
All times are GMT -5. The time now is 10:03 AM. |