标签: malloc

malloc是否会生成字符串常量?

首先,如果这是一个天真的问题,请原谅我; 我只是一个初学者,正在努力学习.

我知道:

  char* a = "CD"; 
Run Code Online (Sandbox Code Playgroud)

将字符串存储在只读存储器中 ; 所以对字符串的任何更改都是不可能的.(不变)

但我确实知道使用malloc的情况也是如此;

char* a = malloc(3*sizeof(char)) ;

a = "CD" ; 
a[0] = 'S' ; // even regular a = "MR"; does not work
Run Code Online (Sandbox Code Playgroud)

我很困惑,我以为这是存储在堆中的 ......

为什么我不能修改字符串?

c malloc pointers

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

为什么我的代码出现分段错误?

当我用GCC编译我的代码然后运行它时,当我在代码中调用函数时,它会打印出:“分段错误(内核已转储)”。

我尝试在Google上搜索解决方案。

这是我当前的代码:

char ** saveLevelPositions() {
  int x, y;
  char ** positions;
  positions = malloc(sizeof(char *) * 25);

  for (y = 0; y < 25; y++) {
    positions[y] = malloc(sizeof(char) * 100);

    for (x = 0; x < 100; x++) {
      positions[x][y] = mvinch(y, x);
    }
  }

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

我希望该功能能够正常运行,并且只会给出细分错误。

编辑:有关上下文的一些信息,这是GitHub项目的链接:https : //github.com/xslendix/rogue

c malloc segmentation-fault

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

在函数中使用后,将char指针设置为NULL

在C语言中,我有一个函数,其中要获取一个字符串作为参数,然后在使用它后要销毁它,因为我必须在无限循环中调用它,并让Process在5之后返回-1073741819(0xC0000005)分钟。

这是我的功能:

void renderText(char *text) {
    //use it here and then destroy it.
    *text = NULL; //not working!
    text = NULL; //also not!
    text[0] = '\0'; //also not!
}
Run Code Online (Sandbox Code Playgroud)

将参数传递为:

renderText("Hello There!");
Run Code Online (Sandbox Code Playgroud)

我可以使用一个malloc()函数来创建一个字符串,然后可以传递给上面的函数,但是我必须无限次调用它,以便函数可以通过引用调用指针来使它为NULL。

c malloc null pointers

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

释放C中的结构还会删除其成员吗?

我是C语言的新手,我对分配内存有疑问。因此,我在下面尝试了应释放结构elem1的这段代码。

struct elem{

    char *data1;
    char *data2;

};

int main()
{

    struct elem *elem1 = malloc(sizeof(struct elem));
    elem1->data1 = "abc";
    elem1->data2 = "def";
    char *a = elem1->data1;
    char *b = elem1->data2;
    free(elem1);

    printf("%s\n%s\n",a,b);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

该代码可以很好地编译并返回,

abc 
def
Run Code Online (Sandbox Code Playgroud)

我希望它会失败,因为免费还应该释放其成员的记忆。但是为什么行得通呢?释放结构后,如果我想访问结构的成员该怎么办?

c malloc free

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

如何在C中将内存分配给字符串数组-malloc错误

问题:我想为固定大小的字符串数组的元素分配内存,但是,我遇到崩溃的机会是75%。

那是我的程序完美运行的25%的时间,但是错误是我以前从未遇到过的错误

#define PACKAGE_COUNT 60
#define MAX_COUNT     5

const char *colors[] = { "blue", "red", "yellow", "purple", "orange" };

char **generatePackage() {


  char **generatedPackage = malloc(PACKAGE_COUNT);
  int randomIndex         = 0;

  for (int i = 0; i <= PACKAGE_COUNT; ++i) {

    randomIndex         = rand() / (RAND_MAX / MAX_COUNT + 1);
    generatedPackage[i] = malloc(sizeof(char) * sizeof(colors[randomIndex])); 
    // ERROR

    strcpy((generatedPackage[i]), colors[randomIndex]);

    // printf("generatePackage - %d: %s \n", i + 1, generatedPackage[i]);
  }

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

在此处输入图片说明 在此处输入图片说明 在此处输入图片说明

c malloc runtime-error

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

指针堆分配如何为C中的结构分配足够的空间?

以下代码可以编译并完美运行:

typedef struct n {
    char value;
    struct n* next;
} node;
void insert_new_node(node** head, char new_value)
{
    node* new_node = malloc(sizeof(node*));
    new_node->value = new_value;
    new_node->next = NULL;

    if(*head == NULL)
    {
        *head = new_node;
    }
    else
    {
        node* current = *head;

        while(current->next != NULL)
        {
            current = current->next;
        }

        current->next = new_node;
    }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是-请注意,我实际上实际上只是为指向结构的指针分配了空间,而不是为结构本身分配了空间(ref:)node* new_node = malloc(sizeof(node*));。所以我的问题是,该结构数据实际存储在哪里,如果存储在堆中,它将如何工作?

c memory malloc pointers dynamic-memory-allocation

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

如何使用free释放使用malloc进行的堆分配?

free()在令牌生成器中使用堆释放时遇到问题。令牌生成器是递归下降解析计算器的一部分,该计算器可以完美地工作。但是在合并了对释放函数的调用后,它的行为就不稳定。实际上,计算器可能永远不会耗尽其堆,编写具有内存泄漏的程序只是一种不好的做法。

标记化

#define OPERAND 0
#define OPERATOR 1
#define PARENTHESIS 2
#define TERMINAL 3
#define ADD '+'
#define SUBTRACT '-'
#define MULTIPLY '*'
#define DIVIDE '/'
#define EXPONENT '^'
#define L_PARENTHESIS '('
#define R_PARENTHESIS ')'

typedef struct {
    int id;
    char *value;
} token;

int token_count();
token *tokenize();
void deallocate();
Run Code Online (Sandbox Code Playgroud)

标记化

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

#include "tokenize.h"

int token_count(char string[]) {
    int i = 0;
    int count = 0;
    while (string[i] != '\0') {
        if (string[i] >= '0' …
Run Code Online (Sandbox Code Playgroud)

c malloc memory-leaks dynamic

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

在特殊情况下使用void **是否安全?还是不确定的行为?

我想创建一个函数来释放指针所指向的内存,并将该指针分配为NULL。所以我必须将参数声明为void**(间接指针),我知道void*转换指针是安全的,并且使用void**是未定义的行为。但我认为在这种情况下应该没问题,此功能是否可以安全地释放malloc()没有未定义行为的分配的内存?要求黑客提供帮助或提出更好的方法。

C代码:

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

// If I use safeFree(&mac_arr); ,
// it will warn "Incompatible pointer types passing ' int **' to parameter of type ' void **'"
// So I used #define, don't care about this.

#define SAFE_FREE(p) safeFree((void **)&(p))

void safeFree(void ** ptr);

int main(void) {
    int *mac_arr = malloc(sizeof *mac_arr);
    SAFE_FREE(mac_arr);
    return 0;
}

void safeFree(void ** ptr){
    if (ptr != NULL && *ptr != NULL) { …
Run Code Online (Sandbox Code Playgroud)

c malloc

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

c中的malloc函数?

我不明白以下程序输出pf的原因

#include <stdio.h>
#include <stdlib.h>
int main(){
int* ip = (int*)malloc(100*sizeof(int));
if (ip){
int i;
for (i=0; i < 100; i++)
  ip[i] = i*i;
}
free(ip);
int* ip2 = (int*)malloc(10*sizeof(int));
printf("%d\n", ip[5]);
printf("%d\n", ip2[5]);
ip[5] = 10;
printf("%d\n", ip2[5]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出显示ip&ip2在堆中具有相同的引用。当我们分配100 int in(ip​​2)时,它如何为ip1返回相同的引用。它是malloc函数的功能吗?我知道malloc像“ new”一样工作,对吗?如果这样做,那么它应该返回一些随机引用,对吗?

c malloc

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

为什么char指针保存的数据比C中分配的内存多?

在C语言中进行动态内存分配时,将内存大小分配给char指针时,我感到困惑。虽然我只给出1个字节作为限制,鉴于每个字母对应于1个字节,char指针成功地尽可能长地获取了输入

我也试图找到输入前后的指针大小。有人可以帮我了解这里发生了什么吗?输出使我感到困惑。

看下面的代码:

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

int main()
{
    int limit;

    printf("Please enter the limit of your string - ");
    gets(&limit);

    char *text = (char*) malloc(limit*4);

    printf("\n\nThe size of text before input is %d bytes",sizeof(text));

    printf("\n\nPlease input your string - ");
    scanf("%[^\n]s",text);

    printf("\n\nYour string is %s",text);

    printf("\n\nThe size of char pointer text after input is %d bytes",sizeof(text));

    printf("\n\nThe size of text value after input is %d bytes",sizeof(*text));

    printf("\n\nThe size of ++text value after input is …
Run Code Online (Sandbox Code Playgroud)

c malloc pointers char

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