• STATISTIQUES
  • Il y a eu un total de 1 membres et 6260 visiteurs sur le site dans les dernières 24h pour un total de 6 261 personnes!


    1 membre s'est inscrit dans les dernières 24h!


    Membres: 2 606
    Discussions: 3 580
    Messages: 32 820
    Tutoriels: 78
    Téléchargements: 38
    Sites dans l'annuaire: 58


  • ANNUAIRE
  • [EN] Gekko
    Site de challenge présenter sous la forme d'une quête. Vous êtes un agent secret qui répond sous le nom...
    Challenges
    [FR] apprendre-a-manipuler
    Site d'apprentissage de la manipulation d'autrui.
    Hacking
    [FR] InfoMirmo
    Apprentissage de l'informatique par l'intermédiaire de challenges de sécurité. Venez app...
    Hacking
    [FR] PHP Débutant
    Apprendre le PHP par l'exemple, facilement et simplement. Réservé d'abord aux débutants....
    Programmation
    [EN] Listbrain Version 3
    Site proposant 66 challenges présentés dans une liste mélangée.
    Challenges
    [EN] Astalavista
    JavaScript: 1, Exploit: 2, Crypto: 34, CrackIt: 15, Stegano: 8, Programming: 12, Logic: 36, Special: 6, Science: 4, Info...
    Challenges
    [FR] Cyber-Hacker
    CH - Cyber Hacker est un jeu par navigateur de simulation de hack, programmez et envoyez vos virus et piratez les aut...
    Hacking

  • DONATION
  • Si vous avez trouvé ce site internet utile, nous vous invitons à nous faire un don du montant de votre choix via Paypal. Ce don servira à financer notre hébergement.

    MERCI!




Note de ce sujet :
  • Moyenne : 0 (0 vote(s))
  • 1
  • 2
  • 3
  • 4
  • 5
[C] SegFault > 6
06-01-2013, 15h32
Message : #1
Sh4dows Hors ligne
Tweetos
*



Messages : 293
Sujets : 5
Points: 49
Inscription : Dec 2012
[C] SegFault > 6
Bonjour,
J'ai une fonction qui doit prendre le premier élément d'un tableau A et le mettre dans le second B, (A[0] dans B[0]). Or j'ai un "segfault" ou un "glibc free.." dès lors que mon tableau A dépasse 6 en taille. Mes deux tableaux sont des tableaux de int.

Pour information c'est un de mes projets d'école, je ne comprend juste pas d'ou provient mon erreur dans cette fonction..

Exemple :
Code :
A = 1 2 3 4 5
B =

Sortie de ma fonction..

A = 2 3 4 5
B = 1

Ensuite voici la fonction qui pose problème :
Code C :

void    take_b(struct s_data *vars)
{
  int   i;
  int   *temp_a;
  int   *temp_b;

  if (vars->size_a >= 1)
    {
      temp_b = backup(vars->tab_b, vars->size_b);
      vars->size_b += 1;
      free(vars->tab_b);
      if ((vars->tab_b = malloc(sizeof(int) * vars->size_b)) != NULL)
        {
          i = 1;
          vars->tab_b[i - 1] = vars->tab_a[i - 1];
          temp_a = backup(vars->tab_a, vars->size_a);
          while (i < vars->size_b)
            {
              vars->tab_b[i] = temp_b[i - 1];
              i += 1;
            }
          i = 0;
          vars->size_a -= 1;
          free(vars->tab_a);
          vars->tab_a = malloc(sizeof(int) * vars->size_a);
          while (i < vars->size_a + 1)
            {
              vars->tab_a[i] = temp_a[i + 1];
              i += 1;
            }
          free(temp_b);
          free(temp_a);
        }
    }
}
 


Mon push_swap.h :
Code C :

#ifndef _PUSH_SWAP_
#define _PUSH_SWAP_

struct  s_data
{
  int   size_a;
  int   size_b;
  int   *tab_a;
  int   *tab_b;
};

#endif
 


Et mon main.c avec main() et backup() !
Code C :

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

int                     main(int argc, char *argv[])
{
  int                   i;
  struct s_data         *vars;

  if (argc < 2)
    return (0);
  if ((vars = malloc(sizeof(struct s_data))) == NULL)
    return (0);
  vars->size_a = argc - 1;
  vars->size_b = 0;
  if ((vars->tab_a = malloc(sizeof(int) * vars->size_a)) == NULL)
    return (0);
  if ((vars->tab_b = malloc(sizeof(int) * vars->size_b)) == NULL)
    return (0);
  i = 0;
  while (i < argc - 1)
    {
      vars->tab_a[i] = atoi(argv[i + 1]);
      i += 1;
    }
 
  /* JUST FOR TEST MY FUNC */
  i = 0;
  while (i < 1)
  {
      take_b(vars);
      i += 1;
  }

  printf("\n");
  free(vars->tab_a);
  free(vars->tab_b);
  free(vars);
  return (0);
}

int                     *backup(int *tab, int sz)
{
  int                   i;
  int                   *new;

  if ((new = malloc(sizeof(int) * sz)) != NULL)
    {
      i = 0;
      while (i < sz)
        {
          new[i] = tab[i];
          i += 1;
        }
      return (new);
    }
  return (tab);
}
 


Je récupère tous les arguments passé au programme, puis je les transforme en int via atoi() (man pour plus d'infos) et les stocke dans mon tableau présent dans ma structure. Dans cette fameuse structure, il y a tableau A et B, ainsi que deux variables représentant leur taille.

La fonction backup fais une sauvegarde d'un tableau !!

Donc en en appellant mon programme comme ceci :
./a.out 1 2 3 4 Tous fonctionne.
Mais en faisant ceci ./a.out 1 2 3 4 5 6 7 J'ai une des erreurs cité au début Huh

Si vous voyez pourquoi je segfault ??
Merci d'avance Tongue
Faites ce que je dis et non ce que je fais !
+1 (0) -1 (0) Répondre
06-01-2013, 15h38
Message : #2
ark Hors ligne
Psyckomodo!
*****



Messages : 1,033
Sujets : 48
Points: 317
Inscription : Sep 2011
RE: [C] SegFault > 6
Les AERs sont la pour ça ! (Et au passage t'es pas a la norme :3)

Bref, anyway, use gdb pour donner plus d'information (compile en -g3 d'abord (et pense a l'enlever avant le rendu...))

Et sinon, go use des listes doublement chaînées circulaires Big Grin
+1 (0) -1 (0) Répondre
06-01-2013, 15h43 (Modification du message : 06-01-2013, 15h45 par Sh4dows.)
Message : #3
Sh4dows Hors ligne
Tweetos
*



Messages : 293
Sujets : 5
Points: 49
Inscription : Dec 2012
RE: [C] SegFault > 6
*** glibc detected *** /home/XXX/Documents/Projets/PushSwap/a.out: free(): invalid next size (fast): 0x00000000006030d0 ***
a.out: malloc.c:3096: 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.

Program received signal SIGABRT, Aborted.
0x00007ffff7a80d95 in raise () from /lib64/libc.so.6
Faites ce que je dis et non ce que je fais !
+1 (0) -1 (0) Répondre
06-01-2013, 15h47
Message : #4
ark Hors ligne
Psyckomodo!
*****



Messages : 1,033
Sujets : 48
Points: 317
Inscription : Sep 2011
RE: [C] SegFault > 6
hmm, tape bt dans gdb après qu'il ai plante et poste le résultat.
+1 (1) -1 (0) Répondre
06-01-2013, 15h50
Message : #5
Sh4dows Hors ligne
Tweetos
*



Messages : 293
Sujets : 5
Points: 49
Inscription : Dec 2012
RE: [C] SegFault > 6
#0 0x00007ffff7a80d95 in raise () from /lib64/libc.so.6
#1 0x00007ffff7a822ab in abort () from /lib64/libc.so.6
#2 0x00007ffff7ac1d3d in __malloc_assert () from /lib64/libc.so.6
#3 0x00007ffff7ac4c75 in _int_malloc () from /lib64/libc.so.6
#4 0x00007ffff7ac79ae in calloc () from /lib64/libc.so.6
#5 0x00007ffff7de6a2e in _dl_new_object () from /lib64/ld-linux-x86-64.so.2
#6 0x00007ffff7de21d6 in _dl_map_object_from_fd () from /lib64/ld-linux-x86-64.so.2
#7 0x00007ffff7de3fd7 in _dl_map_object () from /lib64/ld-linux-x86-64.so.2
#8 0x00007ffff7dedc2b in dl_open_worker () from /lib64/ld-linux-x86-64.so.2
#9 0x00007ffff7de9c36 in _dl_catch_error () from /lib64/ld-linux-x86-64.so.2
#10 0x00007ffff7ded7ca in _dl_open () from /lib64/ld-linux-x86-64.so.2
#11 0x00007ffff7b5e410 in do_dlopen () from /lib64/libc.so.6
#12 0x00007ffff7de9c36 in _dl_catch_error () from /lib64/ld-linux-x86-64.so.2
#13 0x00007ffff7b5e4af in dlerror_run () from /lib64/libc.so.6
#14 0x00007ffff7b5e517 in __libc_dlopen_mode () from /lib64/libc.so.6
#15 0x00007ffff7b398e5 in init () from /lib64/libc.so.6
#16 0x00007ffff7b39a2d in backtrace () from /lib64/libc.so.6
#17 0x00007ffff7abc9af in __libc_message () from /lib64/libc.so.6
#18 0x00007ffff7ac26d6 in malloc_printerr () from /lib64/libc.so.6
#19 0x00000000004016eb in take_b (vars=0x603010) at take.c:79
#20 0x00000000004009d5 in main (argc=9, argv=0x7fffffffde18) at push_swap.c:89
Faites ce que je dis et non ce que je fais !
+1 (0) -1 (0) Répondre
06-01-2013, 22h15 (Modification du message : 06-01-2013, 22h24 par Sh4dows.)
Message : #6
Sh4dows Hors ligne
Tweetos
*



Messages : 293
Sujets : 5
Points: 49
Inscription : Dec 2012
RE: [C] SegFault > 6
J'ai mis pas mal de temps, mais tous fonctionne Smile Merci à @Ark, j'ai appris pas mal d'astuces avec GDB.
C'était bien la fonction take_b() au dernier while qui produisait l'Erreur de Segmentation !!

Code C :

void    take_b(struct s_data *vars)
{
  int   i;
  int   *tmp_b;

  if (vars->size_a >= 1)
    {
      tmp_b = backup(vars->tab_b, vars->size_b);
      vars->size_b += 1;
      free(vars->tab_b);
      vars->tab_b = malloc(sizeof(int) * vars->size_b);
      i = 1;
      vars->tab_b[0] = vars->tab_a[0];
      if (vars->size_b > 1)
        {
          while (i < vars->size_b)
            {
              vars->tab_b[i] = tmp_b[i - 1];
              i += 1;
            }
        }
      free(tmp_b);
      take_b_ext(vars);
    }
}

void    take_b_ext(struct s_data *vars)
{
  int   i;
  int   *tmp_a;

  tmp_a = backup(vars->tab_a, vars->size_a);
  free(vars->tab_a);
  vars->tab_a = malloc(sizeof(int) * vars->size_a);
  i = 0;
  while (i + 1 < vars->size_a)
    {
      vars->tab_a[i] = tmp_a[i + 1];
      i += 1;
    }
  vars->size_a -= 1;
  free(tmp_a);
}
 


PS : Pensez à checker les mallocs Smile
Faites ce que je dis et non ce que je fais !
+1 (1) -1 (0) Répondre


Atteindre :


Utilisateur(s) parcourant ce sujet : 1 visiteur(s)
N-PN
Accueil | Challenges | Tutoriels | Téléchargements | Forum | Retourner en haut