Jump to content
Fórum Script Brasil
  • 0

"stack Smashing Detected"


Inufelipe
 Share

Question

Ola galera do Script Brasil Forum.

Eu estava "inocentemente" escrevendo

um joguinho de um plantador

de maças...

...quando derepente aparece um

"stack smashing detected", esse

erro é novo ainda para mim, o

que ele faz ?

E como resolvelo ?

Se quiserem dar uma olhada no

codigo:

Apples.cpp

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

class Planter{
    private:
        int apple,money,area,quality,appletree;
    public:
        void take(){
            if(money > (area+appletree)-1){
            srand(time(NULL));
            apple = appletree*(rand()%10+1);
            money -= area+appletree;
            srand(time(NULL));
            quality = rand()%3+1;
            printf("Voce colheu %d macas.",apple);
            }
            else{
                printf("Voce não tem dinheiro suficiente.");
            }
            getchar();
            getchar();
            printf("\n\n");
        }
        void sell(){
            int price;
            bool isgood;
            bool ok;
            ok = 0;
            srand(time(NULL));
            printf("Por quanto voce vendera a unidade da maca (1-3) ?\n\n");
            scanf("%d",&price);
            switch(price){
            case 1:
                ok = 1;
                break;
            case 2:
                if(quality == 2){
                    isgood = rand()%2;
                    if(isgood == 1){
                        ok = 1;
                    }
                }
                break;
            case 3:
                if(quality == 3){
                    isgood = rand()%2;
                    if(isgood == 1){
                        ok = 1;
                    }
                }
                break;
            }
            if(ok == 1){
                    money += apple*price;
                    printf("Voce ganhou %d reais.",apple*price);
                    apple = 0;
            }
            else{
                printf("Esta muito caro, suas macas apodreceram.");
                apple = 0;
            }
            getchar();
            getchar();
            printf("\n\n");
        }
        void test(){
            apple--;
            switch(quality){
                case 1:
                    printf("Esta boa");
                    break;
                case 2:
                    printf("Esta otima");
                    break;
                case 3:
                    printf("Esta deliciosa");
                    break;
            }
            getchar();
            getchar();
            printf("\n\n");
        }
        void plant(){
            bool ok;
            ok = 0;
            if(area > appletree){
                if(apple > 0){
                    if(money > 1){
                        ok = 1;
                    }
                }
            }
            if(ok == 1){
                apple--;
                money-=2;
                appletree++;
                printf("Voce tem mais uma macieira\n\n");
            }
            else{
                printf("Para plantar mais uma macieira voce precisa de 1 m2 de espaco sobrando, 2 reais e uma maca, voce não tem isso.\n\n");
            }
            getchar();
            getchar();
            printf("\n\n");
        }
        void buy(){
            if(money > 4){
                money -= 5;
                area += 1;
                printf("Voce comprou mais um metro quadrado.");
            }
            else{
                printf("Voce não tem dinheiro.");
            }
            getchar();
            getchar();
            printf("\n\n");
        }
        int s2i(char *data){
            int result = 0;
            int temp;
            while(*data <= '9' && *data >= '0'){
                switch (*data){
                    case '0':
                        temp = 0;
                        data++;
                        break;
                    case '1':
                        temp = 1;
                        data++;
                        break;
                    case '2':
                        temp = 2;
                        data++;
                        break;
                    case '3':
                        temp = 3;
                        data++;
                        break;
                    case '4':
                        temp = 4;
                        data++;
                        break;
                    case '5':
                        temp = 5;
                        data++;
                        break;
                    case '6':
                        temp = 6;
                        data++;
                        break;
                    case '7':
                        temp = 7;
                        data++;
                        break;
                    case '8':
                        temp = 8;
                        data++;
                        break;
                    case '9':
                        temp = 9;
                        data++;
                        break;
                }
                result = (result*10)+temp;
            }
            return result;
        }
        void load(){
            FILE *f;
            char content[256];
            int i;
            char tmp[20];
            int j;
            if(f = fopen("data.txt","r")){
            fscanf(f,"%s",&content);
            i = 0;
            while(content[i] != ''){
                if(content[i] == 'A'){
                    j = 1;
                    while(1){
                        if(content[(i+j)] != 'A' && content[(i+j)] != 'M' && content[(i+j)] != 'R' && content[(i+j)] != 'que' && content[(i+j)] != 'T'){
                        tmp[(j-1)] = content[(i+j)];
                        j++;
                        }
                        else{
                            tmp[(j-1)] = '';
                            break;
                        }
                    }
                    apple = s2i(tmp);
                    i+= j;
                }
                if(content[i] == 'M'){
                    j = 1;
                    while(1){
                        if(content[(i+j)] != 'A' && content[(i+j)] != 'M' && content[(i+j)] != 'R' && content[(i+j)] != 'que' && content[(i+j)] != 'T'){
                        tmp[(j-1)] = content[(i+j)];
                        j++;
                        }
                        else{
                            tmp[(j-1)] = '';
                            break;
                        }
                    }
                    money = s2i(tmp);
                    i+= j;
                }
                if(content[i] == 'R'){
                    j = 1;
                    while(1){
                        if(content[(i+j)] != 'A' && content[(i+j)] != 'M' && content[(i+j)] != 'R' && content[(i+j)] != 'que' && content[(i+j)] != 'T'){
                        tmp[(j-1)] = content[(i+j)];
                        j++;
                        }
                        else{
                            tmp[(j-1)] = '';
                            break;
                        }
                    }
                    area = s2i(tmp);
                    i+= j;
                }
                if(content[i] == 'que'){
                    j = 1;
                    while(1){
                        if(content[(i+j)] != 'A' && content[(i+j)] != 'M' && content[(i+j)] != 'R' && content[(i+j)] != 'que' && content[(i+j)] != 'T'){
                        tmp[(j-1)] = content[(i+j)];
                        j++;
                        }
                        else{                            tmp[(j-1)] = '';
                            break;
                        }
                    }
                    quality = s2i(tmp);
                    i+= j;
                }
                if(content[i] == 'T'){
                    j = 1;
                    while(1){
                        if(content[(i+j)] != 'A' && content[(i+j)] != 'M' && content[(i+j)] != 'R' && content[(i+j)] != 'que' && content[(i+j)] != 'T'){
                        tmp[(j-1)] = content[(i+j)];
                        j++;
                        }
                        else{                            tmp[(j-1)] = '';
                            break;
                        }
                    }
                    appletree = s2i(tmp);
                    break;
                }
            }
            }
            free(f);
        }
        Planter(){
            int menu;
            apple = 3;
            money = 10;
            area = 3;
            quality = 0;
            appletree = 0;
            printf("\n\t1.Novo Jogo");
            printf("\n\t2.Carregar Jogo\n\n>");
            scanf("%d",&menu);
            switch(menu){
                case 2:
                    load();
            }
            printf("ok\n");
            while(1){
                printf("Voce tem %d macas, %d  macieiras, %d reais e %d M2 de area.\n\n",apple,appletree,money,area);
                printf("Opcoes:\n\t1.Colher\n\t2.Vender\n\t3.Provar uma maca\n\t4.Plantar\n\t5.Comprar M2\n\n> ");
                scanf("%d",&menu);
                printf("\n\n");
                switch(menu){
                    case 1:
                        take();
                        break;
                    case 2:
                        sell();
                        break;
                    case 3:
                        test();
                        break;
                    case 4:
                        plant();
                        break;
                    case 5:
                        buy();
                        break;
                }
                
            }
        }
};

int main(){
    Planter planter;
};
O erro ocore quando você carega o arquivo usando a função load(), para caregar tem que usar um arquivo chamado data.txt: data.txt
A10M10R10Q3T10

falous

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

tipo sobre o erro parece que ta havendo algum estouro de pilha quando tu carrega esse arquivo e ele te avisa isso, um Buffer Overflow ...sobrecarregando.

sobre a solucao , vou ver aqui , talvez ate adicionando algum comando de esvaziar buffer , tipo testar memset(content, 0, 256); na funcao

att

Ramon Mayor Martins

Edited by Ramon Mayor Martins_
Link to comment
Share on other sites

  • 0

Opa Inufelipe , magina cara , que desculpa que nada , é pra isso que serve o forum , pra duvidas mesmo , pode perguntar qualquer coisa, o que tiver ao meu alcance eu tento explicar xD

Mas então,buffer é uma memória temporária, é normalmente utilizada quando a taxa de leitura e recebimento do arquivo são variáveis...

o buffer overflow é um erro que acontece quando se acessa um ponto além (over) do segmento de memória alocado para um dado

tentou o memset que eu sugeri ? da uma pesquisada nas referencias de funcoes sobre o memset , acredito que va ajudar no seu problema xD

att

Ramon Mayor Martins

Edited by Ramon Mayor Martins_
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Forum Statistics

    • Total Topics
      149.8k
    • Total Posts
      646.6k
×
×
  • Create New...