Loop End Error (Bug delete with char*)

Asked

Viewed 216 times

-3

1 - I have this code (Gave An Abridged, Type -1, And Then 'n' To Exit The Loop And Make The Mistake):

#include <iostream>
#include <cstdlib>
#include <cstdio>
#undef max
using namespace std;

int* cont = new int(0);

//-----------------------------------------// MAIN // -----------------------------------//

void main(){
    char* n = new char('s');

    while (*n == 's' || *n == 'S'){

        int* dinheiro = new int(500);

        while (*dinheiro >= 1){
            printf("Quanto Deseja Apostar ? ");
            scanf("%d%*c", &*dinheiro);

            printf("\nTentar Novamente (s-n) ? ");
            cin >> n;
            cin.ignore(numeric_limits<int>::max(), '\n');
            system("cls");

        }
        delete dinheiro;
    }
    delete n;
}

And in that part:

printf("Tentar Novamente (s-n) ? ");
cin >> *n;
cin.ignore(numeric_limits<int>::max(), '\n');
system("cls");

If user does not type’s' to get back in the loop it gives this error:

Erro

Obs.: Apart from the delete the program works correctly.

  • 2

    No need to worry about every byte. I find it unlikely that swapping int for void will save anything. Returning an int is one of the simplest things a program usually does, because the value is returned inside a processor logger.

  • 1

    You’re distressed by the wrong things ;-) But if you still want to fret about it, I suggest you do the Disassembly of the generated code (with and without optimizations, to compare), and compare the results. Also run a benchmarks to measure time and memory usage. If you notice any difference I swallow my diploma (<- this is figure of speech, haha)

2 answers

4

See the line:

cin >> n;

The variable n is char*, so the >> operator thinks there is an array allocated there, and reads things that do not fit in the buffer ("-1" has 2 chars). There’s no buffer, because you’ve dynamically allocated only 1 char (to train, OK, in a real program, it seems pointless).

I suggest you switch to:

cin >> *n;

or

scanf("%c", n);

If you type -1, the line above (any one) will read only the "-", without writing out of allocated space.

Something else: &*dinheiro is half useless. & and * operators cancel each other (in the absence of overloading Operator), and it would be the same as writing only dinheiro.

And an important lesson in the world of C and C++: the fact that the compiler accepts is very loosely related to the fact that the program is correct.

  • The point is, it doesn’t solve my real code: Code

  • 4

    Okay, I pointed out where I saw an error and I didn’t post the solution. I already edited to fix it. Note: be perfectionist here too and write "leave it" and "access it", with the accent in the right place ;-)

  • On the question of the parameter numeros for Dimdim, actually there is nothing complicated, just pass: Dim(0, numbers)... And if you really want pointer to pointer just use **.

  • I just thought I didn’t need to say this, but to get the address of a variable just use the operator &: __int8 alguma_pos = 0; Dim(&alguma_pos, numeros);

  • I know that but (&0) It Doesn’t Work Right, The Biggest Question I Want Is This: How to Create a Pointer -> void DimDim(__int8 pos, ( int* dinheiro[] <- ISSO DAKI ) ) And how I would pass that parameter

  • 1

    Exactly. That’s why I put the right way to do it, putting it in a temporary variable. Taking the "address from scratch" doesn’t make sense. Imagine if you assign something to the value pointed by that pointer and change the value of zero? hahahaha

  • All right Forget the first parameter and focus on the second.

  • My last comment: void DimDim(__int8 pos, int** dinheiro); is called with: int* coisas = new int[9]; DimDim(0, &coisas). Note: I changed the int* dinheiro[] for int** on purpose.

Show 3 more comments

-4


I solved it in a simple way: instead of char* n I’ll use string* n. Ready, the delete non-Boat

I managed to resolve the void DimDim()

Now she’s like this void DimDim(__int8 pos, int* numeros)

And to call her I do so: DimDim(3, &numeros[0]);

After researching well, I understood a little more about the operator [] so I got :D

  • What You Want Me To Learn ?

  • 3

    Using pointers. My tip didn’t solve? I don’t even know if you tried it.

  • @But I changed character to string didn’t need your tip and I’m giving a +1 in your reply for trying to help thank you

  • 1

    My answer was helpful to me and people of -1... Do not Give To Understand The Sopt... , In Friend’s Response The Bottom Has Not Been Solved My Problem But He Has +2 One It Was I Gave Because N I Have To Click The Up Arrow, I Try To Help And I Recognize.

  • 1

    Don’t worry, face, participate in the community that you take the tricks and understand how it works. #Paredeescreverassim

Browser other questions tagged

You are not signed in. Login or sign up in order to post.