标签: malloc

我们可以在c程序中使用太多的malloc和free吗

在程序中调用过多的malloc和free可以吗?

我有一个程序可以为每条记录执行 malloc 和 free 操作。虽然听起来很糟糕,但是如果我使用太多的 malloc 和 free 会不会有性能问题?

malloc

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

Malloc 不为 char* 分配内存

我使用 malloc() 和字符串得到了一个简单的、意想不到的结果。代码:

int main(void) {

char* b64str;
char* binStr = "00000101";

printf("Expected size of allocated structure (b64str): 8\n");
b64str = (char*)malloc((strlen(binStr)+1)*sizeof(char));
printf("Actual size of allocated structure (b64str): %d\n\n", strlen(b64str));
Run Code Online (Sandbox Code Playgroud)

输出:

Expected size of allocated structure (b64str): 8
Actual size of allocated structure (b64str): 0
Run Code Online (Sandbox Code Playgroud)

为什么?

c string malloc char

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

如何在 C 中正确地 malloc 结构体

这是我的完整代码,它看起来可以工作,但效果不是很好。我会接受任何像这样工作的代码。

首先,代码可以工作,但是当我想将第三个名称添加到结构中时,它崩溃了。

还有其他方法可以做到这一点吗?

我需要结构,因为将来我想添加一些其他参数,例如年龄、平均值、性别等。

请帮帮我。

//The student table
typedef struct students {
    char name[50];
} students;

//Global params
int scount = 0;
students *s;

//Basic functions
void addNewStudent();

int main()
{
    int loop = 1;
    char in;
    int ch;
    printf("Willkommen.\n Wahlen Sie bitte von die folgenden Optionen:\n");
    while (loop)
    {
        printf("\t[1] Neue Student eingeben\n");
        printf("\t[9] Programm beenden\n");

        scanf(" %c", &in);
        while ((ch = getchar()) != '\n');
        switch (in)
        {
        case '1':
            addNewStudent();
            break;
        case '9':
            loop = 0;
            break;
        default: …
Run Code Online (Sandbox Code Playgroud)

c malloc struct pointers realloc

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

浮点指针malloc数组接收到数组中

我正在编写一个代码,需要我使用 malloc 函数,将输入输入到数组中,确定并打印平均值以及每个数组值与平均值的关系。这是我的代码

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

int main (){
  int i, n;
  float* numbers;
  float sum, average;
  sum = 0;
  printf("How many numbers? ");
  scanf("%d", &n);
  numbers = (int*)malloc(n * sizeof(int));
  printf("Enter %d decimal numbers\n", n);

for(i = 0; i < n; i++)
{

    scanf("%f", &numbers[i]);
    printf("%f\n", &numbers[i]);
    sum = sum + numbers[i];
}

printf("%f\n", &sum);
 average = sum / n;
 printf("The average was %f\n", &average);
 for(i = 0; i < n; i++)
    {
    if (numbers[i] < average)
        printf("%f …
Run Code Online (Sandbox Code Playgroud)

c arrays malloc

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

通过静态变量分配的内存会发生内存泄漏吗?

我得到了一组源代码,其中的情况如下:

假设我有一个源文件 (source1.c),其中包含:

static float *var;

void alloc_mem ()
{
  var = (float *) malloc(N * sizeof(float));
}

void some_function (float *data)
{
  // do something with var and data
}
Run Code Online (Sandbox Code Playgroud)

在主函数中,我有

int main()
{
  alloc_mem ();
  some_function (data);
}
Run Code Online (Sandbox Code Playgroud)

现在,我无法var通过调用free(var)main来释放分配给的内存,因为它不在那里的范围内。

这种情况会导致内存泄漏吗?如果我在 source1.c 中定义一个函数为

void dealloc_mem()
{
  free(var);
}
Run Code Online (Sandbox Code Playgroud)

并在 main 的末尾调用它,它会起作用吗?

c malloc free static memory-leaks

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

C中的函数指针?

我是 C 语言初学者,正在学习 C 语言中的函数指针。还有我遇到的问题?

编写一个比较函数来按名称的第一个字符排序?

*int (firstnamecharcompar)(const void * a, const void * b))

这是我的代码解决方案。

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

int compare1(const void *a,const void *b)
{
    char *c = *(char**)a;
    char *d = *(char**)b;
    return c[0] - d[0];                             
    //return ( *(char*)a[0] == *(char*)b[0] );                
}

int main()
{
    char* str[3];
    int i;
    for(i=0;i<3;i++)
    {
        str[i] = (char*)malloc(10*sizeof(char));
    }
    for(i=0;i<3;i++)
    {
        printf("Enter %d string => " , i+1 );
        scanf("%s", str[i]);                        
        printf("\n");
    }
    
    for(i=0;i<3;i++)
    {
        printf("%s ",str[i]);
    }
    
    qsort(str,3,10,compare1);
    
    for(i=0;i<3;i++) …
Run Code Online (Sandbox Code Playgroud)

c malloc pointers function-pointers void-pointers

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

使用malloc()后变量的大小是多少?

我想了解动态内存分配的概念。我想知道分配内存后大小有什么变化。

我的代码如下。

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

int main(){
    char *s;
    printf("Size before malloc: %d\n", sizeof(s));

    s = (char*)malloc(1024 * sizeof(char));
    printf("Size after malloc: %d\n", sizeof(s));
    
    printf("Size actual: %d\n", sizeof(s) * 1024);

    s[0] = 'a';
    s[1] = 'b';
    s[2] = 'c';
    s[3] = 'd';
    printf("Size by calculation: %d", sizeof(s) / sizeof(char));

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

当我执行此代码时,我得到如下输出。

Size before malloc: 8
Size after malloc: 8
Size actual: 8192
Size by calculation: 8
Run Code Online (Sandbox Code Playgroud)

那么为什么不在分配内存后返回 8192 作为大小呢?此外,当我将字符添加到s为什么不得到 as时4

c malloc

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

条件跳转或移动取决于 for 循环中 strcat 的未初始化值

我有一个包含 3 条染色体字符串的文件,我想将其连接成一个基因组。然后我必须跨多个线程访问这个串联字符串(我使用 pthread_t)。为此,我必须在提取数据时使用 pthread_mutex_lock,然后使用 strcat 连接使用 const char* 函数 fai_fetch 提取的数据,然后将数据保存为 char* (见下文)。

// genome_size the size of all the chromosomes together
// chr_total the number of chromosomes I wish to concatenate
char* genome = (char*) malloc(sizeof(char) * (genome_size+chr_total));

for (int i = 0; i < chr_total; i++){
    pthread_mutex_lock(&data_mutex);
    const char *data = fai_fetch(seq_ref,chr_names[i],&chr_sizes[i]);
    pthread_mutex_unlock(&data_mutex);
    //sprintf(&genome[strlen(genome)],data);
    strcat(genome,data);  
    //sprintf(genome+strlen(genome),data); //All three gives conditional jump or move error

    //sprintf(genome,data); // THIS SOLVES VALGRIND ISSUE ONE BUT DOES NOT GIVE A CONCATENATED …
Run Code Online (Sandbox Code Playgroud)

c c++ malloc valgrind

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

关于 C 中的强制转换的混乱

我只是想问一下有什么区别

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

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

因为我在做项目时遇到了这种情况,我尝试创建一个数组作为指针,并动态为其分配内存。但是当我使用时,int * a = malloc(sizeof(int))我无法更改数组的任何值,从而导致分段错误,当我将其更改为int * a =(int *)malloc(sizeof(int))它时,一切都开始正常工作。更准确地说,这是有问题的代码:

     int i = 0;
     for (i = 0; i < valCounter; i++) {
          if (strcmp(vars[i].name, name) == 0) {
               return 2;
          }
     }
     strcpy(vars[valCounter].name, name);
     strcpy(vars[valCounter].type, type);
     i--;
     vars[i].intArrayVal = (int *)malloc((dimension + 5) * sizeof(int)); 
     int j = 0;
     char currentNumber[1000];
     char elemente[1000];
     int *pointer;
     strcpy(elemente, elements);
     strcpy(currentNumber, strtok(elemente, ","));
     vars[i].intArrayVal[j++] = atoi(currentNumber);
     while (currentNumber …
Run Code Online (Sandbox Code Playgroud)

c malloc pointers casting

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

只有栈可以存储局部(指针)变量,堆不行吗?

我是 C 和 C++ 新手。我知道每当调用一个函数时,它的变量都会在堆栈上分配内存,包括变量恰好是一个指针,该指针指向通过mallocor分配在堆上的数据new(但我听说不能保证malloc分配的存储100%在堆上,如果我错了,请纠正我)。例如,

\n
Void fn(){\n    Member *p = new Member()\n}\n \n
Run Code Online (Sandbox Code Playgroud)\n

或者

\n
Void fn() {\n        int *p = (int*) malloc( sizeof(int) * 10 );         \n}\n
Run Code Online (Sandbox Code Playgroud)\n

如果我错了,请纠正,在这两种情况下,变量p(保存堆上分配的对象的地址)都在堆栈上,并且它指向堆上的对象。\n所以说所有的都正确吗?我们声明的变量位于堆栈上,即使它们可能指向堆上的某些内容?\n让\xe2\x80\x99s 说局部变量指针的地址p加载到内存地址001,它具有位于堆上的成员对象的地址,该地址是002。我们可以画这样的图。\n在此输入图像描述

\n

如果这是正确的,我的下一个问题是,我们是否可以拥有一个实际位于堆上的指针,并且它指向位于堆栈上的变量?如果不可能,该指针是否可以指向位于堆上的变量?\n也许这个问题的另一种表达方式是:为了访问堆中的某些内容,我们只能通过堆栈上的指针来访问它?\n可能的图表如下所示

\n

如果可能的话,我可以在这里举个例子吗?

\n

在此输入图像描述

\n

c c++ malloc heap-memory

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