标签: dynamic-allocation

关于malloc/calloc调用的奇怪SIGABORT

用gdb运行我的程序我得到了这个:

fem.o:malloc.c:3096:sYSMALLOc:断言`(old_top ==(((mbinptr)(((char*)&((av) - > bins [((1) - 1)*2])) - __builtin_offsetof(struct malloc_chunk,fd))))&& old_size == 0)|| ((unsigned long)(old_size)> =(unsigned long)(((__ builtin_offsetof(struct malloc_chunk,fd_nextsize))+((2*(sizeof(size_t))) - 1))~~((2*(sizeof) (size_t))) - 1)))&&((old_top) - > size&0x1)&&((unsigned long)old_end&pagemask)== 0)'失败.

程序接收信号SIGABRT,已中止.__kernel_vsyscall()中的0xb7fe1424

我发现此代码后出现此错误:

problem->y0 = (double *)calloc(n_tot, sizeof(double));
Run Code Online (Sandbox Code Playgroud)

问题是具有double*y0作为成员的结构.

以前在函数中,我这样做

problem = (fem_problem *)calloc(1, sizeof(fem_problem));
Run Code Online (Sandbox Code Playgroud)

我没有得到任何错误问题== NULL.

一些建议?

加:

我已经检查了n_tot的内容,它有正确的号码

c dynamic-allocation

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

用calloc宏,是安全的吗?

如果我在我的代码中使用这个宏是安全的吗?

#define my_calloc(x, n) ((x) = (__typeof__ (x))calloc((n), sizeof(__typeof__ (&(x)))))
Run Code Online (Sandbox Code Playgroud)

我用gcc作为编译器......

在我的程序中有很多内存分配点,所以我使用它.我在5分钟前试过它,我得到了一些奇怪的sigabort和sigsev,现在我要回家了......如果我能找到一些东西,我会再试一次.

一些想法/提示?

编辑增加:

一般我使用宏如下:

double *x;
my_calloc(x, 10);

int **y;
my_calloc(y, 30);
Run Code Online (Sandbox Code Playgroud)

c dynamic-allocation

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

在c ++中使用带有struct的'new'关键字

#include "PQueue.h"

struct arcT;

struct coordT {
    double x, y;
};

struct nodeT {
    string name;
    coordT* coordinates;
    PQueue<arcT *> outgoing_arcs;
};

struct arcT {
    nodeT* start, end;
    int weight;
};

int main(){
    nodeT* node = new nodeT; //gives error, there is no constructor
}
Run Code Online (Sandbox Code Playgroud)

我的目的是nodeT在堆中创建一个新的.错误是:

错误C2512:'nodeT':没有合适的默认构造函数可用

c++ compiler-errors new-operator dynamic-allocation

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

当动态数组超出范围时释放内存

我正在为函数中的数组动态分配内存.我的问题是:一旦函数完成运行就释放了内存?

码:

void f(){
    cv::Mat* arr = new cv::Mat[1];
    ...
}
Run Code Online (Sandbox Code Playgroud)

c++ scope dynamic-arrays dynamic-allocation

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

动态添加对象到数组

我有这个代码,它工作正常:

News news_data[] = new News[] {
new News("1","news 1","this is news 1"),
new News("2","news 2","this is news 2"),
new News("2","news 1","this is news 2"),
};
Run Code Online (Sandbox Code Playgroud)

在这段代码中我添加了3个新对象,但我必须在循环中动态添加它们.我怎样才能做到这一点?我实际上并不了解这个数组结构.如果你能简单的话,请向我解释一下这段代码

我试过这个,但它不起作用:

   News news_data[];
   for(i=1;i<3;i++){
      news_data=new News[] {
          new News("1","news 1","this is news 1"),
          new News("2","news 2","this is news 2"),
          new News("2","news 1","this is news 2"),
      };
   }
Run Code Online (Sandbox Code Playgroud)

java arrays oop list dynamic-allocation

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

在C++ 11中释放动态分配的uv_timer_t(libuv)实例

我有一个需要将任务安排到libuv事件循环的函数.我的想法是创建一个0ms超时的计时器.我试过以下代码:

void myFunction() {
    ...
    uv_timer_t* timer = new uv_timer_t();
    uv_timer_init(uv_default_loop(), timer);
    uv_timer_start(timer, [&](uv_timer_t* timer, int status) {
        // Scheduled task
    }, 0, 0);
}
Run Code Online (Sandbox Code Playgroud)

这种方法运行良好但问题是,动态分配的计时器永远不会被释放.我已经尝试在回调中释放计时器,但这导致了分段错误:

void myFunction() {
    ...
    uv_timer_t* timer = new uv_timer_t();
    uv_timer_init(uv_default_loop(), timer);
    uv_timer_start(timer, [&](uv_timer_t* timer, int status) {
        // Scheduled task
        delete timer;
    }, 0, 0);
}
Run Code Online (Sandbox Code Playgroud)

我也尝试过调用uv_timer_stop(timer);uv_unref((uv_handle_t*) timer);在实际内存释放之前,但是分段错误仍然是remian.

c++ memory-leaks event-loop dynamic-allocation c++11

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

分配动态分配的指针数组

在这里筛选了很多帖子之后,遇到了很多麻烦.所有东西都编译但是我在这个函数中遇到了崩溃,这个函数应该动态地将一个数组的地址分配到这个指针数组中.我看到一个或两个内存地址发布,所以我不确定为什么它会在这个中间崩溃.

string *copyArray(string ptrArray[],int sizeArray)
   {
    string **dynamString = new string*[sizeArray];
    int i;

    for (i=0;i<=sizeArray;++i)
        {
         (*dynamString[i]) = ptrArray[i];
         cout << dynamString[i];
        }
    return *dynamString;

}
Run Code Online (Sandbox Code Playgroud)

从主要我有:

string *arrPtr;
Run Code Online (Sandbox Code Playgroud)

和函数调用

arrPtr = copyArray(arrayOfStrings, arraySize);
Run Code Online (Sandbox Code Playgroud)

c++ arrays pointers dynamic-allocation

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

试图在C中学习动态内存分配

我正在尝试学习动态内存分配和结构,我有一些问题.

首先

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

int main()
{
    int *number;
    number = malloc(1*sizeof(int));
    int a;
    for(a=0;a<150;a++)
    {
        number[a]=a;
        printf("%d ",number[a]);
    }
    return 0;
 }
Run Code Online (Sandbox Code Playgroud)

在这个样本中,我计划给它错误.因为我将它分配给1个整数的大小,然后我写了太多的整数.它不应该给我一个错误吗?你能详细解释一下吗?

struct people 
{
    char *name;
    int age;
    char *personalInfo;
} human[3];
Run Code Online (Sandbox Code Playgroud)

当我定义这样的结构时,如何分配它以保持超过3个人?如何将其更改为人类[20]或更多?如果答案是写人而不是人[3],我应该如何分配?喜欢malloc(number*sizeof(char)*sizeof(int)*sizeof(char))

还有一件事,在第二个例子中,我需要分配名称和个人信息指针吗?

c malloc dynamic-allocation

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

动态分配内存中free()的范围是什么?

假设我有以下代码:

typedef struct {
  int numBars;
  BarType *bars;
} fooType;

foo = (fooType *) malloc(sizeof(fooType));
foo->bars = (BarType *) malloc(sizeof(barType));
Run Code Online (Sandbox Code Playgroud)

调用free(foo)也会释放条形码或者我需要这样做:

free(foo->bars);
free(foo);
Run Code Online (Sandbox Code Playgroud)

直观地说,我觉得调用free(富)应该是足够了 - 如果我不需要调用free(foo-> numBars)我不应该需要调用free(foo->条).但是我没有为numBars手动分配内存,而是为了吧.

c malloc free dynamic-memory-allocation dynamic-allocation

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

为链表中的节点分配内存,但出乎意料的是,其中的下一个节点也被分配

我正在尝试实现一个链表,但是当我为一个音符分配内存时,其中的指针不是NULL.

这是我的结构

template <typename T>
struct Node {
  T value;
  Node* next;
};
Run Code Online (Sandbox Code Playgroud)

我为笔记分配内存

first = new Node<T>;
Run Code Online (Sandbox Code Playgroud)

first-> next不是NULL.这迫使我明确地将该注释分配给NULL.这让我很困惑.但为什么会这样呢?

c++ pointers linked-list dynamic-allocation

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