std::map operation by value or pointer? [closed] - c++

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.

Related

c++ nested structure initialization and accessing the members [closed]

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.

why do we need strcpy()? [closed]

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 6 years ago.
Improve this question
struct prac
{
int name[3];
char name1[12];
} b1, b2, c2;
main()
{
int i;
struct prac b2={1,2,3};
strcpy(c2.name1,"goodmorning");
printf("%s",c2.name1);
}
here to store the values in b2 array we just need to write b2={1,2,3} but if I want to store some values in the string c2 we need to call strcpy(), why it shows error if I write c2="goodmorning", rather than using strcpy()?
Because you can't assign to an array, only initialize it (which you do when you define the second b2 variable) or by copying to it (which you do with the strcpy call).

program to view addresses of elements in an array [closed]

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
help plz its showing invalid indirection
i used it to find the location or memory addresses of elements in the array b
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int *ptr;
int b[]={1,0,2,3,4,5,6,7,8,9};
ptr=b;
for(int i=0;i<10;i++)
cout<<ptr[i]<<" "<<*b[i];
}
In order to print the address of the ith element in an array b, use
std::cout << b + i;
This will work in all cases except when b is an array of char, in which case you need to cast to void*
std::cout << static_cast<const void*>(b + i);
in place of iostream.h it should be iostream.
void main() ; it should be int main().
cout<<ptr[i]<<" "<<(b+i)<<endl;
Formatting by using endl in above line of code will make the result clear.
Your function should return an integer value.
return 0;

2 value in one variable (const and const_cast) c++ [closed]

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.

Confusion with structure variable in C++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Is it possible to assign value of one structure variable to another structure variable
Is this code correct -
#include<iostream.h>
struct s1
{
int a;
float b;
char c;
} st1,st2,st3;
int main()
{
struct s2{
int x;
float y;
char z;
} ss1,ss2,ss3;
// Read & Initialize structures
ss2=ss1;
:
ss3.z=st1.c;
:
}
void func1()
{
ss2.x=st1.a;
ss3.y=st2.b;
ss1.z=st3.c;
:
ss1=ss3;
}
Kindly clear my doubt whether the above code is OK or not
ss2=ss1; this will depend on the compiler some compiler will allow to copy the structure variable and some not.
ss2.x=st1.a; ss2 structure will be local to the main function and it is not available in funct1(), so it must be throwing the compilation error.