标签: delete-operator

如何从C++中的结构数组中删除项?

我有以下数组结构(链表):

    struct str_pair   
     {  
       char ip  [50] ;  
       char uri [50] ;  
       str_pair *next ;  
     } ;

str_pair *item;
Run Code Online (Sandbox Code Playgroud)

我知道要创建一个新项目,我需要使用

item = new str_pair;
Run Code Online (Sandbox Code Playgroud)

但是,我需要能够遍历数组并删除特定项.我将循环部分排序.但是如何从结构数组中删除项目?

c++ linked-list delete-operator data-structures

0
推荐指数
1
解决办法
5960
查看次数

如何删除我创建的这个对象?

以此程序为例:

class Piece
{
public:
    Piece(bool color);

protected:
    bool color;
};

Piece::Piece(bool color)
{
    this->color = color;
}

//-----------------------------

class King : public Piece
{
public:
    King(bool color);
};

King::King(bool color) : Piece(color)
{
    // empty
}

//-----------------------------

class Tile
{
public:
    // constructors/destructors
    Tile(Piece * ppiece, int rrow, int ccol);
    ~Tile();

private:
    Piece * piece;
    int row, col;

};

Tile::Tile(Piece * ppiece, int rrow, int ccol)
{
    this->ppiece = piece;
    this->row = rrow;
    this->col = ccol;
}

//---------------------------

int …
Run Code Online (Sandbox Code Playgroud)

c++ new-operator delete-operator

0
推荐指数
1
解决办法
135
查看次数

C++ delete []崩溃

我正在编写一个C++程序,它将字符串存储在字符串数组中,当数组已满时,我会调整数组大小,以便使用下面的代码为更多项目腾出空间.但有时(并非总是)它会在"delete [] temp;"崩溃 线,我不知道为什么以及如何解决它.请帮忙.

我搜索了很多,但无法在任何地方找到答案.当我调试它时说"无效指针",但是当我之前存储数据并且没有释放它时它怎么会无效?

这是我的代码:

if(item_cnt >= (arr_size - 1))
{
    int oldsize = arr_size;
    string * temp;
    arr_size *= 2;
    temp = arr;
    arr = new string [arr_size];
    memcpy(arr, temp, oldsize * sizeof(temp));
    delete[] temp;
}
Run Code Online (Sandbox Code Playgroud)

c++ arrays string delete-operator

0
推荐指数
1
解决办法
1445
查看次数

从多线程程序安全地删除对象

免责声明:Boost和C++ 11都不允许.

我有一个程序,我在其中创建了一个实例,Foo并在多个线程中使用它.然后我想要安全地删除它,以便这些线程不会陷入分段错误.

Foo每次线程函数运行时,我都会添加一个互斥锁成员并将其锁定.为了使不同的线程不相互冲突.

class Foo
{
    public:
        pthread_mutex_t mutex;
};

void* thread ( void* fooPtr )
{
    Foo* fooPointer = (Foo*) fooPtr;

    while ( 1 )
    {
        if ( fooPointer )
        {
            pthread_mutex_lock ( &fooPointer->mutex );
            /* some code, involving fooPointer */
            pthread_mutex_unlock ( &fooPointer->mutex );
        }
        else
        {
            pthread_exit ( NULL );
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我想要foo安全删除,因此线程中不会发生错误.我添加了一个析构函数Foo:

Foo::~Foo()
{
    pthread_mutex_lock ( &mutex );
}
Run Code Online (Sandbox Code Playgroud)

现在,在所有线程完成当前循环之前,不会删除该对象.

问题是:删除实例后是否会解锁互斥锁?删除实例后,所有线程都会完成吗?我打赌答案是no.所以我改变了析构函数,但现在看起来线程不安全:

Foo::~Foo()
{
    pthread_mutex_lock ( …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading mutex thread-safety delete-operator

0
推荐指数
1
解决办法
4884
查看次数

删除包含动态指针的STL :: list <class something>的类

我有一个问题,我无法在任何地方找到答案.但是,我必须展示一些代码:

  #include "Vector2D"

    class something
    {
        Vector2D * p_Position;
      public:
        something(){p_Position = new Vector2D;}
        ~something(){delete p_Position;}
    };

    int main()
{
    std::list<something> Somethinglist;

    Somethinglist.push_back(something());

    Somethinglist.clear();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

因此,当涉及到.clear()函数时,这将导致断言失败.所以我试了几件事.首先,如果我不把它delete p_Position放在解构器中,这个代码就完全可以了.这是为什么?STL列表.clear()功能是否会自动销毁动态指针?或者一个相当直接的问题:我该如何修复此代码?

c++ stl list delete-operator

0
推荐指数
1
解决办法
111
查看次数

我应该如何在这个场合使用删除c ++

所以这是我的代码:

#include "stdafx.h"
#include <iostream>

using namespace std;

#define MAX 5
#define STR_LENGTH 40

void main()
{
    char *p_str[MAX];

    for (int i = 0; i < MAX; i++)
    {
        *(p_str+i) = new char(STR_LENGTH);
        cout << "Please enter a string:  ";
        cin >> *(p_str+i);
    }

    for (int i = 0; i < MAX; i++)
    {
        cout << *(p_str+i) << endl;
        delete (p_str+i);
    }

}
Run Code Online (Sandbox Code Playgroud)

在那里的最后一行,我有删除,但它到达那里时,它有什么想法解决它吗?

c++ delete-operator

0
推荐指数
1
解决办法
79
查看次数

delete []如何知道要删除多少内存?

int* i = new int[4];
delete[] i;
Run Code Online (Sandbox Code Playgroud)
  1. 当我们调用delete []时,程序如何知道"i"是4字节长度.4是存储在内存中的某个地方吗?
  2. delete []的实现取决于系统还是编译器?
  3. 是否有一些系统API来获取i的长度?

正如HadeS所说,它将保存已分配多少内存的信息?在哪里?它必须保存在内存中,或者可能在指针附近i.

c++ delete-operator

0
推荐指数
1
解决办法
769
查看次数

检测到新的/删除堆损坏:CRT检测到应用程序在堆缓冲区结束后写入内存

在下面的代码片段中,我试图取消分配用于使用New创建Set的动态内存,但是在擦除SET的节点后,如果我尝试删除.

#include <iostream>
#include <set>
#include <string>

using namespace std;

int main()
{
    std::set<char *> myset;
    std::set<char *>::iterator it;  
    char *l_ptr1, *l_ptr2;

    std::cout <<"Before Insertion::Address of myset " << &myset<<endl;
    //l_ptr1 = (char*)malloc(256*sizeof(char*));    
    //l_ptr2 = (char*)malloc(256*sizeof(char*));
    l_ptr1 = new char(256);
    l_ptr2 = new char(256);
    printf("Before Insertion ::Address of l_ptr1 %x\n", l_ptr1);
    printf("Before Insertion ::Address of l_ptr2 %x\n", l_ptr2);

    std::cin>>l_ptr1;
    myset.insert(l_ptr1);
    std::cin>>l_ptr2;
    myset.insert(l_ptr2);
    std::cout <<"After Insertion::Address of myset " << &myset<<endl;

    std::cout << "Myset Contains :: Value and It's Address "<<endl; …
Run Code Online (Sandbox Code Playgroud)

c++ memory delete-operator

0
推荐指数
1
解决办法
3869
查看次数

删除Array Java中的元素

我有一个像这样的数组(大小= 5)

int[] arr = {1,2,3,4,5};

我怎么能删除4而不是最终(size = 4)这样

int[] arr = {1,2,3,5}

java arrays element delete-operator

0
推荐指数
1
解决办法
4106
查看次数

为什么编译器在删除动态分配的内存后不会自动为指针变量赋值?

我有一小段代码:

#include <iostream>
using namespace std;

int main() 
{
    int *p = new int(10);

    if(p != NULL)
    {
        cout<<"Deleted dynamic allocated memory"<<endl;
        delete p;
    }

    if(p == NULL)
    {
        cout<<"NULL"<<endl;
    }
    else
    {
        cout<<"Not NULL"<<endl;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

使用deleteoperator 删除动态分配的内存后,为什么编译器不会自动为指针(如p = NULL)分配NULL?

c++ null new-operator delete-operator

0
推荐指数
1
解决办法
312
查看次数