标签: free

设置条件以避免双重释放已分配的内存

Ubuntu 10.10 gcc 4.4.4

我只是在尝试分配和释放.

但是,当一个对象被多次释放时,我试图避免这个问题.

但是,当我测试时,我注意到创建和释放的obj没有返回到null状态.那么有什么条件我可以设置,如果这确实发生将避免?

我也试过在free之后将对象设置为NULL.但是,它仍然试图释放该对象.

这是对这个问题的引用,只是让你知道不重复: 释放已分配的内存

我的代码如下:

#include <stdio.h>
#include "objects.h"

int main(void)
{
    obj_t *obj = NULL;

    obj = create_object();

    destroy_object(obj);

    destroy_object(obj);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

==

#ifndef OBJECTS_H_INCLUDED
#define OBJECTS_H_INCLUDED

typedef struct Obj_t obj_t;

obj_t* create_object();
void destroy_object(obj_t *obj);

#endif /* OBJECTS_H_INCLUDED */
Run Code Online (Sandbox Code Playgroud)

==

#include <stdio.h>
#include <stdlib.h>

#include "objects.h"

struct Obj_t {
    int obj_id;
};

obj_t* create_object()
{
    obj_t *obj = malloc(sizeof obj);

    return obj;
}

void destroy_object(obj_t *obj)
{
    if(obj != NULL) { …
Run Code Online (Sandbox Code Playgroud)

c malloc free

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

这段代码泄漏了内存吗?我需要解放任何东西吗?C++内存

所以我在C++中编写了一些矩阵类.所以每个矩阵都有一个指向一个名为entries的数组的指针,我不确定我是否正确这样做但是我在子类中重新声明了数组.(我不是C++专家)

这个记忆是否需要免费?我只是用引用的数组覆盖指针吗?任何帮助将不胜感激.谢谢

class Matrix {
protected:
    float* entries;
public:
    int rows;
    int cols;


    Matrix() {
    }
    ~Matrix() {
    }
};

class Matrix4x4 : public Matrix {
    protected:
        float entry[4][4];
    public:

    /* This will create an empty matrix */
    Matrix4x4() {
        //Define the size of the arrays
        rows = 4;
        cols = 4;
        this->empty();
    }
    ...
};
Run Code Online (Sandbox Code Playgroud)

c++ memory free memory-leaks

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

C++ malloc/memcpy/free崩溃

我真的遇到了这个C++错误:

    template<typename T>
    void Shift(T* Data, const ulong& Length, long Offset) const
    {
        if((!Data) || (!Length))
            return;

        if(Offset < 0)
            Offset = (Length-1) - ((-Offset-1) % Length);
        else
            Offset %= Length;

        if(!Offset)
            return;

        int TSize = sizeof(T);

        T* Shifter = new T[Length];

        if(Shifter)
        {
            memcpy(Shifter, Data + TSize * Offset, TSize * (Length - Offset));
            memcpy(Shifter + TSize * (Length - Offset), Data, TSize * Offset); //fails
            memcpy(Data, Shifter, TSize * Length);

            delete[] Shifter;
        }
    }
Run Code Online (Sandbox Code Playgroud)

嗯,失败的是:

77CD0575 ntdll!TpWaitForAlpcCompletion()(C:\ Windows\system32 …

c++ malloc free templates memcpy

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

为什么malloc没有被释放?

#include <stdio.h>
#include <stdlib.h>
typedef struct node{
    struct node *pre;
    struct node *next;
    int data;
}NODE; //struct declaration

int main(){

    NODE *new_node=(NODE*)malloc(sizeof(NODE)); //memory allocation

    printf("\nnew_node addr: %d\n",new_node);

    free(new_node); //deallocation

    printf("new_node addr: %d\n",new_node);


}
Run Code Online (Sandbox Code Playgroud)

结果:

new_node addr: 2097152
new_node addr: 2097152
Program ended with exit code: 0
Run Code Online (Sandbox Code Playgroud)

结果为何相同?
我释放new_node的内存.但是new_node有地址.
为什么??

c malloc free

0
推荐指数
2
解决办法
48
查看次数

奇怪的free()无效指针C.

我修改了很多次这段代码的问题(但总是出现错误):似乎在释放"过滤器"的最后一个索引时出错

char** read_and_filter(int fd) {
  char buf[MAXLENGTH];
  char **bufs=NULL;
  char ch;
  int j = 0, len = 0, t = 0;
  while (!t && read(fd,&ch,1) == 1) {
    switch (ch) {
    case '\n':
      t = 1;
    case ' ':
      bufs = realloc(bufs, (j+1)*sizeof(char*));
      bufs[j++] = strndup(buf,len);
      memset(buf,0,len);
      len = 0;
      break;
    default:
      buf[len++] = ch;
    }
  }
  bufs[j] = 0;
  return bufs;
}

int main(int argc, char **argv) {
  char **filter;
  int i,fd = open("input.txt",O_RDONLY);
  filter = read_and_filter(fd);
  for(i = …
Run Code Online (Sandbox Code Playgroud)

c malloc free pointers realloc

0
推荐指数
2
解决办法
819
查看次数

当我使用free()释放malloced指针时,错误指针无效

在我的函数中使用它后,我无法释放此指针.它给了我这个错误信息.该函数应检查trie字典,以确定单词拼写是对还是错.而root是第一个trie节点.

`./peller'中的错误:free():无效指针:0x00007fe53a80d848

这是功能:

bool check(const char *word)
{
    int pos=0;
    int path;
    char wordChar=*(word+pos);
    wordTriePtr cursor=(wordTriePtr) malloc(sizeof(wordTrie));
    cursor=root;
    while(wordChar!='\0')
    {
        path=determinePath(wordChar);
        if(cursor->children[path]==NULL)
        {
            free(cursor);
            return false;
        }
        else
            cursor = cursor->children[path];

        wordChar=*(word+(++pos));
    }
    if(cursor->isThisWord==true)
    {
        free(cursor);
        return true;
    }
    else
    {
        free(cursor);
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

c malloc free pointers

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

Valgrind说"绝对泄漏"但是它呢?

我正在尝试编写一个用于井字游戏的蒙特卡罗树搜索,但是在运行它时会出现真正的内存问题(例如我的计算机内存不足).

所以我决定调查情况valgrind.

下面是一个代码块,上面valgrind写着"绝对泄漏".

void player_init (Player **p, player_t symbol)
{
  *p = (Player *) malloc (sizeof (Player));
  (**p).symbol = symbol;
  (**p).score = 0;
}

void player_init_all (Player ***p)
{
  *p = (Player **) malloc (sizeof (Player *) * 2);
  for (int i = 0; i < 2; i++)
    {
      (*p)[i] = (Player *) malloc (sizeof (Player));
    }
  player_init (&(*p)[0], PLAYER1);
  player_init (&(*p)[1], PLAYER2);
}

void player_destroy (Player *p)
{
  free (p);
}
Run Code Online (Sandbox Code Playgroud)

在哪里Playerplayer_t …

c malloc free valgrind memory-leaks

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

声明gchar后需要g_free吗?

我是使用GTK +和C编写小型应用程序的初学者。我正在GtkTreeView使用以下显示功能设置一个过滤器,主要是从此处复制的。

static gboolean filter_func (GtkTreeModel *model, GtkTreeIter *row, gpointer data) {
  // if search string is empty return TRUE

  gchar *titleId, *region, *name;
  gtk_tree_model_get (model, row, 0, &titleId, 1, &region, 2, &name, -1);

  // get search string
  if (strstr (titleId, "search text here") != NULL) {
    return TRUE;
  }

  g_free (titleId);
  g_free (region);
  g_free (name);

  return FALSE;
}
Run Code Online (Sandbox Code Playgroud)

我假定到目前为止这free()需要有malloc()和阅读https://developer.gnome.org/glib/stable/glib-Memory-Allocation.html告诉我:

重要的是要与g_malloc()(以及诸如的包装器g_new())进行匹配g_free()

因此,如果是这样,那么为什么g_free()在这里被称为?之所以如此重要,是因为对于搜索中键入的每个字符,此代码将被调用数千次。

c malloc free gtk3

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

释放三重指针

运行之后(它编译好了),我得到"双重自由或腐败",但只有特别是如果我将n设置为奇数.n均匀没问题,我真的很困惑......

#include<stdio.h>
#include<stdlib.h>

typedef unsigned int uint;

int main(void)
{   
    int i, j;
    uint n = 3;

    uint*** a = (uint***) malloc( n* sizeof(uint**) );
    for(i=0; i<n; i++)
    {
        *(a + i) = (uint**) malloc( (n)* sizeof(uint*) );
        for(j=0; j<n; j++);
        {
            (*((*(a + i))+j)) = (uint*) malloc(1 * sizeof(uint));
        }
    }

    for(i=0; i<n; i++)
    {
        for(j=0; j<n; j++);
        {
            free (*((*(a + i))+j));
        }
        free (*(a + i));
    }
    free(a);
}
Run Code Online (Sandbox Code Playgroud)

c free pointers

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

C++编译器通常会"优化"malloc并免费使用new和delete吗?

如果我正在编写100%ANSI C但编译.cpp文件,编译器会自动"优化"malloc和免费调用new和delete吗?鉴于他们的差异,这甚至是否有意义?我不认为这是如何运作的,但我的一位朋友说这就是发生的事情.

c c++ malloc free compiler-optimization

0
推荐指数
2
解决办法
220
查看次数