标签: malloc

声明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
查看次数

另一个功能的空闲内存

所以我有这个代码,在一个函数中进行分配,并在调用函数中进行解除分配.在尝试通过ptr或*ptr释放内存时获取分段错误或中止消息.请看看:

#include <stdio.h>

int main()
{

    char *ptr;
    fun(&ptr);
    printf("ptr = %p\n",ptr);
    printf("&ptr = %p\n",&ptr);
    printf("String ptr = %s\n",ptr);
    free (ptr);

    return 0;
}
void fun(char **str)
{
    *str = malloc(10);
    *str = "HELLO";
    printf("str = %p\n",str);
    printf("&str = %p\n",&str);

    printf("String str = %s\n",*str);
}
Run Code Online (Sandbox Code Playgroud)

以下是输出:

str = 0x7ffe63247858                                                                                                                 
&str = 0x7ffe63247838                                                                                                                
String str = HELLO                                                                                                                   
ptr = 0x400764                                                                                                                       
&ptr = 0x7ffe63247858                                                                                                                
String ptr = HELLO                                                                                                                   
*** Error in `/home/a.out': munmap_chunk(): invalid pointer: 0x0000000000400764 ***                                                  
Aborted  
Run Code Online (Sandbox Code Playgroud)

题 :

为什么我们不能免费ptr?如果可以的话,最好的方法是什么?

c malloc

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

用于指针的C中的内存分配

我正在尝试在C中构建一个名为PROCESS的结构,这个结构应该包含进程的ID(id)和等待时间(wt).

typedef struct PROC{
    int id;
    int wt;
}PROCESS;

PROCESS *pt = NULL;
Run Code Online (Sandbox Code Playgroud)

现在我想像数组一样创建这个结构的一个实例.我想做的是这样的':

PROCESS pt[10];
pt[0].id = 5;
pt[1].id = 7;
Run Code Online (Sandbox Code Playgroud)

但我想用动态内存分配来做到这一点:

pt = calloc(2,sizeof(PROCESS));

pt[0]->id = 5;
Run Code Online (Sandbox Code Playgroud)

我的错是什么?

c malloc calloc dynamic-memory-allocation

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

unique_ptr中原始指针的默认删除器

使用默认删除器是否安全std::unique_ptr

我想这样使用它:

uint8_t* binaryData = new uint8_t[binaryDataSize];
std::unique_ptr<uint8_t> binPtr(binaryData);
Run Code Online (Sandbox Code Playgroud)

所以默认的删除器std::unique_ptr看起来像这样:

template<typename _Up>
typename enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value>::type
operator()(_Up* __ptr) const
{
    static_assert(sizeof(_Tp)>0,
                  "can't delete pointer to incomplete type");
    delete [] __ptr;
}
Run Code Online (Sandbox Code Playgroud)

从我的观点来看,使用由原始指针分配的原始指针是new[]安全的,而不是由分配的原始指针安全std::malloc.我错过了什么吗?有更好的解决方案吗?

c++ malloc unique-ptr c++11

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

".exe"触发了一个断点

我继承了完全以32位运行的代码,但是当我将编译器更改为64位时,如果运行代码,则会出错.如果我执行代码,它(几乎总是)只是稍后失败.代码中有很多指针,C++不是我强大的套件.是否有人可以了解错误发生的原因?

我需要了解的是:首先创建BYTE***是否正常,然后BYTE**和BYTE*永远不会被分配给.我不明白这是如何工作的.我认为BYTE*会被分配到第一个,然后是BYTE**,然后是BYTE***.为什么这适用于x86,而不适用于x64?

void* AllocMemory(SIZE_T dwBYTEs)
{
    void* result;
    if (dwBYTEs > 0)
    {
        result = malloc(dwBYTEs);
        if (result != NULL)
            return result;
    }
    return 0;
}

BYTE** CreateBytes(int arg_0, int arg_4, int arg_8)
{
    BYTE** hedi = (BYTE**)AllocMemory(arg_0 * 4);
    if (!hedi)return 0;
    hedi[0] = (BYTE*)AllocMemory(arg_0 * arg_8 * arg_4);
    if (!hedi[0]) {
        delete(hedi);
        return 0;
    }
    for (int i = arg_0 - 1; i > 0; i--) {
        hedi[arg_0 - 1 - i + 1] = &hedi[0][arg_4 * arg_8 …
Run Code Online (Sandbox Code Playgroud)

c++ malloc memory-management

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

为什么动态分配内存需要进行类型转换?

在malloc()类型转换返回的C地址中隐式地和在C++中我需要明确地进行类型转换.但我正在使用一个整数指针,它会根据指针算法指出下一个地址,那为什么我需要对内存地址进行类型转换呢?

我实际上正在使用'new'关键字,但我需要清楚我的想法.

c c++ malloc

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

无法释放父结构的子级

我有一个奇怪的错误,我不能释放()一个我malloc()'RAM的结构.

我有一个struct parent和child,其中child是int的结构.我使用malloc()为父结构和子结构分配ram(其中子结构是malloc(),其大小与子结构的数组相同).然后我使用memcpy将数组复制到父结构.

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

typedef struct child child_t;
typedef struct parent parent_t;

struct child {
    int item;
};

struct parent {
    child_t *_child;
};

child_t list[] = {
    { .item = 1 },
    { .item = 2 },
    { .item = 3 },
};

int main(void) {
    parent_t *_parent = malloc(sizeof(parent_t));

    _parent->_child = malloc(sizeof(list));
    memcpy(&_parent->_child, &list, sizeof(list));

    free(_parent->_child);
    free(_parent);
    printf("success\n");
    return 1;
}
Run Code Online (Sandbox Code Playgroud)

free(_parent->_child);只是最终给我Aborted(core dumped)作为错误.我检查了ram的用法,我可以说我应该解雇孩子,但我不知道怎么做.

c memory malloc struct

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

在没有new malloc的情况下,用现有字符串动态构造一个数组

我想动态分配一个字符串数组.所以arr应该包含指向每个字符串的第一个字符的二十个指针.

为简单起见,每个字符串都是相同的,存储在base中.第一个for循环现在应该创建我的数组,这似乎工作得很好.

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

int main()
{
  int n = 20;
  char** arr = malloc( sizeof(char*) + n );

  char* base = "abcdefghijklmnopqrstuvwxyz";

  for(int i = 0; i < n; i++)
  {
    *(arr + sizeof(char*) * i) = base;
  }

  for(int i = 0; i < n; i++)
  {
    printf("%s\n", *(arr + sizeof(char*) * i));
  }
}
Run Code Online (Sandbox Code Playgroud)

循环的秒数在第二次迭代期间创建分段错误.

c malloc pointers segmentation-fault

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

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

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

c c++ malloc free compiler-optimization

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

malloc()函数分配的内存的生命周期

如果我向函数传递一个指针,其中指针获取分配内存的地址,那么当函数退出时是否释放了内存?

void initWomenList(Women **head, Women *headWoman) {        
  headWoman = (Women *) malloc(sizeof(Women));
  if (headWoman == NULL) {
      printf("Allocation of headWoman failed\n");
      exit(1);
  }
  headWoman->userWoman = NULL;
  headWoman->next = NULL;

  head = &headWoman;
}
Run Code Online (Sandbox Code Playgroud)

当函数返回时,head和headWoman都是NULL吗?

c malloc pointers

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