标签: malloc

C替代malloc

在阅读K&R(第6.5节,第二版)时,我遇到了以下功能:

struct tnode *talloc(void)
{
    return (struct tnode *) malloc( sizeof(struct tnode) );
}
Run Code Online (Sandbox Code Playgroud)

该函数分配一些空格来存储struct tnode.我只是想通过询问我是否会达到以下目的来检查我的理解:

struct tnode *talloc(void)
{
    struct tnode s;
    return &s;
}
Run Code Online (Sandbox Code Playgroud)

c malloc struct pointers

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

malloc'd指针将自身重置为NULL?

我使用malloc获得了一些非常奇怪的行为.我从不分配超过4kB,所以这对我来说似乎特别奇怪.我的主要看起来像:

int main(int argc, char **argv)
{
    char *buf;
    char *raw = malloc(1024);
    fgets(raw, 1024, stdin);

    parse(raw, &buf); // Process raw input, get parse length

    printf("raw: 0x%p\n", raw); // Outputs 0x00000000

    if(raw != NULL)
    {  // Only prints when less than 10 characters are entered
        free(raw); 
        printf("raw is not NULL\n");
    }
    free(buf);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我输入少于10个字符时,这可以正常工作,当我输入10个字符时,我得到分段错误,当我输入10个以上时,输出显示raw为NULL.应该注意的是,raw的大小是1024 malloc'd字节,所以我应该有更多的工作空间.

解析函数是:

int parse(char *src, char **dst)
{
    int num_valid = 0, len = strlen(src), j = 0;

    // Count number of valid characters …
Run Code Online (Sandbox Code Playgroud)

c malloc pointers segmentation-fault

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

声明一个带有指针的联合指针

我正在努力学习SSE指令,我渴望增加两个方面.但是,当我尝试初始化其中一个时,程序崩溃了

键入位置时访问冲突

这是抛出错误的代码:

typedef union{
    __m128 vec;
    float* afloat;
}u_float;

int main(){

__declspec(align(16)) u_float *mat1;

mat1 = (u_float*)malloc(sizeof(u_float)*4);
for(int i = 0; i < 4; i++)
    mat1[i].afloat = (float*)malloc(sizeof(float)*4);

for(int i = 0; i < 4; i++)
    for(int j = 0; i < 4; j++)
            mat1[i].afloat[j] = 1; // Error.

return 0;}
Run Code Online (Sandbox Code Playgroud)
  1. 为什么会抛出这个错误?
  2. 哪个是解决问题的最佳方法?

c++ malloc sse unions

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

使用malloc时什么都没有指针?

据我所知,我已经遵循了其他用户提供的建议,但是当我尝试返回指向数组的指针时,我仍然会遇到段错误.main中的printf什么也没有返回,所以我猜测指针不是堆上的任何东西?

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

int * stringy(int length){
   int *ptr = malloc(sizeof(int)*length);
   int i;
   for(i = 0; 1 < length; i++){
      ptr[i] = i;
   }
   return(ptr);
}

void main(int argc, char **argv){
   int strlen = 12;
   int *newptr = stringy(strlen);
   printf("%d\n",newptr[0]);
   free(newptr);
}
Run Code Online (Sandbox Code Playgroud)

c arrays malloc pointers segmentation-fault

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

自动为C结构分配内存

我一直在使用C/C++,但今天我在某本书中看到了一些我以前从未见过的东西:

struct item {
   int code;
   float prize;
};

void main() {

   struct item a,*b;

   a.code = 123;
   a.prize = 150.75;
   printf ("Code: %d, Prize %d", a.code, a.prize);

   b->code = 124;
   b->prize = 200.75;

   printf ("Code: %d, Prize %d", a->code, a->prize);
}
Run Code Online (Sandbox Code Playgroud)

以上打印值正常,这是一个令人惊讶的*b部分.由于b是一个指针,为它分配的内存应该在堆栈上,大小为size_t(例如64位),而其数据只能通过在堆上单独分配来获得:

b = (item*)malloc(sizeof(struct item));
Run Code Online (Sandbox Code Playgroud)

显然,没有必要这样做.这怎么可能?

c malloc struct

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

malloc如何获取比分配的数据更多的字节数据

根据我的理解,malloc()它允许我们在运行时动态分配内存.以下是我正在研究的代码

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main() {
  char *description;
  clrscr();
  description = malloc(2*sizeof(char));
  strcpy(description,"Hello there!");
  printf("%s",description);
  free(description);
  printf("%s",description);
  getch();
}
Run Code Online (Sandbox Code Playgroud)

我的问题是我要求系统在malloc()函数中分配2个字节的内存.因此,当我尝试填充字符串"Hello there!"并打印相同时,我应该只获得字符串的前2个字符作为输出,但我得到了我在strcpy()函数输出中给出的整个字符串.

并且在我使用free()函数后,如果我description再次尝试打印,如果我没有错,我不应该得到任何输出,但我仍然得到相同的字符串.可能知道这是如何工作的.我正在使用turbo C++编译器.

c malloc free pointers dynamic-memory-allocation

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

在C中为字符串分配内存

我刚刚开始C编程,我有一个初学者的问题:

int main(int argc, char *argv[])
{ 
char *a=malloc(1*sizeof(char));
a[0]='a';
a[1]='b';
a[2]='c';
printf("%c\n",a[0]);
printf("%c\n",a[1]);
printf("%c\n",a[2]);
printf("%s\n",a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)

所以我想通过逐个输入字符来创建一个由未知长度字组成的字符串.因为我不知道单词的长度所以我只使用malloc.我计划先为一次性字符分配内存,然后在输入新字符时使用reallocate为下一个字符添加新空间.然而,在我malloc(1*sizeof(char))之后,我应该发现我可以在字符串中添加多个字符,为什么会发生这种情况?这是正确的方法吗?

感谢大家花时间阅读我的长问题:)

c string malloc char

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

使用malloc在for循环中创建链接列表始终返回相同的地址

我正在创建一个带有for循环的链表,并在for循环中执行malloc.我得到的问题是在for循环中,malloc总是返回相同的内存地址!

我认为它应该每次迭代返回一个不同的地址,以便我可以链接节点.当前输出意味着程序总是覆盖相同的地址,我无法链接节点,因为始终只有一个节点.这是对的吗?

struct list_node {
    char *id;
    char *url;
    struct list_node *next;
};
int main(void) {
    int j;
    for (j = 0; j < 5; j++) {
        struct list_node *new_node = malloc(sizeof(struct list_node));
        printf("No %d has address %p\n", j, &new_node);
    }
}

=> 
No 0 has address 0x7fff5db122b0
No 1 has address 0x7fff5db122b0
No 2 has address 0x7fff5db122b0
No 3 has address 0x7fff5db122b0
No 4 has address 0x7fff5db122b0
Run Code Online (Sandbox Code Playgroud)

有人知道这个吗?

c malloc for-loop linked-list

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

运算符 - >在malloc函数中

在这里观看:

struct mystruct{
 int a;
 int b;
};

int main(void){
 struct mystruct* ptr;
 ptr = malloc( 10*sizeof( struct mystruct ) );
Run Code Online (Sandbox Code Playgroud)

这样我就分配了一个struct数组.如果你试图打印,例如ptr [4],你会注意到一个指针.当我有一个指针并且我可以访问一个成员时,我必须使用 - >运算符吗?但如果我这样做:

ptr[4]->a
Run Code Online (Sandbox Code Playgroud)

它不起作用!我必须这样做

ptr[4].a 
Run Code Online (Sandbox Code Playgroud)

让它工作......为什么?我有一个指针!ptr [4] =(ptr + 4)对吗?

在正常数组中也会发生这种情况

struct mystruct array[10];
array[4].a
Run Code Online (Sandbox Code Playgroud)

但是数组是一个指针!

c malloc pointers

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

哪个指针值是malloc调用的最大值

关于malloc的相当简单的问题.我可以在分配区域内设置的最大值是多少.例如:

char *buffer;
buffer = malloc(20);
buffer[19] = 'a'; //Is this the highest spot I can set?
buffer[20] = 'a'; //Or is this the highest spot I can set?
free(buffer);
Run Code Online (Sandbox Code Playgroud)

c malloc

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