Questions tagged as 'c++'

2
answers

How to calculate Euclidean distance

I want to calculate the Euclidean distance using the following formula: SoItried,makingthiscode:#defineSLEEP_11000voidHeaderClass::DistanciaEuclidianaEntrePontos(){intx1,x2,y1,y2,distancia;std::cout<<"Coordenadas ponto 1 (x): "; std...
asked by 11.12.2015 / 21:28
2
answers

How to organize the code without losing performance?

Using functions instead of putting the code directly influences the performance of a program? For example: void minhafuncao(){ printf("Funcao"); } main(){ minhafuncao(); } instead of main(){ printf("Funcao"); } I know macros have...
asked by 09.04.2015 / 17:45
3
answers

Random numbers are always the same

Why is this code always displaying the same random number results as a given? // Figura 6.9: fig06_09.cpp // Lança um dado de seis lados 6.000.000 de vezes. #include <iostream> using std::cout; using std::endl; #include <iomanip>...
asked by 29.08.2017 / 16:44
4
answers

Copy from txt file to the other

int main(void) { void copiaConteudo(FILE *arquivo, FILE *arquivo1); FILE *arquivo = fopen("tmp/exercicio.txt","r"); if (arquivo == NULL) { printf ("Não foi possível abrir o arquivo"); return 1; } FILE *arq...
asked by 31.08.2017 / 21:50
2
answers

Is there any difference between the ways to declare classes in C ++?

Since I started learning C ++ I've always done something like: class.h #ifndef CLASSE_H #define CLASSE_H class OutraClasse; class Classe { public: Classe(); void foo(OutraClasse *bar); }; #endif And here I declare met...
asked by 05.10.2016 / 20:07
3
answers

How does serial communication work and how to do it using C / C ++?

I use Debian 7.1 and I need to do serial communication between a computer and a microcontroller using C / C ++ . I have already done a lot of research on the subject, but the articles and examples I found are very confusing, unclear and not v...
asked by 20.03.2014 / 17:05
1
answer

What is using namespace?

Since the beginning of my C ++ learning, I've been told to use namespace std , so I do not spend all the time using std::cout . However, after studying more about language, I learned that :: is a scope operator, but what ab...
asked by 05.12.2018 / 15:38
2
answers

How to read the command line?

Let's say I want to do a routine to automate the process of compiling a program. In general (regardless of language), you call the compiler from the command line. For example:    javac myProgram.class or    gcc myProgram.c -o myProgram...
asked by 20.07.2016 / 17:45
1
answer

___ ___ erkimt Is there a difference between using "return" or "exit ()" to end the "main ()"? ______ qstntxt ___

%code% terminates running the application immediately. The %code% command exits the function, but when it is inside the %code% it will exit the application, including the value used in it will be returned to the one who called the application as an error code (zero is ok), as well as the function output.

Of course there is a difference in other functions, where %code% terminates application immediately and the command will return the execution stream to the calling function.

If both do the same thing in the described situation, does either use one or the other?

    
______ azszpr174313 ___

There is an important semantic difference in C ++. The %code% will close the scope and call all pending destructors. If the application is closed, rarely will the call of the destructors produce a different result, but technically it is possible for one of them to do something that is important to the end of the application, or to print relevant information for the user. >

Even though the action looks the same, the %code% function causes a premature exiting of the application. Then execution is terminated almost immediately. %code% finalizes static objects, but not destructors. The %code% and %code% neither does this and it closes on time.

There's a difference even in C if you have functions registered with %code% . These functions will always run no matter how the application is shutting down. But there will be undefined behavior if one of the linked functions in %code% has reference to some data in stack . Something like %code% and %code% . This is especially important in C. documentation

Note that the %code% function is not special and can be called within the application. In this case there will be an important difference since a %code% will not terminate the application, although in the context of the question this would not happen. But understand that if what you wrote will not be called in the operating system and this %code% is part of a module that will be loaded and used in another application, your module will terminate the application and not only your code if you use %code% , %code% will then be the most appropriate in most situations.

A call to %code% can be recursive, although it should not. Of course there is difference in this case, but it falls into what I said in the previous paragraph.

That's why you always have to choose the most semantically correct way . If you want to terminate the application immediately use %code% , otherwise use %code% . Many people say that if %code% is terminating the application, %code% is the most appropriate. Not everyone agrees, especially in C ++. That's why it's good to understand the workings, the implications of each and do not blindly follow rules.

I do not know if it counts as a difference, because it is a function, to use it, you need to use a %code% so that it is available, and if the code unit it contains would not enter the application, the executable will be a bit bigger. The command is always available.

    
___

exit() terminates running the application immediately. The return command exits the function, but when it is inside the main() it will exit the application, including the value used in it will be returned to the one who ca...
asked by 30.12.2016 / 13:15
1
answer

What is the use of * in the expression "Foo * foo = new Foo" in C ++? [duplicate]

I was analyzing this question made in SOEN. There you are teaching to instantiate a particular class. I was able to understand more or less how it works, because when I use my example, it is giving error when compiling. class Pessoa {...
asked by 05.01.2016 / 15:50