where does struct object get memory in main function? [duplicate] - c++

This question already has answers here:
Are data members allocated in the same memory space as their objects in C++?
(6 answers)
Closed 4 years ago.
how does tmp get memory from the machine, from heap or stack?
I thought it was from the stack, but it seem that the code can run properly
#include<bits/stdc++.h>
using namespace std;
struct node {
int a[1000000];
};
int main() {
node tmp;
memset(tmp.a, -1, sizeof(tmp.a));
cout << tmp.a[0];
return 0;
}

In stack, since it's an automatic variable to the main function.
PS: This code doesn't compile, for example with this error: error: type 'node' does not provide a subscript operator: cout << tmp[0];.

Related

Why this code does NOT give segmentation fault? [duplicate]

This question already has answers here:
Does "Undefined Behavior" really permit *anything* to happen? [duplicate]
(9 answers)
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 1 year ago.
This is a simple program I wrote to induce segfault
#include<iostream>
using namespace std;
char* func(){
char ch='a';
char *ptr=&ch;
return ptr;
}
int main()
{
char *ptr=func();
cout<<*ptr;
}
As variable ch must've been freed after func() scope ends, I thought that this program would give segmentation fault.
But it shows output as 'a'. What am i missing ?

How am I being able to access function stack memory after returning? [duplicate]

This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 1 year ago.
This is my code:
using namespace std;
// if memory is alloted statically then you can't access memory address after the function has returned and its stack is gone.
int* function(int x)
{
int *x_ptr = &x;
return x_ptr;
}
int main()
{
int x=10;
int *x_ptr = function(x);
cout<<*x_ptr<<endl;
}
you can't access memory address after the function has returned
Correct.
The behaviour of the program is undefined.

Why can I initialize the size of an array by taking user input in C++? [duplicate]

This question already has answers here:
Are variable length arrays there in c++?
(2 answers)
Array[n] vs Array[10] - Initializing array with variable vs numeric literal
(1 answer)
Closed 1 year ago.
Given that user input is taken at runtime but array is created during compile time. How and why does this piece of code work?
#include <iostream>
using namespace std;
int main(){
int n;
cin>>n;
int arr[n]; //Why can I use this statement to create an array?
}
It's a non-standard extension, supported by gcc and clang.
It works by allocating space on the stack at the point the array is declared, a bit like alloca.
The memory allocated is automatically freed (by adjusting the stack pointer) when the function returns (or arr goes out of scope).

When I run this code, result is always 24 regardless of what string is. Why? [duplicate]

This question already has answers here:
c++ sizeof( string )
(9 answers)
Closed 1 year ago.
When I run this code, result is always 24 regardless of what string is. Why?
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s = "asdccccc";
cout << sizeof(s);
return 0;
}
A string is an object. By using sizeof you are getting the size of the members of that object. One of those members is probably a pointer to the actual string contents, but the size of a pointer is constant no matter what it points to.
Consider this simple example
class string
{
const char* _ptr;
....
....
public:
}
When you write sizeof(string), you will get the size of the class, not the size of string literal _ptr points to.

Error when declaring local array [duplicate]

This question already has answers here:
Getting a stack overflow exception when declaring a large array
(8 answers)
Closed 8 years ago.
Something like this throws an error:
using namespace std;
int main()
{
int test[1000000] = {};
}
Something like this doesn't:
using namespace std;
int test[1000000] = {};
int main()
{
}
Why is that? A million ints isn't even too memory-demanding.
The first one allocates space on the stack. The second one allocates space in the data segment at compile/link time. The stack is of limited size.
Stack is not dynamic, but you can also do this
int* arr = new int[1000000];
but don't forget to delete it because this declares array in the heap which is dynamic memory and by deleting it from heap you prevent the memory leak.
Example :
delete arr;
This is just alternative how to use memory