![]() ![]() It is just a simple check, but it is something to get in the habit of doing every time or risk attempted reads and writes from/to unallocated memory. for malloc, calloc, realloc, and for other function calls that allocate for you like strdup). When allocating memory, you must validate that each call succeeded (e.g. ![]() While generally not a problem when you sequentially fill/read from an array, when randomized storage and retrieval routines are used, this can be a real problem. By initializing all values to 0 you prevent any possibility of an accidental attempted read from an uninitialized value (undefined behavior). The same applies to allocating for numeric arrays as well. The storage for all your strings is then guaranteed to be nul-terminated by virtue of the initialization. If you use calloc instead of malloc, you initialize all values to 0 ( nul). The same holds true when allocating storage for your lines as well. Jack white on my supercan free#The example below uses the traditional approach of iterating over a fixed number of lines to print the lines, and then free the memory allocated, but there is no reason you couldn't simply iterate over the valid pointers in both cases to accomplish the same thing. ( note: there is no reason not to pass the size of the collection, but there are circumstances where this is a benefit) This also provides the ability to pass a pointer to your collection to any function without also passing the number of lines/values contained. (the first NULL simply serves as your sentinel value telling you when to stop). So the loop will only interate over the pointers that have a value, stopping when the first NULL pointer is encountered. Only pointers you have assigned something to will have a value. To iterate over the collection you can simply do: size_t i = 0 How does that work? Example, suppose you initialized 100 pointers to NULL and have assigned a number of lines to each pointer as you go. Why? It isn't mandatory, but doing so allows you to iterate over your array of pointers without knowing the number of lines. On reallocation, it requires that you set all new pointers allocated to NULL. This is easily accomplished for the original allocation. ![]() When allocating your pointers originally, and as part of your reallocation, you can benefit by setting each pointer to NULL. ![]() The realloc to twice current is just one of the standard schemes. You can simply add a fixed amount each time, or you can use some scaled multiple of the original - it's up to you. Note, you are free to add any incremental amount you choose. Here, you simply keep a counter of the number of lines read, and when you reach your original allocation limit, you simmply reallocate twice the number of pointers you currenly have. This is much more efficient than calling realloc and reallocating the entire collection for every line you read. The general, and efficient, approach to allocating memory for an unknown number of lines is to first allocate a reasonably anticipated number of pointers (1 for each anticipated line). char **array, which is technically a pointer-to-pointer-to-char or pointer-to-char* if you like) The short-hand for pointer-to-pointer-to-type is simply double-pointer (e.g. There are a number of different types of data structures you can employ, lists, trees, etc., but the basic way (as you call it "2D char array") is handled by creating an array of pointer-to-pointer-to-type (with type being char in this case) and then allocating space for, filling with data, and assigning the starting address for the new block of memory to each pointer as your data is read. It is worth understanding this process in detail as it will serve as the basis for just about any any other circumstance where you are reading an unknown number of values (whether they are structs, floats, characters, etc.). This is the classic question of how do I handle dynamic allocation and reallocation to store an unknown number of strings. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |