标签: malloc

使用malloc的数组中的SIGSEGV错误

我在这段代码中收到SIGSEGV错误,我无法弄明白.所以请有人帮助我.我无法理解如果我通过malloc函数将内存分配给数组,我必须以零索引或1启动它.

#include<iostream>
#include<stdio.h>
using namespace std;
#include<malloc.h>

long long int gold_coins(long long int[],long long int);

int main()
{
    long long int n,i,d;
    long long int* m;
    cin>>n;
    while(n!=EOF)
    {
        m = (long long int*) malloc(n+1);
        for(i=0;i<=n;i++)
           m[i]=i;
        d=gold_coins(m,n);
        cout<<d<<endl;
        cin>>n;
    }
    return(0);
}

long long int gold_coins(long long int m[],long long int n)
{
    if(n<4)
        return m[n];
    else
    {
        long long int q=gold_coins(m,n/2)+gold_coins(m,n/3)+gold_coins(m,n/4);
        if(q>m[n])
            m[n]=q;
        return(m[n]);
    }
}
Run Code Online (Sandbox Code Playgroud)

提前致谢.

c++ malloc

-6
推荐指数
1
解决办法
176
查看次数

将static_cast <char*> malloc/free转换为new/delete

因为与malloc/free相关的分段错误发生,我想将malloc/free转换为new/delete.当malloc/free转换为下面时发生错误.让我知道如何解决它.

(原版的)

char *only_valid_data = static_cast<char*> (malloc (data_size));
Run Code Online (Sandbox Code Playgroud)

(经换算)

char *only_valid_data = new static_cast<char*> [data_size];
Run Code Online (Sandbox Code Playgroud)

c++ malloc static-cast

-6
推荐指数
1
解决办法
888
查看次数

sizeof动态数组不正确

在阵列有四个元件,因此大小应4BIT*4 = 16(int数据类型采取在我的系统4位以存储该值.)但是,当我跑这个代码我只得到8位为的大小dynamicArray.

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

int main(void) {

    //Dynamic arrays save memory by creating a pointer that stores
    //the beginning of the array
    int *dynamicArray = malloc(20 * sizeof(int));
    *dynamicArray = 10;
    printf("Address %x stores value %d\n", dynamicArray, *dynamicArray);

    dynamicArray[1] = 20;
    printf("dynamicArray[1] stores value %d\n", dynamicArray[1]);
    dynamicArray[2] = 45;
    printf("dynamicArray[2] stores value %d\n", dynamicArray[2]);
    dynamicArray[3] = 34;
    printf("dynamicArray[3] stores value %d\n", dynamicArray[3]);
    printf("The size of dynamicArray is %d\n", sizeof(dynamicArray));

    // Release unused memory:
    free(dynamicArray);

    return …
Run Code Online (Sandbox Code Playgroud)

c malloc sizeof dynamic-arrays

-6
推荐指数
1
解决办法
124
查看次数

Windows内存格式化问题

我正在尝试以可移植的方式使这种动态重新分配工作。

我的程序接受用户的一行文本并将其附加到缓冲区。如果缓冲区中的文本长度为20个或更多,它将删除前20个字符,并将其后的所有字符移到缓冲区的开头。

我有这段代码可以在Linux上正常工作,但是当我在Windows上运行它时会发出垃圾。有谁知道为什么/如何仅使用malloc使其具有可移植性。IE浏览器不使用string.h(strcpy)的str ...除了伦。

仅限于c17-无折断的钉子(不可移植)。这是我的代码。编译无误gcc 7.3,mingw 7.3。我用更安全的功能替换了gets和puts函数,但在Windows上仍然出现垃圾。我认为这是一个格式问题...

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

void wbuff (message)
    char *message;
{
    FILE *f = fopen("file.txt", "w");
    fprintf(f, "%s", message);
    fclose(f);
}

char *rean (message)
    char *message;
{
    /* performs (write) on buffer, trims lefover, then restores */

    char buf[80] = "";
    puts("enter a line");
    gets(buf);

    int bln  =  strlen( buf );
    int mln  =  strlen( message );
    int nln  =  bln + mln;
    printf("new length %d\n", nln);

    message = realloc(message, …
Run Code Online (Sandbox Code Playgroud)

c malloc portability string.h c17

-9
推荐指数
1
解决办法
133
查看次数

Valgrind 检测到内存泄漏,找不到 C

Valgrind 正在发现内存泄漏,但我似乎无法确定它们,我希望这里有人可以帮助我:

在此处输入图片说明

在此处输入图片说明

主要电话是 Dictionary* dictionary = initDictionary();

c memory malloc valgrind

-10
推荐指数
1
解决办法
65
查看次数

当你可以使用指针时,使用malloc有什么意义?

你不能只使用指针存储尽可能多的数据吗?你为什么要malloc()用来获得更多的记忆?

int * a;
int max, i;
printf("Enter the maximum number you want: ");
scanf("%d", &max);

for (i = 0; i < max; i++)
{
    * (a + i) = i;
}

for (i = 0; i < max; i++)
{
    printf("%d\n", * (a + i));
}
return 0;
Run Code Online (Sandbox Code Playgroud)

所以我让用户选择任何数字,计算机将"分配内存"吧?而不是使用以下代码:

a = (int *) malloc(10 * sizeof(int));
Run Code Online (Sandbox Code Playgroud)

c malloc

-23
推荐指数
1
解决办法
1263
查看次数

标签 统计

malloc ×6

c ×4

c++ ×2

c17 ×1

dynamic-arrays ×1

memory ×1

portability ×1

sizeof ×1

static-cast ×1

string.h ×1

valgrind ×1