简体   繁体   English

分配结构数组时出现段错误

[英]Segfault error when mallocing an array of structs

after a long time spent trying to debug this I've come for your help.经过很长时间试图调试这个我来寻求你的帮助。 Basically in this exercise I'm trying to read the string "31|Name1;23|Name2;15|Name3" and store it in an array of struct s_perso where the |基本上在这个练习中,我试图读取字符串"31|Name1;23|Name2;15|Name3"并将其存储在struct s_perso的数组中,其中| are marking the end of an age and the beginning of a name, and where the ;标志着一个时代的结束和一个名字的开始,以及在哪里; are marking the beginning of a new struct.正在标记一个新结构的开始。

Here's the given ft_perso.h:这是给定的 ft_perso.h:

#include <string.h>
#ifndef FT__PERSO__H
#define FT__PERSO__H

typedef struct      s_perso
{
    char    *name;
    float   life;
    int     age;
    char    *profession;
}   
                        t_perso;

#endif

We will only use the datas age and name from this struct s_perso .我们将仅使用此结构s_perso中的数据agename

Here's my code:这是我的代码:

#include "ft_perso.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int     numberofstructs(char *str)
{
    int         i;
    int         length;

    i = 0;
    length = 0;
    if (str[0])
        length = 0;
    else
    {
        while (str[i])
        {
            if (str[i] == ';')
                length += 1;
            i++;
        }
    }
    return (length);
}

int     get_data_length(char *str, int i)
{
    int         length;

    length = 0;
    while (str[i] != '|' && str[i] != ';' && str[i] != '\0')
    {
        length++;
        i++;
    }
    return (length);
}

char    *get_data(char *str, int i)
{
    int j;
    char    *str2;

    j = 0;
    str2 = (char *)malloc(sizeof(char) * get_data_length(str, i) + 1);
    while (str[i] != '|' && str[i] != ';' && str[i] != '\0')
    {   
        str2[j] = str[i];
        i++;
        j++;
    }
    str2[j] = '\0';
    return (str2);
}

t_perso     **ft_decrypt(char *str)
{
    int             i;
    int             j;
    t_perso         **textttt_perso;

    i = 0;
    j = 0;
    textttt_perso = (t_perso **)malloc(sizeof(t_perso **));
    *textttt_perso = (t_perso *)malloc(sizeof(t_perso *) * numberofstructs(str));
    
    while (j <= strlen(str) && str[j])
    {
        if (str[j] == ';')
        {
            i++;
            j++;
        }
        textttt_perso[i]->age = atoi(get_data(str, j));
        j = j + get_data_length(str, j) + 1;
        textttt_perso[i]->name = get_data(str, j);
        j = j + get_data_length(str, j);
    }
    textttt_perso[i+1] = 0;
    return (textttt_perso);
}

int     main(void)
{
    int i;
    t_perso **tab;
    i = 0;
    char        str[29] = "31|Name1;23|Name2;15|Name3";
    tab = ft_decrypt(str);
    while(i <= numberofstructs(str))
    {
        printf("age = %d\n", tab[i]->age);
        printf("age = %s\n", tab[i]->.name);
        i++;
    }
}

From my debugging, I get the segfault error on the second call (when i = 1 and we are working on the substring 23 ) instruction of t_perso **ft_decrypt(char *str) :从我的调试中,我在第二次调用(当i = 1并且我们正在处理 substring 23时)得到t_perso **ft_decrypt(char *str)指令的段错误:

textttt_perso[i]->age = atoi(get_data(str, j));

My guess is that my allocation of memory either for the array of struct in itself or the number of arrays it can contain is wrong.我的猜测是,我为结构本身的数组或它可以包含的 arrays 的数量分配 memory 是错误的。 I can't point my finger on the problem tho...我不能把我的手指指向这个问题...

Thanks in advance for your help, have a nice day !提前感谢您的帮助,祝您有美好的一天!

You never allocate space for an actual structure.您永远不会为实际结构分配空间。 In your example:在您的示例中:

textttt_perso = (t_perso **)malloc(sizeof(t_perso **));

allocates space for one pointer and:为一个指针分配空间并且:

*textttt_perso = (t_perso *)malloc(sizeof(t_perso *) * numberofstructs(str));

allocates enough space for 3 pointers.为 3 个指针分配足够的空间。 At some point you need to allocate space for the actual structures.在某些时候,您需要为实际结构分配空间。

You also have other issues.你还有其他问题。 In numberofstructs() you have if(str[0]) that will cause length to always be zero.numberofstructs()你有if(str[0])这将导致length始终为零。 Also in numberofstructs() , you count the semi-colons.同样在numberofstructs()中,您计算分号。 If there is data after the last sem-colon you would need to add 1 to length .如果最后一个分号后有数据,则需要将 1 添加到length

You have many other issues in this code that will show up if the data isn't perfect but here is an implementation of ft_decrypt that should work.如果数据不完美,这段代码中还会出现许多其他问题,但这里有一个应该可以工作的ft_decrypt实现。 Initial malloc should be to hold the array of pointers.初始 malloc 应该是保存指针数组。 Then the loop should allocate a structure for each array entry.然后循环应该为每个数组条目分配一个结构。

t_perso** ft_decrypt(char* str)
{
  int i = 0;
  int j = 0;
  t_perso** textttt_perso;

  textttt_perso = malloc(sizeof(*textttt_perso) * numberofstructs(str));

  while (j <= strlen(str) && str[j])
  {
    if (str[j] == ';')
    {
      i++;
      j++;
    }
    textttt_perso[i] = malloc(sizeof(*textttt_perso[i]));
    textttt_perso[i]->age = atoi(get_data(str, j));
    j = j + get_data_length(str, j) + 1;
    textttt_perso[i]->name = get_data(str, j);
    j = j + get_data_length(str, j);
  }
  return (textttt_perso);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM