Hello Hello !
Hier soir, en m'ennuyant je voulus faire un petit test pour m'amuser,
essayer de "simuler" la classe String de C++. Mais bien sûr, comme d'habitude, quand je veux faire de petites choses.. POUF une erreur du coup je me rends encore compte de mon faible niveau ^^

Donc du coup voilà le code:
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
typedef struct _string
{
  char *ch;
  size_t len;
}String;
 
String *
create_string(char *str)
{
  String *ptr = NULL;
 
  ptr->ch = malloc(sizeof(char) * strlen(str));
  strcpy(ptr->ch, str);
  ptr->len = strlen(ptr->ch);
 
  return ptr;
}
 
int
main(void)
{
  String *chaine = create_string("Saluuu");
 
  printf("chaine->str <---> %s\nchaine->len <---> %d\n", chaine->ch, chaine->len);
  return 0;
}
Et je mange un joli:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
Erreur de segmentation (core dumped)
Bon certes ce n'est pas l'erreur du siècle mais être bloqué par de si petites choses me fait savoir que je n'en sais pas assez et je stresse... Du coup j'ai valgrind et voilà le résultat:
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
 
==5543== Memcheck, a memory error detector
==5543== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==5543== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==5543== Command: ./a.out
==5543== 
==5543== Invalid write of size 4
==5543==    at 0x804847E: create_string (in /home/lechienkitu/C/Test/a.out)
==5543==    by 0x80484D8: main (in /home/lechienkitu/C/Test/a.out)
==5543==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==5543== 
==5543== 
==5543== Process terminating with default action of signal 11 (SIGSEGV)
==5543==  Access not within mapped region at address 0x0
==5543==    at 0x804847E: create_string (in /home/lechienkitu/C/Test/a.out)
==5543==    by 0x80484D8: main (in /home/lechienkitu/C/Test/a.out)
==5543==  If you believe this happened as a result of a stack
==5543==  overflow in your program's main thread (unlikely but
==5543==  possible), you can try to increase the size of the
==5543==  main thread stack using the --main-stacksize= flag.
==5543==  The main thread stack size used in this run was 8388608.
==5543== 
==5543== HEAP SUMMARY:
==5543==     in use at exit: 6 bytes in 1 blocks
==5543==   total heap usage: 1 allocs, 0 frees, 6 bytes allocated
==5543== 
==5543== LEAK SUMMARY:
==5543==    definitely lost: 6 bytes in 1 blocks
==5543==    indirectly lost: 0 bytes in 0 blocks
==5543==      possibly lost: 0 bytes in 0 blocks
==5543==    still reachable: 0 bytes in 0 blocks
==5543==         suppressed: 0 bytes in 0 blocks
==5543== Rerun with --leak-check=full to see details of leaked memory
==5543== 
==5543== For counts of detected and suppressed errors, rerun with: -v
==5543== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Erreur de segmentation (core dumped)
Donc je vois bien que mon erreur est au niveau de mon strcpy(), mais en quoi je foire ?

Merci à vous.