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
Related
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?
This question already has answers here:
In a non-void function I want to return nothing
(2 answers)
When and how should I use exception handling?
(7 answers)
Closed 6 months ago.
Consider the following code:
#include <iostream>
int test(int a){
if (a > 10){
return a;
}
else{
std::cout << "Error!";
return nothing;
}
}
int main(){
std::cout << test(9);
return 0;
}
What I want is that The integer function test(int a), return a if a > 10, otherwise return Error!. but since this is an integer function, it must return an integer value, but I want that it print Error and return nothing. Is there a way for do this? (Also note that I don't want to use a void function)
#include <stdexcept>
int test(int a){
if (a > 10){
return a;
}
else{
throw std::invalid_argument( "a is smaller or eq than 10" );
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Im new to c++ and im trying to get a bool* function with a dynamic array as its parameter and its size to return true or false when none of them is zero but im getting an error: cannot convert 'bool' to 'bool*' in return.
bool* noneZero(int *zero, int N) {
int PL = 0;
for (int i = 0; i < N; i++) {
if (i == 0) {
PL++;
}
}
if (PL == N)
return false; //cannot convert 'bool' to 'bool*' in return
else
return true; //cannot convert 'bool' to 'bool*' in return
}
int main(int argc, char** argv) {
int *z=new int[5]{0,0,0,0,0};
cout << noneZero(z,5);
}
Also the question is how the teacher gave it to me. we dont work with vectors. Basically i have to return false when all of the numbers in my dynamic array are 0 and true when they arent. My question is why i get an error: cannot convert 'bool' to 'bool*' in return
Frankly, there are many problems in your code and I suggest to start from scratch.
First, dynamic arrays in C++ are std::vector. You need a good reason to use something else.
Next, your function is too complicated. You need not count the number of zeros if all you want is to check if there is none or at least one:
bool noneZero(const std::vector<int>& in) {
for (size_t i=0; i< in.size(); ++i) { // a vector "knows" its size !
if ( in[i] == 0 ) return false; // no need to search further
}
return true;
}
int main() {
std::vector<int> z{1,2,3,4,5};
bool x = noneZero(z);
if (x) std::cout << "there is no 0 in the vector\n";
for (const auto& e : z) {
std::cout << e << " ";
}
}
A std::vector manages the memory for you, ie no need for manual new or delete. It is not clear why you use the pointer returned from the function as if it points to an array (it does not and your code has undefined behavior). I added a loop that prints the vectors elements.
Problem:
A bool* must return a pointer a pointer to a bool, not a bool itself.
You're checking the value of i, not the values of the array.
Solution:
Change the function from a bool* to a bool.
Change i == 0 to *(zero + i) == 0.
Additional information:
Seems like you're using using namespace std;. using namespace std; is considered a bad practice (More info here).
You probably should use std::vector if you can.
Full code:
#include <iostream>
bool noneZero(int *zero, int N) {
int PL = 0;
for (int i = 0; i < N; i++) {
if (*(zero + i) == 0) {
PL++;
}
}
if (PL == N)
return false;
else
return true;
}
int main(int argc, char** argv) {
int *z = new int[5]{0,0,0,0,0};
std::cout << noneZero(z,5);
delete[] z;
}
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;
}
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.