Doesnt work, ima give up. Thanks for all of your help
Printable View
Doesnt work, ima give up. Thanks for all of your help
It's completely unnecessary since you allocate the required memory before.
Do you need it? Otherwise, remove it.
Well I dont understand how to use malloc in this particular way. I dont need it for my assignment i just wanted to try it. Im more getting comments saying what are you doing this why would you do that.....than actual help. If you tell me I have a buffer overrun and a memory leak, I search to try and find out what a buffer overrun means, sort of understand it, but still dont know how to fix it in this particular case.
You should check to see if realloc fails.
Ok, well first off many people here escribe to the logic of "why should I write sample code?" Which is never my attitude. Secondly, a lot of times it helps to know why since sometimes we may know a better how. Don't let any of us discourage you through critiquing your work. Your program seems to be off to a fine start. Just be aware of what it is doing.
You are allocating a buffer with malloc(). Then within a loop reallocating the buffer with realloc(). Which is actually perfectly acceptable code. However, it may not function as fast as if you were to only realloc() when you know you absolutely have to. I think a lot of people get it etched into their brain that realloc() is slow somewhere along their learning C process to such an extent that they lose site of the fact that using it in a loop is not actually wrong, nor will it increase the probability of a NULL return. Its just realloc() is a slow function is all.
Thanks for your encouragement. I am checking if realloc fails. Still get error while dumping...
While all that's true, I brought the point up because (a) you should check whether malloc and realloc fail, 'cause otherwise bad things happen and (b) on this Win machine with this program, realloc always fails the fifth time. Always. I don't know why, because I surely have more than twenty bytes of memory, but there it is.
You're correct Elysia I got the order mixed up. Storage should be allocated before assigning the integer read from input. Moreover malloc() does not specify the order and contiguity of storage allocated by successive calls to it so printing iarray by incrementing it in units of 1 won't display the stored numbers as they may or may not be in successive memory locations.
Any other ideas?
A bst... I dunno. I see more problems with how you are going about the task rather than the mechanical process of your methods. Linked lists would work fine too.