Bypasses initialisation in goto statement [duplicate] - c++

This question already has answers here:
Goto before variable initialization causes compiler error
(4 answers)
initialization of 'unused' is skipped by 'goto label' - why do I get it for std::string but not for int?
(2 answers)
Closed 12 days ago.
I've come across an error that I thought should have been a warning:
char* p;
int main()
{
if (p) goto continue_func;
int a = 3;
continue_func:
int b = 2;
}
The error I get:
initialization of 'a' is skipped by 'goto continue_func'
transfer of control bypasses initialization of:
I don't see why this is illegal. If the case had been something like this:
char* p;
int main()
{
if (p) goto continue_func;
int a = 3;
continue_func:
int b = 2;
int c = a + b;
std::cout << c;
}
Then I understand that a is being used. But in the first example it's not. Is this truly illegal C++, and why?

Related

How to give alias for a function in c [duplicate]

This question already has answers here:
How do I assign an alias to a function name in C++?
(8 answers)
Closed 6 years ago.
The problem is something like:
I have a function named ADD_TWO(int,int);, now in the program, I want to create its alias keeping the previous function untouched. Something like ADD_TWO_NUMBERS(int,int);. i.e if I call ADD_TWO_NUMBERS it should execute ADD_TWO.
I want to have an Alias for the function.
int ADD_TWO(int a, int b)
{
return a+b;
}
int main()
{
int (*ADD_TWO_NUMBERS)(int, int) = ADD_TWO;
int result = ADD_TWO_NUMBERS(10, 12); //or (*ADD_TWO_NUMBERS)(10, 12)
cout<<" Result :- "<<result<<endl;
return 0;
}
Use Function pointers
Aside from using function pointers you can use function-like macros. Try something like
#define ADD_TWO_NUMBERS(x,y) ADD_TWO(x,y)
Since this question has c++ tag and assuming you're using a c++11 compiler, then:
int ADD_TWO(int a, int b)
{
return a+b;
}
int main()
{
auto ADD_TWO_NUMBERS = ADD_TWO;
int result = ADD_TWO_NUMBERS(10, 12);
cout << " Result :- " << result << endl;
return 0;
}

c++ googletest handling exceptions within try-block [duplicate]

This question already has answers here:
why does throw "nothing" causes program termination?
(7 answers)
Closed 6 years ago.
When I start my gtest project with a structure like this, the code always breaks at the throw-statement, as if there is no try-catch around it.
Is There a way of changing the behaviour to just go on in the catch-block?
void errornousFunction()
{
try
{
int i = 5;
throw;
}
catch ( ... )
{
int i = 5;
}
}
TEST(testCaseName,asdf)
{
errornousFunction();
}
void errornousFunction()
{
try
{
int i = 5;
throw i;
}
catch ( ... )
{
int i = 5;
}
}
then u can try

strange behavior of placement new [duplicate]

This question already has answers here:
Why doesn't my program crash when I write past the end of an array?
(9 answers)
Closed 9 years ago.
class A
{
public:
static void * operator new (size_t,void *p)
{
return p;
}
int i;
};
int main()
{
void *p = malloc(sizeof(A));
cout<<p<<endl;
A *a= new (p) A;
a->i = 10;
cout<<a<<endl;
cout<<a->i<<endl;
a->i = 100;
cout<<a->i<<endl;
}
output:
0x1e0e010
0x1e0e010
10
100
But I change the code of operator new to
static void * operator new (size_t,void *p)
{
return p+1024;
}
it doesn't crash and its output is:
0x25c4010
0x25c4410
10
100
I am using ubuntu13.10 and gcc4.8.1
Thanks
The short answer: Undefined behavior is undefined.

memcmp strangely fails returning array [duplicate]

This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 9 years ago.
I have C++ code that looks like this:
static int* ArrayGenerator()
{
int temp[1] = {9};
return temp;
}
static int* ArrayGenerator(int i)
{
//parameter is just for demonstration
int temp[1] = {9};
return temp;
}
int _tmain(int argc, _TCHAR* argv[])
{
int arr1[1] = {9};
printf("arrays are %s equal\n\n", (memcmp(arr1, ArrayGenerator(), 1) == 0) ? "" : "not");
printf("arrays are %s equal\n\n", (memcmp(arr1, ArrayGenerator(1), 1) == 0) ? "" : "not");
}
The first gives me 'are equal' the second gives me 'are not equal'.
Why is this?
You cannot return local pointers from a function. When you return temp from the functions it passes out of scope and the memory is no longer valid. This causes undefined behavior. I already explained this here

Checking the int limits in stoi() function in C++ [duplicate]

This question already has answers here:
c++ parse int from string [duplicate]
(5 answers)
Closed 9 years ago.
I have been given a string y in which I'm ensured that it only consists digits. How do I check if it exceeds the bounds of an integer before storing it in an int variable using the stoi function?
string y = "2323298347293874928374927392374924"
int x = stoi(y); // The program gets aborted when I execute this as it exceeds the bounds
// of int. How do I check the bounds before I store it?
you can use exception handling mechanism:
#include <stdexcept>
std::string y = "2323298347293874928374927392374924"
int x;
try {
x = stoi(y);
}
catch(std::invalid_argument& e){
// if no conversion could be performed
}
catch(std::out_of_range& e){
// if the converted value would fall out of the range of the result type
// or if the underlying function (std::strtol or std::strtoull) sets errno
// to ERANGE.
}
catch(...) {
// everything else
}
detailed description of stoi function and how to handle errors
Catch the exception:
string y = "2323298347293874928374927392374924"
int x;
try {
x = stoi(y);
}
catch(...) {
// String could not be read properly as an int.
}
If there is a legitimate possibility that the string represents a value that's too large to store in an int, convert it to something larger and check whether the result fits in an int:
long long temp = stoll(y);
if (std::numeric_limits<int>::max() < temp
|| temp < std::numeric_limits<int>::min())
throw my_invalid_input_exception();
int i = temp; // "helpful" compilers will warn here; ignore them.