标签: malloc

Windows malloc问题?

在Windows(Visual Studio C++ 2010)中,我正在尝试进行简单的字符串复制.这是我的代码:

char * filename;
(...)
filename = (char *) malloc(wcslen(argv[(i + 1)]) + 1);
wcscpy((wchar_t *)filename, argv[i + 1]);
wprintf(L"Filename is: %s", filename);
Run Code Online (Sandbox Code Playgroud)

如果我的argv [i + 1]大于14,程序会崩溃.如果它是14或更低,它运行正常.见下文.

C:\Visual Studio 2010\Projects\test\Release>test.exe -f 12345678901234 aa asas asas
First Argument  argv[1]   -f
Argc = 6
Filename is: 12345678901234
Run Code Online (Sandbox Code Playgroud)

这里有什么问题?我错过了什么?谢谢.

c++ malloc

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

C - Malloc崩溃/指针损坏的堆

我有一个2D LinkedList的结构我正在制作:

struct Node
{
    void *data;
    struct Node *up, *left, *right, *down;
};
Run Code Online (Sandbox Code Playgroud)

因为我已经习惯了Java,所以我创建了一个假装成构造函数的函数,它看起来像:

struct Node* buildNode(void *data)
{
    struct Node *node = malloc(sizeof(struct Node*)); // Program crashes here.
    node->data = data;
    node->up = NULL;
    node->left = NULL;
    node->right = NULL;
    node->down = NULL;
    return node;
}
Run Code Online (Sandbox Code Playgroud)

我的程序在到达malloc第二段中的行时崩溃.如果我*从中删除malloc(sizeof(struct Node*))它,它不会崩溃并正常工作.

为什么是这样?我的buildNode函数只是返回一个指向a的指针Node,而实际上并没有返回Node结构本身.我得到的错误与腐败堆有关,因为我对C有点新,我不明白这一切意味着什么.

谢谢!

c malloc pointers

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

C语言中free()函数的问题

我在while循环中有一个for循环来处理字符串.这基本上是我的代码的结构:

char myString[1000];
//Initialize and maybe change myString
for(/*conditions*/){
    while(/*conditions*/){
        if(strchr(myString,' ') == NULL){
            break;
        }
        char *temp = malloc(sizeof(char) * strlen(myString));
        strcpy(temp,myString);
        *strchr(temp,' ') = '\0';
        strcat(myString," ");
        strcat(myString,temp);
        free(temp);
    }
}
Run Code Online (Sandbox Code Playgroud)

有时,这段代码运行得很好,但有时候进程结束并返回3,这意味着有一个错误(3是我尝试使用NULL时通常会得到的返回值,我不应该喜欢,例如myPointer->examplewhere myPointerNULL ).经过一些测试,我发现引起问题的那条线是free(temp);.我试图替换它,if(temp != NULL){free(temp);}但它没有改变任何东西.我试图tempchar temp[1000]而不是malloc拿走free(temp);线来宣布,但它仍然做同样的事情.如果我拿走free(temp);线路仍然使用malloc问题解决了,但是存在巨大的内存泄漏,所以我不能这样做.如果有错误取决于myString字符串中的内容,这意味着如果那里有某个值,则总会出现错误,如果有另一个特定值,则永远不会出现错误,但我可以我们设法找出哪种类型的价值观有效,哪些价值观无效,似乎是随机的.

为什么free(temp);有时候工作,有时候不工作,怎样才能让它始终有效?

c malloc pointers strlen

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

在C中使用malloc

我是C的初学者.我在想,这是怎么回事malloc.这是一个示例代码,我在试图理解它的工作时写了.

码:

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

OUTPUT:

Array is: 
1 2 3 4 5
Run Code Online (Sandbox Code Playgroud)

在上面的程序中,我只为1个元素分配了空间,但是数组现在包含5个元素.因此,当程序顺利运行而没有任何错误时,目的是什么realloc().

谁有人解释原因?

提前致谢.

c malloc realloc

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

Malloc不会添加新内存

当我尝试使用malloc为我的数组中的浮点数分配新空间时,我遇到了麻烦.我的目标是动态创建数组,每次我想添加一个新的float时,malloc都会为new float添加一个空间.

这是我试图运行的代码,但每次只分配一个带sizeof(float)的数组,即使变量不断增加.

float *funkcia_e(FILE **subor, int *pocet_cien) {
float *pole_cien;
*pocet_cien = 1;

    while (fgets(nazov, sizeof nazov, *subor) != NULL)
    {
        pole_cien = (float*) malloc((*pocet_cien) * 4);

        fscanf(*subor, "%f", &pole_cien[pozicia++]); //This causes problems

        *pocet_cien = *pocet_cien + 1;
    }
}
int main() {
    int pocet_cien = 1;
    float *pole_cien = NULL;

    funkcia_r(&subor, pole_cien, &pocet_cien);
}
Run Code Online (Sandbox Code Playgroud)

以下是记录的调试:https://s.put.re/RR6wqRk.mp4

似乎malloc实际上破坏了数组,而不是对它进行篡改.有任何想法吗?

c arrays malloc pointers

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

为什么malloc似乎允许我写内存?

为什么这不会返回分段错误11?

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

int main(int argc, char const *argv[])
{
    char *test;
    test = (char*) (malloc(sizeof(char)*3));

    test = "foo";
    printf("%s\n", test);

    test = "foobar";
    printf("%s\n", test);

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

我的结果是

foo
foobar
Run Code Online (Sandbox Code Playgroud)

我是C的新手,但是当我使用Mac上的gcc和Windows 10上的Windows Debugger进行编译时,它并没有像我预期的那样崩溃.

我的理解是,通过使用(char*) (malloc(sizeof(char)*3)),我正在创建一个长度为3的字符数组.然后,当我将测试分配给字符串foobar时,我正在写入6个数组位置.

我坐在这里,盯着我看似有效,可运行的代码,摸不着头脑.

c arrays malloc

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

为什么当 malloc 大小这么大时会出现访问内存错误?

正如你所看到的,当我运行它时,我遇到了内存访问错误。但是如果我将 malloc_size 更改为 100,它就可以工作。如何在不更改 malloc_size 的情况下修复它。

int malloc_size = 900000;
float* ptr = (float *)malloc(malloc_size);


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

    ptr[i] = 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ windows malloc memory-leaks large-data

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

C有像Java这样的垃圾收集器吗?

如果我们在Java中执行类似的操作,C中会发生什么:

    temp.next = temp.next.next;
Run Code Online (Sandbox Code Playgroud)

temp.next节点会发生什么?在Java中有一个转储/垃圾收集器; 在C中有类似的东西吗?

c malloc list

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

动态分配和释放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
查看次数

如果在malloc中传递float,double或even char值会发生什么

在详细研究malloc()时,我遇到了这种奇怪的行为.

int *p;
p=(int*)malloc(10.45);
p=(int*)malloc(10.45f);
p=(int*)malloc('j');
Run Code Online (Sandbox Code Playgroud)

程序只使用警告编译任何这些语句并返回有效地址.这里真正的结果是什么?

c malloc

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

标签 统计

malloc ×10

c ×8

pointers ×3

arrays ×2

c++ ×2

free ×1

heap ×1

large-data ×1

list ×1

memory-leaks ×1

realloc ×1

strlen ×1

windows ×1