Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
struct Point
{
double x,y;
};
Point p;
struct Disk
{
Point center;
int radius;
};
Disk d;
int main()
{
d.center.x=1.2;
cout<<p.x;
}
Could someone please explain me the output of this code?
why am I not getting the value of x as 1.2 and 0 instead?
Let's go through your code line by line.
First, you created a Point called p. So p is sitting in the memory somewhere:
Memory: p:[x, y]
Then, you created a Disk called d, which stores it's own Point object inside it.
Memory: p:[x, y] d:[center:[x, y], radius]
These are completely separate objects. When you modify the Point stored in d with d.center.x=1.2, it does not affect p at all.
Therefore, p is uninitialized, and reading the value of uninitialized variables causes undefined behavior, meaning anything can happen, in this case usually getting a random value.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Is it possible to declare a variable without assigning value in C++?
int a; int a=15; Which one will be Correct?
If i assign value to a variable 3 times or more which one will count at the end in C++??
int a=15;
int a=10;
int a=5;
Which value will be execute for a at the end?
int a; // declared but not assigned
a = 1; // assigning a value
a = 2; // assigning a different value
a = 3; // assigning another value
std::cout << a << "\n"; // will print 3 since only the last assignment matters
int x; declares a variable x without assigning a value.
If you assign to a variable three times then which ever assignment executed last will be the variables final value. Very important idea, C++ statements execute in a specific order, and which order they execute in is essential to understanding what a program does.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Assuming my pointer is not pointing to an array, how is this block:
int *ptr_a;
ptr_a = new int;
*ptr_a = 1;
different compared with:
int *ptr_a = 1;
Is there a difference in memory allocation and when would I use one over the other?
int *ptr_a = 1;doesn't create a new int, this creates a pointer ptr_a and assigns a value of 1 to it, meaning that this pointer will point to the address 0x00000001. It's not an integer. If you try to use the pointer later with *ptr_a = 2, you will get a segmentation fault because the pointer doesn't point to an allocated memory space (in this precise case, it points to kernel memory, which is a no-no).
A good principle nowadays is to used std::unique_ptr<int> ptr_a = std::make_unique<int>(1)which will allocate a new int with a value of 1 and deallocate the memory once ptr_a gets out of scope.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
void main() {
const int a = 10;
const int *b = &a;
int *c = const_cast <int*>(b);
*c = 5;
cout<<a<<" "<<*b<<" "<<*c<<endl; //10 5 5
cout<<&a<<" "<<b<<" "<<c<<endl; //same address
cout<<*(int*)&a<<" "<<*&a<<endl; //5 10
}
what makes type cast affected this?
where is the value stored?
The program has undefined behavior: with the const_cast<int*>(b) you remove the const qualifier from an object which actually is const and the assignment to that object may have arbitrary effect.
The observed effects indicate that the implementation replaced uses of a with its immutable value while it dereferences b to determine the value. It could have arbitrary other effect, too, though. For example, a segmentation fault when trying to write a write protected location could be a possible outcome. Well, anything can happen.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How to cast a Pointer in C++ from class name in String?
Psuedocode:
int * ptr = something;
myStruct ptrstruct = (ClassFromString("myStruct") ptr);
// The class/struct name is passed in as String
Thank you
I'm not a c++ guru but I have two ideas that may help with brainstorming:
May the use of the registry pattern as described here would be of help: Instantiate class from name?
Secondly, following the registry pattern idea you could crate a function for casting e.g. MyClass something = registry.cast("MyClass", ptr);
I am not sure but this must work
Only Void pointer or boost can help, if it happens
thing * p = something; // pointer to object
void * pv = p; // pointer to void
thing * p2 = static_cast<thing *>(pv); // pointer to the same object
Maybe same kind of situation is while returning values from Threads
Overall reflection is not possible in c++.
Its just brainstroming.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I has a class A:
struct A
{
std::map<int,int> aa;
}
and a class B:
struct B
{
std::map<int,B> bb;
}
If those code in a infinite loop:
B b;
int inx=0;
while(true)
{
A a;
a.aa[0] = 0;
b.bb[inx] = a;
++inx;
}
Will that cause memory leak ?
I want all values of a copy to b[inx]. I just want to know that is the implementaion of std::map pointer ?
Will that cause memory leak ?
There are no memory leak in your program, but a compilation error, since there are no operator[] defined for struct B.
Assuming you add to map here:
while(true)
{
A a;
a[0] = 0;
b[inx] = a;
++inx;
}
there are no memory leaks. The memory will increase until you use all int numbers, but that's normal, since they have to be stored somewhere in memory.