标签: free

释放可变参数函数中的所有参数

我的使命是创建一个函数,该函数接收可变数量的参数并为任何类型的参数释放所有参数。我尝试使用可变参数函数来做到这一点,但问题是我不知道参数的类型。

关于如何做有什么建议吗?

c free variadic-functions

-2
推荐指数
1
解决办法
1386
查看次数

c ++如何释放内存*char?

如何使用这样的东西释放内存(Visual Studio 2008 - Win32/console):

我只能包括:iostream

#include <iostream>

void data_t(char *test[])
{
    test[0] = new char[];
    test[1] = new char[];
    test[0] = "Test1";
    test[1] = "Test2";
}

int main()
{
    char *test[2];
    data_t(test);
    cout<<test[0]<<"\n";
    cout<<test[1]<<"\n";
    delete[] test[0];//Debug assertion failed! - The program '[7884] Zadanie_4_sortowanie.exe: Native' has exited with code 3 (0x3).
    delete[] test[1];
}
Run Code Online (Sandbox Code Playgroud)

我做错了什么?

c++ memory free char

-2
推荐指数
2
解决办法
9225
查看次数

在C++中删除新结构

我在C++代码中有错误的行为,似乎是由于动态创建的结构的错误释放造成的.结构形式如下:

typedef struct
{
    char *value;
} Element;

typedef struct
{
    int num;
    Element **elements;
    int *index;
} Container;
Run Code Online (Sandbox Code Playgroud)

它们是这样创建的:

Element *new_element(int size)
{
    Element *elem = new Element;
    elem->value = new char[size];
    return elem;
}

Container *new_container(int num)
{
    Container *cont = new Container;
    cont->num = num;
    cont->elements = new Element*[num];
    cont->index = new int[num];
}
Run Code Online (Sandbox Code Playgroud)

解冻这些的正确方法是什么?

c++ free struct memory-management delete-operator

-2
推荐指数
1
解决办法
89
查看次数

2种免费通话方式 - 有区别吗?

free在堆上分配内存后,这两种调用变量之间是否存在差异:

// variant 1
int* p1 = (int*) malloc(sizeof(int)*4);
free(p1);

//variant 2
int* p2 = (int*) malloc(sizeof(int)*4);
free(*p2);
*p2 = NULL;
Run Code Online (Sandbox Code Playgroud)

c heap malloc free pointers

-2
推荐指数
1
解决办法
80
查看次数

不应该free()接受一个(void*const)作为输入arg

我试图理解免费的工作.是否在标准中定义了free()不会改变它传递的指针?

c free pointers

-2
推荐指数
1
解决办法
72
查看次数

C - 指针在释放后不为空

释放后指针的值是否为NULL?

int* p = malloc(sizeof(*p));

free(p);

if(p==NULL)
    printf("Null\n");
else
    printf("Not null\n");
Run Code Online (Sandbox Code Playgroud)

输出:

 Not null
Run Code Online (Sandbox Code Playgroud)

好吧,我假设没有;

无论如何,我今天早些时候提出了一个问题:

在这里查看: C - 如何释放动态分配的内存?

List* head1 = NULL;

insertFront(&head1, 1);
insertFront(&head1, 2);

print(head1);

while (head1)
{

    List *temp = head1;
    head1 = head1->next;
    free(temp);
}

if(head1 == NULL)
    printf("Null\n");
else
    printf("Not null\n");
Run Code Online (Sandbox Code Playgroud)

这种情况下的输出:

Null
Run Code Online (Sandbox Code Playgroud)

在这种情况下,在释放head1(节点也)后,head1变为null,不是吗?

最后,我错过了一些概念吗?

head1为null,但p不是.

我的问题是:

为什么head1和p之间的值不同?

c free pointers dynamic-memory-allocation

-2
推荐指数
1
解决办法
861
查看次数

如何确定返回字符指针是否需要在C标准库函数中空闲?

我在C程序开发方面经验不多.正如我在标题中所描述的,如何确定返回字符指针是否需要在C标准库函数中空闲?对于例子,做我需要释放函数的返回指针getenvstdlib.hstrstrstring.h中?我在文档中找不到任何描述.

提前致谢.

c free pointers c-standard-library

-2
推荐指数
1
解决办法
989
查看次数

如何在结构中释放char*?

 1| typedef struct container{
 2|     char* abc;
 3| }container;
 4|
 5|
 6| int main(void){
 7|
 8|     container* xyz = malloc(sizeof(container));
 9| 
10|     xyz->abc = malloc(10);
11|     xyz->abc = "abcdefghi\0";
12| 
13|     free(xyz->abc);
14|     free(xyz);
15| }
Run Code Online (Sandbox Code Playgroud)

根据Valgrind的说法,第10行有泄漏,这意味着免费(xyz-> abc)不起作用.我怎么能解脱这个?

c free pointers

-2
推荐指数
1
解决办法
301
查看次数

为什么在使用"free()"之后,内存地址的值总是为0?

我有这个代码:

int * m = (int *)malloc(sizeof(int));
printf("%p %i\n", m, *m);
(*m) = 6;
printf("%p %i\n", m, *m);
free(m);
printf("%p %i\n", m, *m);
Run Code Online (Sandbox Code Playgroud)

我在Linux上运行它.为什么内存地址的值始终为0?

为什么我在Windows上运行它会改变这个值?

c malloc free

-2
推荐指数
1
解决办法
292
查看次数

使用free()无法释放内存

getFree()我在一个类似问题的答案之一中看到它的功能,但是字符串cpy仍然具有第一次给出的相同字符串它运行的次数,即T次.所以,有人请帮忙.

void getFree(char ** ptr) {
    if (*ptr != NULL) {
        free(*ptr);
    }
    return;
}

int main() {
    int T, j;
    scanf("%d", &T);
    for (j = 0; j < T; j++) {
        char * cpy,
        int count = 0, i = 0, x = 0, k = 0;
        cpy = (char*)malloc(500 * sizeof(char));
        if (getchar() == '\n') {
            do {
                *(cpy + i) = getchar();
                i++;
            } while(*(cpy + i - 1) != '\n');
        }
        getFree(&cpy); 
        printf("%s", …
Run Code Online (Sandbox Code Playgroud)

c string malloc free

-3
推荐指数
1
解决办法
81
查看次数

动态分配和释放C

void* heap = malloc(100);
char *c = heap;
strcpy(c, "Terence");
printf("heap = %s\n", heap);
free(heap);
heap = malloc(100);
printf("heap = %s\n", heap);
Run Code Online (Sandbox Code Playgroud)

输出是:

heap = Terence
heap = 
Run Code Online (Sandbox Code Playgroud)

这就是我的期望,但现在我有一个更复杂的代码,结构与上面类似,但输出如下:

heap = "Terence"
heap = "  ren  "
Run Code Online (Sandbox Code Playgroud)

类似的东西.

堆似乎还没有被清理干净?

有办法解决吗?

c heap malloc free

-4
推荐指数
1
解决办法
175
查看次数

为什么指针在释放后会变量存储其先前存储的地址的新地址?

我有两个问题.

  1. freeC 中的函数如何工作?
  2. 为什么指针变量会自行更新以存储新地址?

这是我的代码:

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

int main()
{
    int a;
    int *p;
    p = (int *)malloc(sizeof(int));
    *p = 10;
    int *q = p;
    printf("p:%d\n", p);
    printf("q:%d\n", q);
    printf("*p:%d\n", *p);
    printf("*q:%d\n", *q);
    free(p);
    printf("Memory freed.\n");
    p = (int *)malloc(sizeof(int));
    *p = 19;

    printf("p:%d\n", p);
    printf("q:%d\n", q);
    printf("*p:%d\n", *p);
    printf("*q:%d\n", *q);
}
Run Code Online (Sandbox Code Playgroud)

为什么输出是这样的?

p:2067804800
q:2067804800
*p:10
*q:10
Memory freed.
p:2067804800
q:2067804800
*p:19
*q:19
Run Code Online (Sandbox Code Playgroud)

c malloc free dynamic-memory-allocation

-4
推荐指数
1
解决办法
78
查看次数

c ++ malloc和自由对象的创建和删除

我将首先用C++发布我的测试程序:

#include <iostream>
using namespace std;

class X
{
     int x;
     public:
     X()
     {
       cout<<"constructing\n";
       x=0;
     }
     int& getx()
     {
       return x;
     }
     ~X()
     {
       cout<<"destroying\n";
     }
};
int main()
{
     X* p=(X*)malloc(sizeof(X));
     ++p->getx();
     p->getx()*=5;
     cout<<p->getx();
     free(p);
     return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

5
Run Code Online (Sandbox Code Playgroud)

现在,在任何人抱怨为什么我在C++程序中使用malloc&之前free,我想重申它只是一个测试程序,即使使用operator new&也可以完成上述操作operator delete.但我的问题仍然是:

  1. 即使没有创建X的对象,malloc or operator new我们如何访问类X的变量x?
  2. 显然,free & operator delete也不要破坏对象并执行纯粹的分配.如果我new使用operator delete or free而不是使用而创建一个对象会发生什么delete?我的对象是否仍然存在并且仍然可用吗?

c++ malloc free

-5
推荐指数
1
解决办法
1220
查看次数