Bonjour je suis actuellement de développer un module de gestion de tableau de chaîne de caractère, j’essaie de faire en sorte qu'il soit redimensionnable mais j'ai une erreur de mémoire et je ne vois pas d'où elle peu venir se qui me pose problème pour la régler pouvez vous m'aidez svp
strarray.c (module de gestion de tableau de chaîne)
chararray.c (module de gestion de chaîne)
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 #include <stdio.h> #include <stdlib.h> #include "tools.h" #include "strarray.h" #include "chararray.h" //constructeur strarray strarray_create(int len) { strarray tab = tools_malloc(sizeof(struct _strarray)); tab->len = len; tab->alloc = len; tab->data = tools_malloc(sizeof(chararray) * len); for(int i = 0; i < len; i++) tab->data[i] = str_to_chararray("void"); return tab; } //destructeur void strarray_destroy(strarray tab) { for(int i = 0; i < tab->alloc; i ++) chararray_destroy(tab->data[i]); tools_free(tab->data, sizeof(chararray) * tab->alloc); tools_free(tab, sizeof(struct _strarray)); } //tools void strarray_debug(strarray tab) { for(int i = 0; i < tab->len; i ++) { chararray_debug(tab->data[i]); fprintf(stderr, "\n"); } } void strarray_resize(strarray tab, int new_alloc) { int i; chararray* new_data = tools_malloc(sizeof(chararray) * new_alloc); for(i = 0; i < tab->len; i ++) new_data[i] = str_to_chararray("BOOOOOOO"); tools_free(tab->data, sizeof(chararray) * tab->alloc); tab->data = new_data; tab->alloc = new_alloc; } void strarray_add(strarray tab, char* str); void strarray_add_to_index(strarray tab, int index, char* str) { if((tab->alloc <= 0) || index < 0) { fprintf(stderr, "ERROR <strarray_add> : tableau trop petit\n"); return; } if(index < tab->len) { chararray_set_str(tab->data[index],str); return; } if(index >= tab->alloc ) { strarray_resize(tab, (index + 2) * 2); } for(int i = tab->len; i < index; i ++) tab->data[i] = str_to_chararray("void"); chararray_set_str(tab->data[index], str); if(tab->len <= index) tab->len = index +1; }
tools.c
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124 #include <stdio.h> #include <stdlib.h> #include "tools.h" #include "chararray.h" //constructeur chararray chararray_create(int len) { chararray tab = tools_malloc(sizeof(struct _chararray)); tab->len = len; tab->alloc = len; tab->data = tools_malloc(sizeof(char) *len); for(int i = 0; i < len; i++) { tab->data[i] = 'F'; } return tab; } chararray str_to_chararray(char* str) { chararray tab = tools_malloc(sizeof(struct _chararray)); tab->len = size_str(str); tab->alloc = size_str(str); tab->data = tools_malloc(sizeof(char) * tab->alloc); for(int i = 0; i < tab->alloc; i ++) { tab->data[i] = str[i]; } return tab; } char* chararray_to_str(chararray tab) { char* str = tools_malloc(sizeof(char) * tab->len); for(int i = 0; i < tab->len; i++) str[i] = tab->data[i]; return str; } //destructeur void chararray_destroy(chararray tab) { tools_free(tab->data, sizeof(char) * tab->alloc); tools_free(tab, sizeof(struct _chararray)); } void str_destroy(char* str) { tools_free(str, sizeof(char) * size_str(str)); } //modulation de la taille void chararray_resize(chararray tab, int new_alloc) { char* new_data = tools_malloc(sizeof(char) * new_alloc); for(int i = 0; i < tab->len; i ++) new_data[i] = tab->data[i]; tools_free(tab->data, sizeof(char) * tab->alloc); tab->data = new_data; tab->alloc = new_alloc; } void chararray_set_add(chararray tab, int index, char value) { if(tab->alloc <= 0) { fprintf(stderr, "ERROR <chararray_set_add> : Impossible d'ajouter un element alloc : %c\n", tab->alloc); return; } if(index < tab->len) { tab->data[index] = value; tab->len ++; return; } if(index >= tab->alloc) { chararray_resize(tab, (index + 2) * 2); } for(int i = tab->len; i < index; i ++) tab->data[i] = 'F'; tab->data[index] = value; if(tab->len <= index) tab->len = index +1; } void chararray_set_str(chararray tab, char* str) { int i; for (i = 0; i < size_str(str); i ++) { chararray_set_add(tab, i, str[i]); } } //tools void chararray_debug(chararray tab) { for(int i = 0; i < tab->len; i++) { fprintf(stderr, "%c", tab->data[i]); } fprintf(stderr, "\n"); }
test.c
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 #include <stdio.h> #include <stdlib.h> #include "tools.h" void tools_memory_init(void) { GLOBAL_MEMORY = 0; } void* tools_malloc(int alloc) { void* ptr = malloc(alloc); GLOBAL_MEMORY += alloc; return ptr; } void tools_free(void* ptr, int alloc) { free(ptr); GLOBAL_MEMORY -= alloc; } void tools_check_at_end_of_app(void) { if(GLOBAL_MEMORY != 0) fprintf(stderr, "Il y a une fuite de memoire de %d octe\n", GLOBAL_MEMORY); } int string_to_int(char* str) { int result = 0, i; for(i = 0; str[i] != '\0'; i ++) { result *= 10; result += str[i] - 48; } return result; } int size_str(char* str) { int result = 0; for(int i = 0; str[i] != '\0'; i ++) result ++; return result; }
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 #include <stdio.h> #include <stdlib.h> //#include <SDL.h> //#include <SDL_image.h> //#include "map.h" #include "tools.h" #include "chararray.h" #include "strarray.h" int main(void) { tools_memory_init(); strarray tab = strarray_create(5); strarray_add_to_index(tab, 8, "anatole"); strarray_debug(tab); strarray_destroy(tab); tools_check_at_end_of_app(); return EXIT_SUCCESS; }
Partager