(Console C++)Resizing mallocation?
-
Hi. This is not a Visual C++ question. Also, it'd be great if it worked under ANSI C. In this function I'm writing, strings start to come up and need to be saved on a big char* one next to the other, say: "yellow", "one", "banana" -> "yellowonebanana" thing is that final length is unknown, it could be 1000b as well as it could be 0b. so I looked it up from Google and only solution found was to do the algorythm twice, where in the first one I calculate length, then malloc, then do again and start storing. Is there any way to allocate and resize allocation dynamically? Thx in advance. Mariano Lopez-Gappa.
-
Hi. This is not a Visual C++ question. Also, it'd be great if it worked under ANSI C. In this function I'm writing, strings start to come up and need to be saved on a big char* one next to the other, say: "yellow", "one", "banana" -> "yellowonebanana" thing is that final length is unknown, it could be 1000b as well as it could be 0b. so I looked it up from Google and only solution found was to do the algorythm twice, where in the first one I calculate length, then malloc, then do again and start storing. Is there any way to allocate and resize allocation dynamically? Thx in advance. Mariano Lopez-Gappa.
-
Hi. This is not a Visual C++ question. Also, it'd be great if it worked under ANSI C. In this function I'm writing, strings start to come up and need to be saved on a big char* one next to the other, say: "yellow", "one", "banana" -> "yellowonebanana" thing is that final length is unknown, it could be 1000b as well as it could be 0b. so I looked it up from Google and only solution found was to do the algorythm twice, where in the first one I calculate length, then malloc, then do again and start storing. Is there any way to allocate and resize allocation dynamically? Thx in advance. Mariano Lopez-Gappa.
-
Hi. This is not a Visual C++ question. Also, it'd be great if it worked under ANSI C. In this function I'm writing, strings start to come up and need to be saved on a big char* one next to the other, say: "yellow", "one", "banana" -> "yellowonebanana" thing is that final length is unknown, it could be 1000b as well as it could be 0b. so I looked it up from Google and only solution found was to do the algorythm twice, where in the first one I calculate length, then malloc, then do again and start storing. Is there any way to allocate and resize allocation dynamically? Thx in advance. Mariano Lopez-Gappa.
You could create a file on the local drive and store as much as the drive would hold. There would be no need for any memory allocation or re-allocation. If you don't need speed, its an option.
-
You could create a file on the local drive and store as much as the drive would hold. There would be no need for any memory allocation or re-allocation. If you don't need speed, its an option.
Many thanks to all 3 answers. I'm gonna go for the realloc as it looks like it fits best my needs. Speed is not really an issue but I can't use disk. Thanks again. Case closed. :D
-
Hi. This is not a Visual C++ question. Also, it'd be great if it worked under ANSI C. In this function I'm writing, strings start to come up and need to be saved on a big char* one next to the other, say: "yellow", "one", "banana" -> "yellowonebanana" thing is that final length is unknown, it could be 1000b as well as it could be 0b. so I looked it up from Google and only solution found was to do the algorythm twice, where in the first one I calculate length, then malloc, then do again and start storing. Is there any way to allocate and resize allocation dynamically? Thx in advance. Mariano Lopez-Gappa.
Can't you do this: 1. Read in the string to a temporary max-length memory location. 2. Allocate the strings' memory using the strlen() function. 3. Use strcpy() to move the string into it's position 4. Repeat steps 1-3 until you are done 5. Deallocate the temporary string. That's the easiest way I can I see doing it straight-forward and using only ANSI-C commands.
-
Hi. This is not a Visual C++ question. Also, it'd be great if it worked under ANSI C. In this function I'm writing, strings start to come up and need to be saved on a big char* one next to the other, say: "yellow", "one", "banana" -> "yellowonebanana" thing is that final length is unknown, it could be 1000b as well as it could be 0b. so I looked it up from Google and only solution found was to do the algorythm twice, where in the first one I calculate length, then malloc, then do again and start storing. Is there any way to allocate and resize allocation dynamically? Thx in advance. Mariano Lopez-Gappa.
Mariano Lopez-Gappa wrote:
thing is that final length is unknown, it could be 1000b as well as it could be 0b.
Is that the only difference? Memory is cheap these days. Unless you are talking about several MB of allocations, just allocate 1KB for each instance and be done with it. Yes, there might be some waste here and there, but that's a whole lot more efficient than reallocating memory.
"Take only what you need and leave the land as you found it." - Native American Proverb
-
Mariano Lopez-Gappa wrote:
thing is that final length is unknown, it could be 1000b as well as it could be 0b.
Is that the only difference? Memory is cheap these days. Unless you are talking about several MB of allocations, just allocate 1KB for each instance and be done with it. Yes, there might be some waste here and there, but that's a whole lot more efficient than reallocating memory.
"Take only what you need and leave the land as you found it." - Native American Proverb
LOL. I misread. If he is only talking about 1000b > a > 0b, then I agree with you. Start with a large 1000b buffer. If you run out of room, double the size and try again. When done, return a new allocated buffer of the final size (not a required step if memory isn't an issue). Tim Smith I'm going to patent thought. I have yet to see any prior art.