Placement new input issue

May 31, 2015 at 6:07pm
I'm stumped with a question. I wrote out an answer but it doesn't seem to be working. The inputs aren't the same as the outputs: it gives me some weird symbols for the name and 8 and 0 for the numbers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <new>

struct chaff
{
    char dross[20];
    int slag;
};

int main()
{
    chaff stru1;
    chaff stru2;

    char stru1mem [sizeof(chaff)];
    char stru2mem [sizeof(chaff)];

    std::cout << "Enter the name for the 1st dude: ";
    std::cin.getline (stru1.dross, 20);
    std::cout << "Enter his number: ";
    std::cin >> stru1.slag;
    std::cin.ignore (100, '\n');
    std::cout << "Enter the name for the 2nd dude: ";
    std::cin.getline (stru2.dross, 20);
    std::cout << "Enter his number: ";
    std::cin >> stru2.slag;
    std::cin.ignore (100, '\n');

    chaff *stru1p = new (stru1mem) chaff;
    chaff *stru2p = new (stru2mem) chaff;

    std::cout << "1st dude's name: " << stru1p->dross << " and his number: " << stru1p->slag << std::endl;
    std::cout << "2nd dude's name: " << stru2p->dross << " and his number: " << stru2p->slag << std::endl;
}


Is there anything wrong with my workings?

Thanks!
Last edited on May 31, 2015 at 6:07pm
May 31, 2015 at 6:18pm
You have two separate things going on here.

1. You create two chaff objects stru1 and stru2 and read user input to assign values to the member variables.
1
2
3
4
5
6
7
8
9
10
11
12
13
chaff stru1;
chaff stru2;

std::cout << "Enter the name for the 1st dude: ";
std::cin.getline (stru1.dross, 20);
std::cout << "Enter his number: ";
std::cin >> stru1.slag;
std::cin.ignore (100, '\n');
std::cout << "Enter the name for the 2nd dude: ";
std::cin.getline (stru2.dross, 20);
std::cout << "Enter his number: ";
std::cin >> stru2.slag;
std::cin.ignore (100, '\n');

2. You create two chaff objects using placement new, but you never assign any values to the member variables so when you try to print them you get garbage.
1
2
3
4
5
6
7
8
char stru1mem [sizeof(chaff)];
char stru2mem [sizeof(chaff)];

chaff *stru1p = new (stru1mem) chaff;
chaff *stru2p = new (stru2mem) chaff;

std::cout << "1st dude's name: " << stru1p->dross << " and his number: " << stru1p->slag << std::endl;
std::cout << "2nd dude's name: " << stru2p->dross << " and his number: " << stru2p->slag << std::endl;
Last edited on May 31, 2015 at 6:44pm
May 31, 2015 at 6:25pm
I am assuming you were trying to use pointers to try and troubleshoot your issue.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

struct chaff {
    char dross[20];
    int slag;
};

int main() {
    chaff stru1;
    chaff stru2;
    std::cout << "Enter the name for the 1st dude: ";
    std::cin.getline(stru1.dross, 20);
    std::cout << "Enter his number: ";
    std::cin >> stru1.slag;
    std::cin.ignore(100, '\n');
    std::cout << "Enter the name for the 2nd dude: ";
    std::cin.getline(stru2.dross, 20);
    std::cout << "Enter his number: ";
    std::cin >> stru2.slag;
    std::cin.ignore(100, '\n');
    std::cout << "1st dude's name: " << stru1.dross << " and his number: " << stru1.slag << std::endl;
    std::cout << "2nd dude's name: " << stru2.dross << " and his number: " << stru2.slag << std::endl;
}
May 31, 2015 at 7:54pm
Is there anything wrong with my workings?

Using placement new with a old-fashioned struct is a bit pointless.

The mechanism is provided so you can invoke the constructor of your class independently of the memory allocation normally done by (regular, non-placement) new.

If you have no constructor there is zero point in using this approach.

Andy
Last edited on May 31, 2015 at 7:55pm
Topic archived. No new replies allowed.