2

Im getting this error, what could this be???

Program received signal EXC_BAD_ACCESS, Could not access memory. Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000000 0x00007fff86703c00 in strlen ()

I RAN THIS IN GDB and its the first thing that appears. It doesnt even tell me on what Line I can find this error...

On the terminal I get this error: Segmentation Fault

Im positive the error is in the AREA "Funcion Busqueda" Im pasting the whole thing so you can tell me if you spot something else.

I need to find what is the cause for this Segmentation Fault!!! What could it beeeee!?

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


int ordena(LISTA1 *inicio,LISTA1 *aux,LISTA1 *nodo,LISTA1 *aux2);
int agrega_cliente(LISTA1 *inicio, LISTA1*aux,LISTA1 *nodo,LISTA1 *aux2);
int busca_cliente(LISTA1 *inicio, LISTA1*aux,LISTA1 *nodo,LISTA1 *aux2,LISTA2    *inicioventas,LISTA2 *auxventas,LISTA2 *nodoventas,LISTA2 *aux2ventas);
int main(void)
{
   int menu,pops=0;
LISTA1 *inicio, *aux,*nodo, *aux2;
inicio=NULL;
aux=inicio;
LISTA2 *inicioventas, *auxventas,*nodoventas, *aux2ventas;
inicioventas=NULL;
auxventas=inicioventas;
 system("clear");
  //funcion void carga_datos();
ordena(inicio,aux,nodo,aux2);

  printf("TIENDA DEPARTAMENTAL\n\n\n");
  printf("1. Agregar a un Cliente.\n2. Agregar una venta.\n3. Buscar Cliente. \n4. Borrar Cliente. \n5. Borrar una venta. \n6. Salir.");
  printf("\n\nElige una opcion: "); 
  scanf("%d", &menu);
  switch(menu)
    {
   case 1:
  system("clear");
  printf("opcion1\n");
        agrega_cliente(inicio,aux,nodo,aux2);

  break;
   case 2:
  system("clear");
  printf("opcion2\n");
  break;
    case 3:
  system("clear");
  printf("opcion3\n");

        busca_cliente(inicio,aux,nodo,aux2,inicioventas, auxventas, nodoventas, aux2ventas);
        puts(aux2->nombre);

   break;
  case 4:
  system("clear");
  printf("opcion4\n");
  break;
   case 5:
  system("clear");
  printf("opcion5\n");
  break;
   case 6:
  system("clear");
  printf("opcion6\n");
  break;
  }
 }

////////////////////////////////////////FUNCION BUSQUEDA//////////////////////////
 int busca_cliente(LISTA1 *inicio, LISTA1 *aux,LISTA1 *nodo,LISTA1 *aux2,LISTA2     *inicioventas,LISTA2 *auxventas,LISTA2 *nodoventas,LISTA2 *aux2ventas)
 {
char nombrebusqueda[80];
int numclienteprueba;
printf("Dame el nombre del cliente a buscar\n");
scanf("%s",nombrebusqueda);
aux2=aux; 
while(aux2!=NULL)
{ 
    if((strcmp(nombrebusqueda,aux2->nombre))==0)
    {
        puts(aux2->nombre);
        ////////Buscamos la venta
        aux2ventas=auxventas; 
        while(aux2ventas!=NULL)
        {
            numclienteprueba=aux2ventas->num_clienteventas;
            if(numclienteprueba==aux2ventas->num_clienteventas)
            {
                printf("Wow");
            }
            aux2ventas=aux2ventas->sigvent;
        }
        ////////////
    }
    aux2=aux2->sig;
  }
 }

   ////////////////////////////////////////////Funcion Ordena



 int ordena(LISTA1 *inicio, LISTA1*aux,LISTA1 *nodo,LISTA1 *aux2)
 {
char pruebaman[80],probaman[80];//nombres
char pruebadireccion[150],probadireccion[150];//direccion
int pruebanum_cliente,probanum_cliente;//numero de cliente
aux2=aux;
strcpy(pruebaman,aux2->nombre);
strcpy(pruebadireccion,aux2->direccion);
pruebanum_cliente=aux2->num_cliente;
aux2=aux2->ant;

do
 {

    strcpy(probaman,aux2->nombre);

    if(pruebaman[0]<probaman[0])
    {
        strcpy(pruebaman,aux2->nombre);
        strcpy(pruebadireccion,aux2->direccion);
        pruebanum_cliente=aux2->num_cliente;
        aux2=aux2->sig;
        strcpy(aux2->nombre,probaman); 
        strcpy(aux2->direccion,probadireccion);
        probanum_cliente=aux2->num_cliente;    
    }
    strcpy(pruebaman,aux2->nombre);
    strcpy(pruebadireccion,aux2->direccion);
    pruebanum_cliente=aux2->num_cliente;

    aux2=aux2->ant;


    printf("Ciclo\n\n");

  }
  while(aux2!=NULL);
 aux2=aux;
 strcpy(pruebaman,aux2->nombre);
 aux2=aux2->ant;
 strcpy(probaman,aux2->nombre);
 if(pruebaman[0]<probaman[0])
    ordena(inicio,aux,nodo,aux2);
}


 ///////////////////

  ///////////////////////////////////////////////////////Agrega Cliente
   int agrega_cliente(LISTA1 *inicio, LISTA1*aux,LISTA1 *nodo,LISTA1 *aux2)
 {

FILE *clientes;
clientes=fopen("clientes.txt", "a+");
//fseek(clientes, 0L, SEEK_END);

inicio=NULL;
nodo=malloc(sizeof(LISTA1));
aux=malloc(sizeof(LISTA1));

if(nodo==NULL)
{
    printf("No hay memoria\n");
    exit(1);
}
printf("NOMBRE: ");
gets(nodo->nombre);
printf("DIRECCION: ");
gets(nodo->direccion);
printf("NUMERO DE CLIENTE: ");
scanf("%d", &nodo->num_cliente);

if(inicio==NULL)
{
    inicio=nodo;
    inicio->sig=NULL;
    inicio->ant=NULL;
    aux=inicio;
}
else
{
    aux->sig =nodo;
    nodo->ant=aux;
    aux=nodo;
    nodo->sig=NULL;
}
nodo=malloc(sizeof(LISTA1));

aux2=inicio;

while(aux2!=NULL)
   {
    fprintf(clientes, "%s\n", aux2->nombre);
    fprintf(clientes, "%s\n", aux2->direccion);
    fprintf(clientes, "%d\n", aux2->num_cliente);
    puts(aux2->nombre);
    puts(aux2->direccion);
    aux2=aux2->sig;
   }


aux2=aux;
do
  {
    puts(aux2->nombre);
    aux2=aux2->ant;
  }
    while(aux2!=NULL);
fclose(clientes);
} 

/////////////////////////////////////////////////////
Matt
  • 22,721
  • 17
  • 71
  • 112
drodri420
  • 191
  • 2
  • 6
  • 14
  • 4
    Narrow-down the problem and don't shout at us please. – Blagovest Buyukliev Oct 14 '11 at 13:21
  • Hahaha Im not shouting Im just frustrated, Well Ill narrow the problem down to this area:"Funcion Busqueda" is the function I just added Im positive its got to do with the list and pointers. – drodri420 Oct 14 '11 at 13:22
  • Why not just run it in your debugger ? It will then break at the line where you have the bad memory access. – Paul R Oct 14 '11 at 13:26
  • I did and its the first thing that appears after I do run... – drodri420 Oct 14 '11 at 13:29
  • Nevermind there was a Function that was in the wrong place!!!!! Thank you evryone... – drodri420 Oct 14 '11 at 13:35
  • Could you be more specific about the problem was? What function was in the wrong place? I am getting an almost identical error, and like you, have almost nothing to go on... –  Nov 16 '11 at 17:09

1 Answers1

1

It looks like a NULL pointer. When you try to access memory location 0x0000 it means the pointer is pointing to 0x0000 (NULL).

Steve Wellens
  • 20,506
  • 2
  • 28
  • 69