Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #13367

Re: How to free this memory?

From BCFD36 <bcfd36@cruzio.com>
Newsgroups comp.lang.c++
Subject Re: How to free this memory?
Date 2012-01-14 02:01 -0800
Organization Aioe.org NNTP Server
Message-ID <jerjqf$dl$1@speranza.aioe.org> (permalink)
References (5 earlier) <jeq058$1h17$1@adenine.netfront.net> <zaCdnbr0KNdYGI3SnZ2dnUVZ8tmdnZ2d@giganews.com> <jeq22i$1i5u$1@adenine.netfront.net> <Q5adnfNSlPy5CI3SnZ2dnUVZ8uudnZ2d@giganews.com> <jeq5kl$1m7l$1@adenine.netfront.net>

Show all headers | View raw


On 1/13/12 12:52 PM, Paul <pchrist wrote:
> "Leigh Johnston"<leigh@i42.co.uk>  wrote in message
> news:Q5adnfNSlPy5CI3SnZ2dnUVZ8uudnZ2d@giganews.com...
>> On 13/01/2012 19:52, Paul<pchrist wrote:
>>> "Leigh Johnston"<leigh@i42.co.uk>   wrote in message
>>> news:zaCdnbr0KNdYGI3SnZ2dnUVZ8tmdnZ2d@giganews.com...
>>>> On 13/01/2012 19:20, Paul<pchrist wrote:
>>>>> "Leigh Johnston"<leigh@i42.co.uk>    wrote in message
>>>>> news:bvWdnUQB5qwO7o3SnZ2dnUVZ8smdnZ2d@giganews.com...
>>>>>> On 13/01/2012 18:04, Bint wrote:
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>> On 13/01/2012 17:37, Paul<pchrist wrote:
>>>>>>>>> "Bint"<bint@ign.com>      wrote in message
>>>>>>>>> news:CB35C06F.B997%bint@ign.com...
>>>>>>>>>> Hello, I am allocating some vertices to pass to a function, and
>>>>>>>>>> then
>>>>>>>>>> I
>>>>>>>>>> want
>>>>>>>>>> to free the memory.  How do I do this?  The compiler is giving me
>>>>>>>>>> warnings
>>>>>>>>>> when I do it this way:
>>>>>>>>>>
>>>>>>>>>>             trivertex **vertices;
>>>>>>>>>>
>>>>>>>>>>             vertices = new trivertex*[2];
>>>>>>>>>>
>>>>>>>>>>             vertices[0] = new trivertex[100];
>>>>>>>>>>             vertices[1] = new trivertex[50];
>>>>>>>>>>
>>>>>>>>>> .
>>>>>>>>>> .
>>>>>>>>>> .
>>>>>>>>>>             delete vertices[0];
>>>>>>>>>>             delete vertices[1];
>>>>>>>>>>             delete vertices;
>>>>>>>>>>
>>>>>>>>>> I get this warning:
>>>>>>>>>> " 'delete' applied to a pointer-to-array type 'trivertex *' (aka
>>>>>>>>>> 'double
>>>>>>>>>> (*)[3]') treated as delete"
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> Thanks!
>>>>>>>>>> B
>>>>>>>>>
>>>>>>>>> Hi
>>>>>>>>>
>>>>>>>>> You should use delete[] , to delete memory allocated with new [].
>>>>>>>>> So...
>>>>>>>>> delete[] veritces[0];
>>>>>>>>> delete[] vertices[1];
>>>>>>>>> delete[] vertices;
>>>>>>>>
>>>>>>>> Yes you do delete[] what you new[] however the poster should use
>>>>>>>> std::vector as Alf suggests; it is very rare that you need to use
>>>>>>>> dynamically allocated arrays these days.
>>>>>>>>
>>>>>>>> One use-case for dynamically allocated arrays is when you want a
>>>>>>>> dynamically sized *uninitialized* buffer.
>>>>>>>>
>>>>>>>> /Leigh
>>>>>>>
>>>>>>> I don't have any experience with namespaces or std:vector.  I will
>>>>>>> try
>>>>>>> it
>>>>>>> and see if what Alf posted works in my code.
>>>>>>>
>>>>>>> But if delete[] works, then what is wrong with using it?
>>>>>>
>>>>>> One advantage of std::vector is that you don't have to worry about
>>>>>> calling
>>>>>> delete[] or memory leaks as std::vector takes care of this
>>>>>> automatically.
>>>>>> The standard containers offer many other advantages also such as
>>>>>> exception
>>>>>> safety and a rich interface for modifying the container's controlled
>>>>>> sequence (its elements).
>>>>>
>>>>> I think there is a large division , in this newsgroup, about what is
>>>>> exception safety. You are correct to say that exception safety is very
>>>>> important when it comes to a decision about dynamic allocation or
>>>>> allowing
>>>>> STL allocators to handle it.
>>>>> However I cannot correlate this to your view that all programs should
>>>>> simply
>>>>> terminate when an exception is thrown because with this view it doesn't
>>>>> make
>>>>> any difference. If a program terminates who cares what memory has been
>>>>> allocated/deallocated?
>>>>
>>>> Again with the misrepresentation.  I *never* said that *all* programs
>>>> should simply terminate; I said that termination is acceptable behaviour
>>>> for *some* programs; "some" does not equal "all".
>>>>
>>> Ok but I think you were on the side arguing that *most* programs should
>>> just
>>> terminate on bad alloc.
>>> Maybe I am mistaken, please clarify.
>>
>> It is impossible to quantify exactly but yes I am of the opinion that
>> *most* programs can't function (meet their requirements) if *any*
>> allocation they make fails in which case termination is acceptable.
>>
>> An example of a program that *should* perhaps catch bad_alloc would be a
>> program that can open/edit "documents" where opening or editing a document
>> can fail due to insufficient memory.
>>
>> A program which can have human safety implications should probably
>> allocate *all* the memory it needs to meet its requirements up front
>> nullifying any chance of a bad_alloc being thrown once the program has
>> successfully started.
>>
> Lets simplify it..... If you have a system where the amount of available
> memory is known.  Say a sattellite , then what are the possible causes of a
> bad_alloc?
> Is there still a case for error checking allocations? What other errors can
> occur during a memory allocation, other than a low memory condition?

Typically, you don't do run time allocates on satellites, at least the 
ones with which I am familiar. It doesn't go over well in code reviews. 
People will come by your cube and look at you, then scurry away and laugh.

>
> IMO its very similar to checking for a disc read/write error. By testing for
> bad alloc , your program is confirming the integrity of the memory acccess
> operation.
>
> Say a sattellite orbiting the earth hit a  moot point in the earths magnetic
> feild and becomes unstable due to particles from the sun, is it possible
> that a write to memory could fail?

Yes. It does happen. Sometimes it is a single bit. The code section of 
memory is checksummed on a regular basis.

> If so is it not the case that this
> allocation has failed due to circumstances other than a low memory
> condition?

On the nosey!




-- 
Dave Scruggs

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

How to free this memory? Bint <bint@ign.com> - 2012-01-13 11:05 -0600
  Re: How to free this memory? "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2012-01-13 18:37 +0100
    Re: How to free this memory? "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2012-01-13 18:07 +0000
      Re: How to free this memory? "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2012-01-13 19:38 +0100
        Re: How to free this memory? "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2012-01-13 20:35 +0000
          Re: How to free this memory? Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-01-14 02:16 -0800
          Re: How to free this memory? Kevin McCarty <kmccarty@gmail.com> - 2012-01-16 09:08 -0800
      Re: How to free this memory? Kevin McCarty <kmccarty@gmail.com> - 2012-01-13 10:48 -0800
        Re: How to free this memory? Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-01-14 02:19 -0800
          Re: How to free this memory? Kevin McCarty <kmccarty@gmail.com> - 2012-01-16 08:56 -0800
    Re: How to free this memory? Leigh Johnston <leigh@i42.co.uk> - 2012-01-13 18:16 +0000
  Re: How to free this memory? "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2012-01-13 17:37 +0000
    Re: How to free this memory? Leigh Johnston <leigh@i42.co.uk> - 2012-01-13 17:47 +0000
      Re: How to free this memory? Bint <bint@ign.com> - 2012-01-13 12:04 -0600
        Re: How to free this memory? Leigh Johnston <leigh@i42.co.uk> - 2012-01-13 18:10 +0000
          Re: How to free this memory? "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2012-01-13 19:20 +0000
            Re: How to free this memory? Leigh Johnston <leigh@i42.co.uk> - 2012-01-13 19:27 +0000
              Re: How to free this memory? "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2012-01-13 19:52 +0000
                Re: How to free this memory? Leigh Johnston <leigh@i42.co.uk> - 2012-01-13 20:33 +0000
                Re: How to free this memory? "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2012-01-13 20:52 +0000
                Re: How to free this memory? David Dyer-Bennet <dd-b@dd-b.net> - 2012-01-13 15:01 -0600
                Re: How to free this memory? BCFD36 <bcfd36@cruzio.com> - 2012-01-14 02:01 -0800
                Re: How to free this memory? "MikeWhy" <boat042-nospam@yahoo.com> - 2012-01-13 15:46 -0600
                Re: How to free this memory? Leigh Johnston <leigh@i42.co.uk> - 2012-01-13 21:59 +0000
                Re: How to free this memory? Goran <goran.pusic@gmail.com> - 2012-01-13 22:12 -0800
                Re: How to free this memory? Leigh Johnston <leigh@i42.co.uk> - 2012-01-14 11:58 +0000
            Re: How to free this memory? Goran <goran.pusic@gmail.com> - 2012-01-13 22:01 -0800
        Re: How to free this memory? Victor Bazarov <v.bazarov@comcast.invalid> - 2012-01-13 13:48 -0500
        Re: How to free this memory? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-01-14 20:46 +0000
    Re: How to free this memory? Bint <bint@ign.com> - 2012-01-13 11:54 -0600
  Re: How to free this memory? Travis Vitek <travis.vitek@gmail.com> - 2012-01-13 09:21 -0800
    Re: How to free this memory? jgk@panix.com (Joe keane) - 2012-01-14 01:03 +0000

csiph-web