Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

new and delete

  • 31-01-2002 2:50pm
    #1
    Closed Accounts Posts: 411 ✭✭


    I have a (perhaps odd) question.

    I have allocated an array of chars using the new operator.

    so

    char *MyArray;
    MyArray = new char [10];

    Ordinarily when I'm finished using this array I'd call

    delete [] MyArray;


    BUT my question arises, here...
    A pointer to this allocated array is passed between a few threads, before it eventually needs to be deleted.

    The array pointer is copied to the other threads, and hence a new pointer now points to the same memory.

    At all times I have a pointer of some sorts pointing to this memory, so when I want to delete it do I still simply call

    delete [] NewArrayName;

    Or do I have to call
    delete [array_size] NewArrayName;
    ?

    If delete [] NewArrayName; does work, then how the hell does it know how much memory to delete/deallocate?

    Does new keep a count of how much memory was allocated with the origonal call to new?


Comments

  • Closed Accounts Posts: 77 ✭✭paddymee


    new does not keep any such count.

    You only have to do the delete once.

    Like you said the threads are only getting a pointer to the memory location. They are not getting newly allocated memory.

    You seem to understand what is going on but are giving C/C++ to much credit for memory management. This is the stuff that causes so many problems.

    All the pointers point to the same location in memory. When you do a delete, it does nothing to the pointer, it just frees the memory that it points to. So if you have 5 pointers to this memory and call delete on one of them, all five pointers point to random memory.

    See the problem with sharing pointers? If the other 4 aren't aware that they points to random memory, you are in trouble.

    Also you have entered a new world of horror when you are using threads and sharing global resources. You have to synchronise between the threads to make sure that one thread doesn't change the state of the pointer while another is using it.

    If I was do one of two things depending on the situation.
    1) If the threads only need to read from the buffer I would pass a reference to the buffer to each thread and have them make their own copies. The threads are then responsible for the life time of the memory
    2) If the threads need to read/write I would be very careful and use locks and/or critical sections. And then see if I could do it differently ;-)


  • Closed Accounts Posts: 411 ✭✭Jay


    Well actually I jusy found out how new and delete work.

    Basically new keeps a count by placing an integer value before the array in memory, that says how many bytes are allocated.

    So when you call
    delete [] NewArrayPointer;, it knows how many bytes to delete.

    paddymee: I fully understand how pointers work, and am well aware that the pointers concerned all point to the one location in memory. I also am fully aware that a call to delete on one of these pointers will deallocate the actual memory pointed to by all the pointers.

    I think you missed the point of the question. Perhaps I didn't make it clear. The question was...

    When I call delete [] ptrArray;, how does it know how much memory to deallocate?
    Problem solved.

    Thread synchonization isn't a problem either, I have that one covered.
    I cannot use reference passing since the amount of data being allocated may be large, and I need to avoid the over head of a copy.

    Thanks for your concerns.


  • Closed Accounts Posts: 5,564 ✭✭✭Typedef


    Maybe I'm not understanding you, but when you assign pointer x to y x==y thus deallocating x will deallocate y, I could be wrong?


  • Closed Accounts Posts: 77 ✭✭paddymee


    When I call delete [] ptrArray;, how does it know how much memory to deallocate?

    Oh right. Why didn't you say so ;-)

    Sorry I thought you meant that there was a reference count thingie going on.

    Good luck
    Maybe I'm not understanding you, but when you assign pointer x to y x==y thus deallocating x will deallocate y, I could be wrong?

    You are correct sir.


Advertisement