Memory trick with the free function

I've found that it is possible to free a variable struct or an

array that I have and then just after doing this, use

the variable of a struct to get the data and then

malloc a new variable. By doing this, it allows me

to work with more memory than I already have; otherwise,

I would run into a problem with not having enough

memory to run the next malloc or even proceed to the

next function call.

Could there arise any major problems in my program

by doing this. I guess that I need to know exactly

what the free function does. It seems to only tell

the computer that there is a block of memory open

to use, but not clear that block or even clear the

variable's addresses that was just used in the free

function. Am I sort of on the right track?




Comments

  • What you are doing is highly dangerous in general because any other part of the operating system that allocates memory, including other threads or possibly other procesess could steal it out from under you between the time that you free it and the time that you access it.


    This is less likely in a non-preemptive environment, and very not likely in MSDOS. However, it is _logically_ incorrect to use memory after you've called 'free' because what you've said is that you no longer want that memory and the operating system can do with it what it wants. It could zero out memory if it wanted. It probably won't, but it could.


    A bigger problem is if you're on a protected-memory system, and by freeing the block, that chunk of memory may get taken away from your process's right to use it. Accessing that memory could throw an exception and your process gets punted. Again, this is not going to happen in MSDOS unless you're starting to work with the memory management unit on most PC boxes.


    So, the short is: You could get away with it, but it is a bad idea. It won't always work as you move from platform to platform, it is logically incorrect, and it will be more difficult to debug if something goes wrong. It's the kind of thing you do as a last resort when you absolutely positively have to.





  • I thought that may happen, but I wasn't sure. Thanks!

    I think that I found a way around the problem by taking out some large global structs and either

    making a global pointer or puting the struct into the function that I use it in.

    I guess that I can avoid using the free function in that manner by thinking more about how I engineer my code.

    Thanks again!




    : What you are doing is highly dangerous in general because any other part of the operating system that allocates memory, including other threads or possibly other procesess could steal it out from under you between the time that you free it and the time that you access it.


    : This is less likely in a non-preemptive environment, and very not likely in MSDOS. However, it is _logically_ incorrect to use memory after you've called 'free' because what you've said is that you no longer want that memory and the operating system can do with it what it wants. It could zero out memory if it wanted. It probably won't, but it could.


    : A bigger problem is if you're on a protected-memory system, and by freeing the block, that chunk of memory may get taken away from your process's right to use it. Accessing that memory could throw an exception and your process gets punted. Again, this is not going to happen in MSDOS unless you're starting to work with the memory management unit on most PC boxes.


    : So, the short is: You could get away with it, but it is a bad idea. It won't always work as you move from platform to platform, it is logically incorrect, and it will be more difficult to debug if something goes wrong. It's the kind of thing you do as a last resort when you absolutely positively have to.





Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories