[C con Clase] violacion de un acceso a memoria(fallo en segmentacion)

German Ponce german.informatico en gmail.com
Dom Sep 2 19:14:45 CEST 2007


holas a todos estoy estudiando los algoritmos de ordenamiento y me en
çcontre con el algoritmo bucketsort con codigo en c++ y todo, mi consulta es
que al querer complilar me aparece me arroja errores  y al depurar me
aparece el siguinete mensaje" violacion de acceso a memoria (fallo de
segmentacion) ocurrio en tu programa" ¿ me prodrian ayudar asolucionar este
problemas,

hasta ahora no le encuentro nada de raro , pero como dato en la depuracion
me arroja que el problema  esta en  if((temp->value) > array[j])  mas abajo
lo deje resaltado para que lo ubiquen, les doy las gracias por cualquier
ayuda que me puedan dar

#include <iostream>
#include <stdio.h>
#include <conio.h>

using namespace std;

class element            //elementos
{
public:
    int value;
    element *next;
    element()
    {
    value=NULL;
    next=NULL;
    }
};

class bucket           //cada bucket tiene un rango especifico de valores

{
public:
element *firstElement;
bucket()
{
firstElement = NULL;
}
};

int main()
{
    int lowend=0;         // minimo elemento
    int highend=100;      //max elemento
    int interval=10;      //numero de intervalos
    const int noBuckets=(highend-lowend)/interval; //calculo de numeros de
buquet requeridos
    bucket *buckets=new bucket[noBuckets];
    bucket *temp;

    for(int a=0;a<noBuckets;a++)   //creando cada uno de los bucket
    {
        temp=new bucket;
        buckets[a]=*temp;
    }

    cout<<"--------The Elements to be Sorted using Bucket sort are
------------------\n";
    int array[]={12,2,22,33,44,55,66,77,85,87,81,83,89,82,88,86,84,88,99};

    for(int j=0;j<19;j++)   //enviando elementos al bucket apropiado
    {
    cout<<array[j]<<endl;
    element *temp,*pre;
    temp=buckets[array[j]/interval].firstElement;
        if(temp==NULL)//si el elemto es el primero en el bucket
        {
            temp=new element;
            buckets[array[j]/interval].firstElement=temp;
            temp->value=array[j];
        }
        else
        {
            pre=NULL;
                while(temp!=NULL)     //al salir de este while queda
apuntando al lugar apropaido dentro del bucket
                   {
               if(temp->value>array[j])
                   break;// sale de s cilo si el elemento es menro que el
apuntado
                   pre=temp;
                   temp=temp->next;
                   }
                if((temp->value) > array[j]) //   if the new value is in
betwen or at the begining****AQUI ME ARROJA PROBLEMAS
                {
                    if(pre==NULL)  //inserta al principio si el bucket tiene
elementos ya
                    {
                        element *firstNode;
                        firstNode=new element();
                        firstNode->value=array[j];
                        firstNode->next=temp;
                        buckets[array[j]/interval].firstElement=firstNode;
                    }
                    else  //insercion a la mitad
                    {
                        element *firstNode;
                        firstNode=new element();
                        firstNode->value=array[j];
                        firstNode->next=temp;
                        pre->next=firstNode;
                    }
                }
                else// si el valor creado en el ultimo en el bucket
                {
                    temp=new element;
                    pre->next=temp;
                    temp->value=array[j];
                }

        }
 }

    cout<<"------------------------The Sorted Elements
Are---------------\n";
    for(int jk=0;jk<10;jk++)
    {
        element *temp;
        temp= buckets[jk].firstElement;
            while(temp!=NULL)
            {
                cout<<"*"<<temp->value<<endl;
                temp=temp->next;
            }
    }

cout<<"--------------------------------END--------------------------------\n";

system("pause");

return 1;

}


-- 
(\_/) *̡͌l̡*̡̡ ̴̡ı̴̴̡ ̡̡͡|̲̲̲͡͡͡ ̲▫̲͡ ̲̲̲͡͡π̲̲͡͡ ̲̲͡▫̲̲͡͡ ̲|̡̡̡ ̡ ̴̡ı̴̡̡
*̡͌l̡*
(O.o) Geman Ponce
(> <) Http://german2006.pandela.net
        "Es mejor que te odien por quien eres a que te amen por quien no
eres."
------------ próxima parte ------------
Se ha borrado un adjunto en formato HTML...
URL: <http://listas.conclase.net/pipermail/cconclase_listas.conclase.net/attachments/20070902/3dd7474e/attachment.html>


Más información sobre la lista de distribución Cconclase