This question already has answers here:
Returning a reference to a local or temporary variable [duplicate]
(5 answers)
Can a local variable's memory be accessed outside its scope?
(20 answers)
Return address of local variable in C
(5 answers)
Closed 9 years ago.
I saw some weird behavior with my C++ function whose work is to simply put 'n' ones(1).
char *getSpaces(int n) {
char s[50];
int i = 0;
for(i=0; i<n; i++) {
s[i] = '1';
}
s[i] = 0;
return s;
}
When I do fout<< getSpaces(20), I get following output in the file :-
1111111111SOME_WEIRD_CHARACTERS_HERE
Can anybody explain this?
P.S. I am using codeblocks IDE on windows platform.
Related
This question already has answers here:
C sizeof a passed array [duplicate]
(7 answers)
How to find the size of an array (from a pointer pointing to the first element array)?
(17 answers)
Closed 8 months ago.
I am trying to make my own function that takes a double array and returns maximum index. But when I run the code to check the function with a random array below, it returns 0 when it's supposed to return 9. When I run the same exact code inside the function in main, it works. What's wrong with my code that is working in main() but not when used inside a function?
int findMaxIndex(double array[]) {
int maxIndex=0;
int arraySize = sizeof(array)/sizeof(array[0]);
for (int i=0;i<arraySize;i++) {
if (array[maxIndex]<array[i]) {
maxIndex=i;
}
}
return maxIndex;
}
int main() {
double array[10] =
{11.20,22.2,33.3,43.4,55.5,66.6,77.7,88.8,90.0,100};
cout <<findMaxIndex(array)<<endl;
return 0;
}
This question already has answers here:
What happens when I print an uninitialized variable in C++? [duplicate]
(4 answers)
Uninitialized variable behaviour in C++
(4 answers)
Is uninitialized local variable the fastest random number generator?
(22 answers)
Why do I see strange values when I print uninitialized variables?
(6 answers)
Closed 3 years ago.
If not declare value in an array, then what value hold on the memory address.
Where did these outputs come from?
int main()
{
int a[10];
cout<<a[2]<<endl;
cout<<a[2]<<endl;
cout<<a[3]<<endl;
cout<<a[3]<<endl;
return 0;
}
//Given Output
//1978190368
//1978190368
//1129149863
//1129149863
This question already has answers here:
Is subtracting larger unsigned value from smaller in C++ undefined behaviour?
(2 answers)
Is unsigned integer subtraction defined behavior?
(6 answers)
Closed 3 years ago.
vector<int> a(10);
vector<int> b(20);
if ((a.size() - b.size()) > 1) {
cout << "yes";
}
Can anyone explain why this code prints "yes". If I try storing a.size() - b.size() in an int variable, it is working as expected.
This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 8 years ago.
I have a function in my class like:
centrala* siec_telek::wylosuj_centrale()
{
int wylosowana = dowolna_liczba_do(ilosc_central);
centrala wylosowana_centrala = lista_central[wylosowana];
centrala* wsk = &wylosowana_centrala;
cout <<wsk->przepustowosc_central[0]<<endl<<wsk<<endl;
return wsk;
}
And cout gives me good result but when I call in other function:
centrala* wylosowana_centrala = wylosuj_centrale();
cout << wylosowana_centrala->przepustowosc_central[0]<<endl<<wylosowana_centrala<<endl;
przepustowosc_central[0] gives another result but the pointer is ok (for instance 0x28f9cc twice)
Its because you are assigning local value address (wylosowana_centrala) to return value.
Due to wylosowana_centrala being stored on stack it may got overwritten after exiting function.
As fix you might try changing
centrala* wsk = &wylosowana_centrala;
to:
centrala* wsk = &lista_central[wylosowana];
This question already has answers here:
Accessing arrays by index[array] in C and C++
(2 answers)
Closed 9 years ago.
Today when i was coding inside my visual studio i unintentionally did following
for(int i=0;i<10;i++)
{
cout<<"Value is"<<[i]arr<<endl;
}
instead of arr[i] and it worked.why it worked?
Because [i]arr == *(i + arr) == arr[i]
Note: + operator holds commutative property