Ir ao conteúdo
  • Cadastre-se

C++ está ocorrendo alguns erros no código


Duvidasassembly
Ir à solução Resolvido por AdrianoSiqueira,

Posts recomendados

Está aparecendo alguns erros porém não estou conseguindo arrumar, se alguém conseguir me ajudar, Grata. Segue Código.

 

# include <stdio.h>
# include <stdlib.h>
# include <stdbool.h>
# include <math.h>
# include <string.h>
#include <iostream>

# define  MAX  1024
# define  LOG2 ( valor ) ( sem sinal ) (log (valor) / log ( 2 ))

static  char memoria_principal [MAX] = " " ;

struct caminho
{
    bool bit_validade;
    tag int sem sinal;
    sem assinatura  int sujo;
    void * bloco;
};
typedef  struct caminho;

struct conjunto
{
    Caminho * caminho;
};
typedef  struct conjunto Conjunto;

struct cache
{
    Conjunto * conjuntos;
    unsigned  int size_bloco;
    unsigned  int num_linhas;
    unsigned  int num_conjuntos;
    unsigned  int qtd_caminho;
    int cache_miss;
    int cache_hit;
};
typedef  struct cache Cache;

Cache * cria_cache ( int num_linhas, int size_bloco, int qtd_caminho)
{
    int i, j;
    Cache * cache = (Cache *) malloc ( sizeof (Cache));
    cache-> num_linhas     = num_linhas;
    cache-> size_bloco     = size_bloco;
    cache-> qtd_caminho    = qtd_caminho;
    cache-> num_conjuntos = num_linhas / qtd_caminho;
    cache-> conjuntos      = (Conjunto *) malloc ( sizeof (Conjunto) * cache-> num_conjuntos );
    cache-> cache_miss     = 0 ;
    cache-> cache_hit      = 0 ;

    para (i = 0 ; i < cache-> num_conjuntos; i ++)
    {
        cache-> conjuntos [i]. caminho = (Caminho *) malloc ( sizeof (Caminho) * qtd_caminho);
        para (j = 0 ; j < qtd_caminho; j ++)
        {
            // cache-> conjuntos [i] .caminho [j] .bloco = malloc (sizeof (char) * size_bloco);
            cache-> conjuntos [i]. caminho [j]. bloco          = calloc (cache-> size_bloco , sizeof ( char ));
            cache-> conjuntos [i]. caminho [j]. bit_validade   = falso ;
            cache-> conjuntos [i]. caminho [j]. tag            = 0 ;
            cache-> conjuntos [i]. caminho [j]. sujo           = 0 ;
        }
    }

    cache de retorno ;
}

void  imprime_cache (Cache * cache)
{
    int i = 0 ,
        j = 0 ,
        k = 0 ;

    para (i = 0 ; i < cache-> num_conjuntos ; i ++)
    {
        printf ( " --------------------------- Conjunto % d ----------------- ---------- \ n " , i);
        para (j = 0 ; j < cache-> qtd_caminho ; j ++)
        {
            printf ( " caminho % d : \ n " , j);
            para (k = 0 ; k < cache-> size_bloco ; k ++)
            {
                printf ( " | % c | " , (( char *) cache-> conjuntos [i]. caminho [j]. bloco ) [k]);
            }
            printf ( " \ n " );
        }
        printf ( " ----------------------------------------------- ------------------- \ n " );
    }

}

int  conjuto_cheio (Cache * cache, unsigned  int indice_conjunto)
{
    int i;
    para (i = 0 ; i < cache-> qtd_caminho ; i ++)
    {
        if (cache-> conjuntos [indice_conjunto]. caminho [i]. bit_validade == false )
        {
            return i;
        }
    }
    retorno - 1 ;
}

int  busca_conjunto (Cache * cache, tag int , unsigned  int indice_conjunto)
{
    int i;
    para (i = 0 ; i < cache-> qtd_caminho ; i ++)
    {
        if (cache-> conjuntos [indice_conjunto]. caminho [i]. tag == tag && cache-> conjuntos [indice_conjunto]. caminho [i]. bit_validade )
        {
            return i;
        }
    }


    retorno - 1 ;
}

void  comum_cache (Cache * cache, unsigned  int indice_caminho, unsigned  int indice_conjunto, unsigned  int tag, unsigned  int indice_bloco, unsigned  int endereco)
{
    int i;
    para (i = 0 ; i < cache-> size_bloco ; i ++)
    {
        if ((i + indice_bloco) > cache-> size_bloco || (i + endereco) > MAX)
        {
            pausa ;
        }
        (( char *) cache-> conjuntos [índice_conjunto]. caminho [índice_caminho]. bloco ) [indice_bloco + i] = memoria_principal [endereco + i];
    }
    cache-> conjuntos [indice_conjunto]. caminho [indice_caminho]. tag = tag;
    cache-> conjuntos [indice_conjunto]. caminho [indice_caminho]. bit_validade = verdadeiro ;
    cache-> conjuntos [indice_conjunto]. caminho [indice_caminho]. sujo ++;
}

int  FIFO (Cache * cache, unsigned  int indice_conjunto)
{
    int i, sujo_aux = 0 ;
    para (i = 0 ; i < cache-> qtd_caminho ; i ++)
    {
        if (cache-> conjuntos [índice_conjunto]. caminho [i]. sujo <sujo_aux> cache-> conjuntos [índice_conjunto]. caminho [sujo_aux]. sujo )
        {
            sujo_aux = i;
        }
    }
    return sujo_aux;
}

char  le_cache ( unsigned  int endereco, Cache * cache)
{
    sem sinal  int numBit_bloco = LOG2 (cache-> size_bloco );
    sem sinal  int numBit_conjunto = LOG2 (cache-> num_conjuntos );
    sem sinal  int numBit_tag = 32 - (numBit_bloco + numBit_conjunto);
    unsigned  int indice_caminho = 0 ;  // primeiro caminho com NULL;

    unsigned  int indice_bloco = (cache-> size_bloco - 1 ) & endereco; // numero do bloco que eu vou acessar em alguma linha.

    unsigned  int indice_conjunto = (((cache-> num_conjuntos - 1 ) << numBit_bloco) & endereco) >> numBit_bloco;

    tag int sem sinal = ((((( int sem sinal  )) ( pow ( 2 , numBit_tag) - 1 )) << (numBit_bloco + numBit_conjunto)) & endereco)) >> (numBit_bloco + numBit_conjunto);

    / * printf ("% d \ n", indice_bloco);
    printf ("% d \ n", indice_linha);
    printf ("% d \ n", tag); * /
    indice_caminho = busca_conjunto (cache, tag, indice_conjunto);
    // printf ("% d \ n", indice_caminho);
    if (indice_caminho! = - 1 )
{
    cache-> cache_hit ++;
}
else
{
    cache-> cache_miss ++;
    indice_caminho = conjuto_cheio (cache, indice_conjunto); // retorna primeiro caminho livre

        if (indice_caminho! = - 1 )
        {

            pode_cache (cache, indice_caminho, indice_conjunto, tag, indice_bloco, endereco);
        }
        else
        {
            indice_caminho = FIFO (cache, indice_conjunto);
            pode_cache (cache, indice_caminho, indice_conjunto, tag, indice_bloco, endereco);
        }


    }

    return (( char *) cache-> conjuntos [indice_conjunto]. caminho [indice_caminho]. bloco ) [indice_bloco];
}

int  main ()
{
    Cache * cache = cria_cache ( 16 , 4 , 4 ); // linha, bloco tam, qtd caminho
    imprime_cache (cache);


    strcat (memoria_principal, " Arquitetura de computadores " );

    para ( int i = 0 ; i < strlen (memoria_principal); i ++)
    {
        printf ( " % c " , le_cache (i, cache));
    }

    printf ( " \ n \ n \ n ESCREVENDO NA CACHE ...... \ n \ n \ n " );

    printf ( " \ n Cache perdido: % d \ n Cache atingido: % d \ n " , cache-> cache_miss , cache-> cache_hit );
    imprime_cache (cache);

    return  0;
}
}

  • Curtir 1
Link para o comentário
Compartilhar em outros sites

  • Solução

Seu código tem uma série de erros, você tentou "traduzir" ele de alguma outra linguagem? Aplicada as correções de sintaxe, ele ficou assim:

Spoiler

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <string.h>

#define MAX 1024
#define LOG2(valor) (unsigned)(log(valor) / log( 2 ))


static char memoria_principal[MAX] = " ";


typedef struct caminho {
    bool bit_validade;
    unsigned int tag;
    unsigned int sujo;
    void *bloco;
} Caminho;

typedef struct conjunto {
    Caminho *caminho;
} Conjunto;

typedef struct cache {
    Conjunto *conjuntos;
    unsigned int size_bloco;
    unsigned int num_linhas;
    unsigned int num_conjuntos;
    unsigned int qtd_caminho;
    int cache_miss;
    int cache_hit;
} Cache;


int busca_conjunto(Cache *cache, int tag, unsigned int indice_conjunto);

void comum_cache(Cache *cache, unsigned int indice_caminho, unsigned int indice_conjunto, unsigned int tag, unsigned int indice_bloco, unsigned int endereco);

int conjuto_cheio(Cache *cache, unsigned int indice_conjunto);

Cache *cria_cache(int num_linhas, int size_bloco, int qtd_caminho);

void imprime_cache(Cache *cache);

int FIFO(Cache *cache, unsigned int indice_conjunto);

char le_cache(unsigned int endereco, Cache *cache);

int main() {
    Cache *cache = cria_cache(16, 4, 4); // linha, bloco tam, qtd caminho
    imprime_cache(cache);
    
    strcat(memoria_principal, " Arquitetura de computadores ");

    for (int i = 0; i < strlen(memoria_principal); i++) {
        printf(" %c ", le_cache(i, cache));
    }

    printf("\n\n\n ESCREVENDO NA CACHE ...... \n\n\n");
    printf("\n Cache perdido: %d \n Cache atingido: %d \n", cache->cache_miss, cache->cache_hit);
    imprime_cache(cache);

    return 0;
}


int busca_conjunto(Cache *cache, int tag, unsigned int indice_conjunto) {
    for (int i = 0; i < cache->qtd_caminho; i++) {
        if (cache->conjuntos[indice_conjunto].caminho[i].tag == tag && cache->conjuntos[indice_conjunto].caminho[i].bit_validade) {
            return i;
        }
    }

    return -1;
}

void comum_cache(Cache *cache, unsigned int indice_caminho, unsigned int indice_conjunto, unsigned int tag, unsigned int indice_bloco, unsigned int endereco) {
    for (int i = 0; i < cache->size_bloco; i++) {
        if ((i + indice_bloco) > cache->size_bloco || (i + endereco) > MAX) {
            break;
        }

        ((char *) cache->conjuntos[indice_conjunto].caminho[indice_caminho].bloco)[indice_bloco + i] = memoria_principal[endereco + i];
    }

    cache->conjuntos[indice_conjunto].caminho[indice_caminho].tag = tag;
    cache->conjuntos[indice_conjunto].caminho[indice_caminho].bit_validade = true;
    cache->conjuntos[indice_conjunto].caminho[indice_caminho].sujo++;
}

int conjuto_cheio(Cache *cache, unsigned int indice_conjunto) {
    for (int i = 0; i < cache->qtd_caminho; i++) {
        if (cache->conjuntos[indice_conjunto].caminho[i].bit_validade == false) {
            return i;
        }
    }

    return -1;
}

Cache *cria_cache(int num_linhas, int size_bloco, int qtd_caminho) {
    Cache *cache = (Cache *) malloc(sizeof(Cache));
    cache->num_linhas = num_linhas;
    cache->size_bloco = size_bloco;
    cache->qtd_caminho = qtd_caminho;
    cache->num_conjuntos = num_linhas / qtd_caminho;
    cache->conjuntos = (Conjunto *) malloc(sizeof(Conjunto) * cache->num_conjuntos);
    cache->cache_miss = 0;
    cache->cache_hit = 0;

    for (int i = 0; i < cache->num_conjuntos; i++) {
        cache->conjuntos[i].caminho = (Caminho *) malloc(sizeof(Caminho) * qtd_caminho);
        for (int j = 0; j < qtd_caminho; j++) {
            // cache-> conjuntos [i] .caminho [j] .bloco = malloc (sizeof (char) * size_bloco);
            cache->conjuntos[i].caminho[j].bloco = calloc(cache->size_bloco, sizeof(char));
            cache->conjuntos[i].caminho[j].bit_validade = false;
            cache->conjuntos[i].caminho[j].tag = 0;
            cache->conjuntos[i].caminho[j].sujo = 0;
        }
    }

    return cache;
}

void imprime_cache(Cache *cache) {
    for (int i = 0; i < cache->num_conjuntos; i++) {
        printf(" --------------------------- Conjunto %d ----------------- ---------- \n ", i);

        for (int j = 0; j < cache->qtd_caminho; j++) {
            printf(" caminho %d : \n ", j);

            for (int k = 0; k < cache->size_bloco; k++) {
                printf(" | %c | ", ((char *) cache->conjuntos[i].caminho[j].bloco)[k]);
            }

            printf(" \n ");
        }

        printf(" ----------------------------------------------- ------------------- \n ");
    }
}

int FIFO(Cache *cache, unsigned int indice_conjunto) {
    int sujo_aux = 0;

    for (int i = 0; i < cache->qtd_caminho; i++) {
        if (cache->conjuntos[indice_conjunto].caminho[i].sujo < sujo_aux > cache->conjuntos[indice_conjunto].caminho[sujo_aux].sujo) {
            sujo_aux = i;
        }
    }

    return sujo_aux;
}

char le_cache(unsigned int endereco, Cache *cache) {
    unsigned int numBit_bloco = LOG2(cache->size_bloco);
    unsigned int numBit_conjunto = LOG2(cache->num_conjuntos);
    unsigned int numBit_tag = 32 - (numBit_bloco + numBit_conjunto);
    unsigned int indice_caminho = 0;  // primeiro caminho com NULL;
    unsigned int indice_bloco = (cache->size_bloco - 1) & endereco; // numero do bloco que eu vou acessar em alguma linha.
    unsigned int indice_conjunto = (((cache->num_conjuntos - 1) << numBit_bloco) & endereco) >> numBit_bloco;
    unsigned int tag = ((unsigned int) (pow(2, numBit_tag) - 1)) << ((numBit_bloco + numBit_conjunto) & endereco) >> (numBit_bloco + numBit_conjunto);


    printf("%d \n", indice_bloco);
//    printf("%d \n", indice_linha);
    printf("%d \n", tag);

    indice_caminho = busca_conjunto(cache, (int) tag, indice_conjunto);
    printf("%d \n", indice_caminho);

    if (indice_caminho != -1) {
        cache->cache_hit++;
    } else {
        cache->cache_miss++;
        indice_caminho = conjuto_cheio(cache, indice_conjunto); // retorna primeiro caminho livre

        if (indice_caminho != -1) {
            comum_cache(cache, indice_caminho, indice_conjunto, tag, indice_bloco, endereco);
        } else {
            indice_caminho = FIFO(cache, indice_conjunto);
            comum_cache(cache, indice_caminho, indice_conjunto, tag, indice_bloco, endereco);
        }
    }

    return ((char *) cache->conjuntos[indice_conjunto].caminho[indice_caminho].bloco)[indice_bloco];
}

 

 

Link para o comentário
Compartilhar em outros sites

Crie uma conta ou entre para comentar

Você precisa ser um usuário para fazer um comentário

Criar uma conta

Crie uma nova conta em nossa comunidade. É fácil!

Crie uma nova conta

Entrar

Já tem uma conta? Faça o login.

Entrar agora

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...