【问题标题】:small linked list programme in c, random printf outputc中的小链表程序,随机printf输出
【发布时间】:2020-03-17 16:26:53
【问题描述】:

我正在编写一个小学校程序,我必须使用 'void const *content' 作为参数。 我无法打印新节点的内容。没有'const',代码可以正常工作并显示一切正确。有人能指出我做错了什么吗?

终端输出: Ø 6


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

    typedef struct s_list
    {
        void        *content;
        size_t      content_size;
        struct      s_list *next;
    }               t_list;

t_list  *lstnew(void const *content, size_t content_size)
{
    struct s_list *new = (struct s_list*)malloc(sizeof(struct s_list*));
    if(new == NULL){
        printf("No allocation!");
        exit(1);
    }

    new->content = &content;
    new->content_size = content_size;
    new->next = NULL;


    return(new);
}

int     main(void)
{

    printf("%s\n", lstnew("Hello", 6)->content);
    printf("%zu\n", lstnew("Hello", 6)->content_size);

    return(0);
}

【问题讨论】:

  • 第一次观察:malloc(sizeof(struct s_list*)) 没有分配足够的内存 - 只够一个指针。然后new-&gt;content = &amp;content; 使用的是传递参数的位置,而不是参数本身。

标签: c linked-list printf undefined-behavior singly-linked-list


【解决方案1】:

您在此处获取局部变量的地址:

new->content = &content;

相反,只取值:

new->content = content;

另外,你没有在这里分配足够的内存;您只为指针分配了足够的空间,而不是结构的大小:

struct s_list *new = (struct s_list*)malloc(sizeof(struct s_list*));

malloc 的演员表也是不必要的。我会这样写:

struct s_list *new = malloc(sizeof(*new));

不要使用typedeft_list,而应该在任何地方都使用struct s_list,因为该结构不是不透明的。

【讨论】:

    【解决方案2】:

    在此声明中

    struct s_list *new = (struct s_list*)malloc(sizeof(struct s_list*));
    

    不是为struct s_list 类型的对象分配内存,而是为struct s_list * 类型的指针分配内存。

    你必须写任何一个

    struct s_list *new = malloc( sizeof( struct s_list ) );
    

    t_list *new = malloc( sizeof( t_list ) );
    

    在此声明中

    new->content = &content;
    

    左侧操作数的类型为void *,而右侧操作数的类型为const void **。此外,您正在使用指向局部变量content(函数参数是其局部变量)的指针,该指针在退出函数后将不再存在。

    你需要的是分配内存,并将分配的内存中的变量内容的内容复制过来。

    这是一个演示程序,展示了如何定义函数。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct s_list
    {
        void *content;
        size_t content_size;
        struct s_list *next;
    } t_list;
    
    t_list * lstnew( const void *content, size_t content_size )
    {
        t_list *new = malloc( sizeof( t_list ) );
    
        if ( new != NULL )
        {
            new->content = malloc( content_size );
    
            if ( new->content == NULL )
            {
                free( new );
                new = NULL;
            }
            else
            {
                memcpy( new->content, content, content_size );
                new->content_size = content_size;
                new->next = NULL;
            }
        }
    
        return new;
    }
    
    
    int main(void) 
    {
        t_list *head = lstnew( "Hello", 6 );
        head->next = lstnew("World!", 7 );
    
        printf( "%s %s\n", ( char * )head->content, ( char * )head->next->content );
    
        return 0;
    }
    

    程序输出是

    Hello World!
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-17
      • 2018-09-21
      • 2016-01-16
      • 2020-06-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多