标签: free

C:释放每一个malloc还是只放一个主要的?

我有一个指针***resultSet,我将其作为参数传递给我的SQL函数,该函数从数据库中读取未知数量的数据.数据的实际处理发生在函数之外.

清理这个指针构造时,我只需释放一次主指针,free就可以找到所有后续分配,还是我必须释放我创建的每个malloc?

while((row = mysql_fetch_row(result))) {

                // allocating the rows pointer
                resultSet = malloc(sizeof(void)*(int)mysql_num_rows);
                (*rows)++;

                for (i=0 ; i < mysql_num_fields(result); i++)               
                {
                // allocating the fields pointer
                *(resultSet+i) = malloc(sizeof(void)*(int)mysql_num_fields);

                // allocating the character pointer
                **resultSet = malloc(sizeof(char)*strlen(row[i])+1);
                (*fields)++;

                snprintf(**resultSet, strlen(row[i])+1, "%s", row[i]);
                printf("%s\t",**resultSet);
                if (i==6)
                {
                printf("\t %s\n",**resultSet);
                }
                }
    }
Run Code Online (Sandbox Code Playgroud)

c malloc free pointers

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

C中的问题使用记忆

请帮助:)操作系统:Linux

在"sleep(1000);"中,此时"top(显示Linux任务)"给我写了7.7%MEM使用.valgrind:没发现内存泄漏.

我明白了,写得正确,所有malloc结果都是NULL.但为什么在这个时候"睡觉"我的程序不会减少记忆?遗失了什么?

抱歉我的英文不好,谢谢


~ # tmp_soft
For : Is it free??  no
Is it free??  yes
For 0 
For : Is it free??  no
Is it free??  yes
For 1 
END : Is it free??  yes
END 

~ #top
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                
23060 root      20   0  155m 153m  448 S    0  7.7   0:01.07 tmp_soft    
Run Code Online (Sandbox Code Playgroud)

完整来源:tmp_soft.c

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

struct cache_db_s
{
 int       table_update;
 struct    cache_db_s …
Run Code Online (Sandbox Code Playgroud)

c linux malloc free cc

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

TObject和NIL Delphi中的析构函数类

我想知道为什么在我调用Free方法后对象不是nil.
我的意思是下一堂课:

type Ta = class(TObject)
public
  i: integer;
  destructor Destroy; override;
end;

destructor Ta.Destroy;
begin
  inherited;
end;

procedure Form1.Button1;
var a: Ta;
begin
  a := Ta.Create;       
  a.Free;

  if a = nil then
    button1.Caption := 'is assigned' 
  else 
    button1.caption := 'is not assigned';
end;
Run Code Online (Sandbox Code Playgroud)

我的问题是,为什么后释放对象是不是nil和我将如何做anil析构函数后不使用a := nil

memory delphi free destructor

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

linux c ++中的内存释放?

在我的应用程序中,我unsigned char*使用了为我的variable()分配内存malloc(),当尝试在析构函数中释放该内存时,我使用此条件来避免双重损坏.

unsigned char *wsqData;
wsqData= (unsigned  char *)malloc( 10000*sizeof( unsigned char));
Run Code Online (Sandbox Code Playgroud)

在我的析构函数中

  if(wsqData!=NULL)
  {
      free(wsqData);
      wsqData=NULL;
  } 
Run Code Online (Sandbox Code Playgroud)

现在问题是当我在析构函数之前释放内存时,这个"如果条件"无法正常工作,它再次尝试释放该变量,它会导致双重损坏错误.这种情况有什么问题?

c++ linux memory malloc free

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

挂在SomeObject.Free上.检查FPC中的对象是否已免费?

如果有一个对象(在我的情况下是TJSONData)并且我想释放这个对象,则程序流有时会挂起在这个位置.我的代码中已经有了类似下面的结构,但有时似乎不适合这种情况:

if Assigned(MyRecord.MyJSONData) then
begin
  MyRecord.MyJSONData.Free;
end;
Run Code Online (Sandbox Code Playgroud)

如果我尝试释放对象两次,我可以在测试中重现此行为.在我的程序中,这通常不会发生,但现在我真正的问题:

有没有办法检查对象是否已经空闲?或者我还需要使用FreeAndNil();?

delphi free hang fpc

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

CPP_TEST.exe中0x7604c128处的未处理异常:0xC00000FD:堆栈溢出

为什么stackover flow会出现在我正在使用的地方fflushfree我的代码中.请帮我.

using namespace std;

    struct abc{
        int x;int y;
        }abc;

int _tmain(int argc, _TCHAR* argv[])
{
    struct abc *xyz = (struct abc *) malloc(sizeof(struct abc));
    xyz->x = 5;
    printf("%d\n", xyz->x);
    //system("pause");
        free(xyz);
     // xyz = NULL;
        fflush(stdout);
        _tmain(NULL, NULL);

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

编辑代码:

xyz:
    struct abc *xyz = (struct abc *) malloc(sizeof(struct abc));
    xyz->x = 5;
    printf("%d\n", xyz->x);
    //system("pause");

    free(xyz);
    xyz = NULL;
    fflush(stdout);
    goto xyz;
Run Code Online (Sandbox Code Playgroud)

c c++ malloc free

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

无法释放内存(断言错误)

当我尝试释放循环缓冲区时,我收到一个断言错误(表达式:crtisvalidheappointer).为什么会这样?

相关结构:

typedef struct quote {
    unsigned int seconds;
    double rate;
} quote;

typedef struct cbuf {
    unsigned int max;
    unsigned int start;
    unsigned int end;
    unsigned int size;
    quote *quotes;
} cbuf;
Run Code Online (Sandbox Code Playgroud)

mallocs和释放的代码块:

#define INITIAL_SIZE 10
static cbuf cb1 = {INITIAL_SIZE, 0, 0, 0, NULL};
cb1.quotes = (quote*)malloc(INITIAL_SIZE * sizeof(quote));
if(cb1.quotes == NULL)
{
    printf("Error - memory allocation failed.");
    exit(1);
}

free(&cb1);
Run Code Online (Sandbox Code Playgroud)

c free pointers

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

免费提供动态分配的内存

我有这个C代码:

int main()
{
    int *p=(int *)malloc(100);   //100 bytes

    for(int i=0;i<10;i++)
    {
        p++;
    }

    free(p);

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

现在我的问题是释放(p)释放所有100个字节或仅90个字节.free()如何知道要释放多少字节..?

c malloc free pointers

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

在C中多次免费分配char*

我在C中有这个代码:

 char* func(char* str, int a) {
        str = malloc(a * sizeof(char));
        return str;
    }
    int main() {
        char* s1 = NULL;
        s1 = func(s1,4);
        s1 = "123";
        free(s1);
        s1 = func(s1, 5);
        s1 = "1234";
        free(s1);
        ...
    }
Run Code Online (Sandbox Code Playgroud)

我一直得到运行时错误(好像它是一个infinte循环)

这是使用s1main()合法的吗?它会产生内存泄漏吗?

c malloc free pointers char

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

如何避免这种内存泄漏?

这是我的代码:

void MIDITest::CreateNoteBlock() {
    IMidiMsgExt* midiMessage = new IMidiMsgExt;
    midiMessage->MakemidiMessageMsg(57, 100, 0, 0, 0);
    queuedNotes.insert(*midiMessage);

    midiMessage = new IMidiMsgExt;
    midiMessage->MakemidiMessageMsg(60, 100, 0, tickSize * 38, 0);
    queuedNotes.insert(*midiMessage);

    midiMessage = new IMidiMsgExt;
    midiMessage->MakemidiMessageMsg(62, 100, 0, 0, 0);
    queuedNotes.insert(*midiMessage);

    midiMessage = new IMidiMsgExt;
    midiMessage->MakemidiMessageMsg(65, 100, 0, tickSize * 32, 0);
    queuedNotes.insert(*midiMessage);

    midiMessage = new IMidiMsgExt;
    midiMessage->MakemidiMessageMsg(57, 0, tickSize * 111, 0);
    queuedNotes.insert(*midiMessage);

    midiMessage = new IMidiMsgExt;
    midiMessage->MakemidiMessageMsg(60, 0, tickSize * 111, 0);
    queuedNotes.insert(*midiMessage);

    midiMessage = new IMidiMsgExt;
    midiMessage->MakemidiMessageMsg(62, 0, tickSize * 75, …
Run Code Online (Sandbox Code Playgroud)

c++ methods free memory-leaks

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

标签 统计

free ×10

c ×6

malloc ×6

pointers ×4

c++ ×3

delphi ×2

linux ×2

memory ×2

cc ×1

char ×1

destructor ×1

fpc ×1

hang ×1

memory-leaks ×1

methods ×1