Bonjour,
je demande de l'aide parce que vraiment, je ne comprends pas ce qui se passe :/
Alors voilà, je suis sous Arch Linux, et j'essaye de faire une liste chainée doublement circulaire que voici:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
typedef struct 		s_elem
  {
    char 		*data;
    size_t		sel;
    struct s_elem 	*prev;
    struct s_elem 	*next;
  }			t_elem;
typedef struct 		s_box
  {
    t_elem 		*root;
    size_t		nbr;
    size_t		pos;
  }			t_box;
J'execute cette fonction pour l'initialiser:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
t_box		*new_list(t_box *list)
{
  if ((list = malloc(sizeof(*list))) == NULL)
    {
      printf( "%s\n", "[ERROR]: malloc impossible.");
      exit(0);
    }
  list->nbr = 0;
  list->pos = 0;
  if ((list->root = malloc(sizeof(list->root))) == NULL)
    {
      printf( "%s\n", "[ERROR]: malloc impossible.");
      exit(0);
    }
  list->root->data = NULL;
  list->root->sel = 0;
  list->root->prev = list->root;
  list->root->next = list->root;
  return (list);
}
jusqu'ici, tout va bien. Mais dès que j'essaye de la remplir avec cette fonction, c'est une autre histoire:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void		add_elem(t_box *list, char *val)
{
  t_elem	*new_elem;
  t_elem	*tmp;
 
  if ((new_elem = malloc(sizeof(*new_elem))) == NULL)
    {
      printf( "%s\n", "[ERROR]: malloc impossible.");
      exit(0);
    }
  tmp = list->root->next;
  while (tmp != list->root)
    tmp = tmp->next;
  new_elem->data = val;
  new_elem->sel = 0;
  new_elem->prev = tmp;
  new_elem->next = tmp->next;
  if (tmp == list->root)
    tmp->prev = new_elem;
  tmp->next = new_elem;
  list->root->prev = new_elem;
  list->nbr = (list->nbr) + 1;
}
Voici donc mon problème:
j'obtient un Seg Fault dans la boucle :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
  while (tmp != list->root)
    tmp = tmp->next;
de la fonction add_elem. Je n'arrive pas à comprendre pourquoi.. :/ Surtout que quand j'inverse size_t sel et struct s_elem *next dans ma structure, j'obtient cette erreur:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
malloc.c:2365: sysmalloc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
Abandon (core dumped)
Voici ce que me dit Valgrind:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
==7141== Memcheck, a memory error detector
==7141== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==7141== Using Valgrind-3.9.0 and LibVEX; rerun with -h for copyright info
==7141== Command: ./coucou check.c check.o functions.c functions.o includes link_list.c link_list.o main.c main.o Makefile coucou coucou.c my_select.o pos.c pos.o
==7141== 
==7141== Invalid write of size 8
==7141==    at 0x400CE1: new_list (in /home/coucou)
==7141==    by 0x401522: main (in /home/coucou)
==7141==  Address 0x54410a8 is 0 bytes after a block of size 8 alloc'd
==7141==    at 0x4C27730: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7141==    by 0x400CA1: new_list (in /home/coucou)
==7141==    by 0x401522: main (in /home/coucou)
==7141== 
==7141== Invalid write of size 8
==7141==    at 0x400CF7: new_list (in /home/coucou)
==7141==    by 0x401522: main (in /home/coucou)
==7141==  Address 0x54410b0 is 8 bytes after a block of size 8 alloc'd
==7141==    at 0x4C27730: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7141==    by 0x400CA1: new_list (in /home/coucou)
==7141==    by 0x401522: main (in /home/coucou)
==7141== 
==7141== Invalid write of size 8
==7141==    at 0x400D09: new_list (in /home/coucou)
==7141==    by 0x401522: main (in /home/coucou)
==7141==  Address 0x54410b8 is 16 bytes after a block of size 8 alloc'd
==7141==    at 0x4C27730: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7141==    by 0x400CA1: new_list (in /home/coucou)
==7141==    by 0x401522: main (in /home/coucou)
==7141== 
==7141== Invalid read of size 8
==7141==    at 0x400DA3: add_elem (in /home/coucou)
==7141==    by 0x401555: main (in /home/kinoo_m/coucou)
==7141==  Address 0x54410b8 is 16 bytes after a block of size 8 alloc'd
==7141==    at 0x4C27730: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7141==    by 0x400CA1: new_list (in /home/coucou)
==7141==    by 0x401522: main (in /home/coucou)
==7141== 
a==7141== Invalid read of size 8
==7141==    at 0x400DBC: add_elem (in /home/coucou)
==7141==    by 0x401555: main (in /home/coucou)
==7141==  Address 0x54410b8 is 16 bytes after a block of size 8 alloc'd
==7141==    at 0x4C27730: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7141==    by 0x400CA1: new_list (in /home/coucou)
==7141==    by 0x401522: main (in /home/coucou)
==7141== 
==7141== Invalid read of size 8
==7141==    at 0x400E0D: add_elem (in /home/coucou)
==7141==    by 0x401555: main (in /home/coucou)
==7141==  Address 0x54410b8 is 16 bytes after a block of size 8 alloc'd
==7141==    at 0x4C27730: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7141==    by 0x400CA1: new_list (in /home/coucou)
==7141==    by 0x401522: main (in /home/coucou)
==7141== 
==7141== Invalid write of size 8
==7141==    at 0x400E2E: add_elem (in /home/coucou)
==7141==    by 0x401555: main (in /home/coucou)
==7141==  Address 0x54410b0 is 8 bytes after a block of size 8 alloc'd
==7141==    at 0x4C27730: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7141==    by 0x400CA1: new_list (in /home/coucou)
==7141==    by 0x401522: main (in /home/coucou)
==7141== 
==7141== Invalid write of size 8
==7141==    at 0x400E3A: add_elem (in /home/coucou)
==7141==    by 0x401555: main (in /home/coucou)
==7141==  Address 0x54410b8 is 16 bytes after a block of size 8 alloc'd
==7141==    at 0x4C27730: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7141==    by 0x400CA1: new_list (in /home/coucou)
==7141==    by 0x401522: main (in /home/coucou)
==7141== 
==7141== Invalid write of size 8
==7141==    at 0x400E49: add_elem (in /home/coucou)
==7141==    by 0x401555: main (in /home/coucou/)
==7141==  Address 0x54410b0 is 8 bytes after a block of size 8 alloc'd
==7141==    at 0x4C27730: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7141==    by 0x400CA1: new_list (in /home/coucou)
==7141==    by 0x401522: main (in /home/coucou)
==7141== 
acheck.cacheck.oafunctions.cafunctions.oaincludesalink_list.calink_list.oamain.camain.oaMakefileamy_selectamy_select.camy_select.oapos.c==7141== 
==7141== HEAP SUMMARY:
==7141==     in use at exit: 512 bytes in 17 blocks
==7141==   total heap usage: 17 allocs, 0 frees, 512 bytes allocated
==7141== 
==7141== LEAK SUMMARY:
==7141==    definitely lost: 56 bytes in 2 blocks
==7141==    indirectly lost: 456 bytes in 15 blocks
==7141==      possibly lost: 0 bytes in 0 blocks
==7141==    still reachable: 0 bytes in 0 blocks
==7141==         suppressed: 0 bytes in 0 blocks
==7141== Rerun with --leak-check=full to see details of leaked memory
==7141== 
==7141== For counts of detected and suppressed errors, rerun with: -v
==7141== ERROR SUMMARY: 93 errors from 9 contexts (suppressed: 2 from 2)
Merci d'avance !