удалите [], предоставил измененный указатель нового редактора. Неопределенное Поведение?

Просто сделай это.

<script>
 $(".deleteProduct").click(function(){
    var btn = $(this);
    var id = $(this).data("id");
    var token = $(this).data("token");
    $.ajax(
        {
            url: "comment/delete/"+id,
            type: 'DELETE',
            dataType: "JSON",
            data: {
                "id": id,
                "_method": 'DELETE',
                "_token": token,
            },
            success: function ()
            {
                btn.closest("tr").remove(); // closest tr removed
                console.log("it Work");
            }
        });

    console.log("It failed");
});

5
задан Sam 10 August 2014 в 11:44
поделиться

3 ответа

From the C++ Standard, section 5.3.5/2:

the value of the operand of delete shall be the pointer value which resulted from a previous array new-expression. If not, the behaviour is undefined

17
ответ дан 18 December 2019 в 06:36
поделиться

Yes, you must delete[] the original pointer you were given by new; in this case, that would be a pointer to the head of the array, rather than the tail. The code here is deleting some other unspecified random object.

4
ответ дан 18 December 2019 в 06:36
поделиться

Yeah, recall how this is often implemented: new really calls malloc, which gives back a pointer to (void*)(&(((int*)p)[1])), where p is the actual start of the memory allocated, and the first int is the size of the actual memory we got back.

The pointer we get back is one sizeof(int) (or whatever alignment requires) further along in the actual memory allocated. We lay down our object there, leaving the actual size undisturbed.

Then when that pointer passed to delete, which passes it to free, free looks one int before the passed pointer, to find the size that's being given back.

Passing back something other than what we got is going to mean that free thinks an arbitrary amount of actual memory is being passed back, and it'll screw up the free list accordingly.

Again, this is how it's often implemented, not how new, delete, malloc, or free are required to be implemented.

3
ответ дан 18 December 2019 в 06:36
поделиться
Другие вопросы по тегам:

Похожие вопросы: