What am I doing worng?

This is a discussion on What am I doing worng? within the C++ Programming forums, part of the General Programming Boards category; I have been reading C++ Primer Plus, and I'm stuck on this programming question: Begin with the following structure declaration: ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    15

    What am I doing worng?

    I have been reading C++ Primer Plus, and I'm stuck on this programming question:
    Begin with the following structure declaration:
    struct chaff
    {
    char dross[20];
    int slag;
    };
    Write a program that uses placement new to place an array of two such structures in a
    buffer. Then assign values to the structure members (remembering to use strcpy() for
    the char array) and use a loop to display the contents. Option 1 is to use a static array,
    like that in Listing 9.9, for the buffer. Option 2 is to use regular new to allocate the
    buffer.
    This is what I have:
    Code:
    #include <iostream>
    #include <new>
    
    using namespace std;
    
    struct chaff
    {
    	char dross[20];
    	int slag;
    };
    
    int main()
    {
    	char * buf = new char(sizeof(chaff) * 2);
    	chaff * c1 = new (buf) chaff;
    	chaff * c2 = new (buf + sizeof(chaff)) chaff;
    	strcpy(c1->dross, "Chaff 1");
    	c1->slag = 5;
    	strcpy(c2->dross, "Chaff 2");
    	c2->slag = 2;
    	chaff * c;
    	for (int i = 0; i < 2; i++)
    	{
    		c = (chaff *) &buf[sizeof(chaff) * i];
    		cout << c->dross << endl;
    		cout << c->slag << endl;
    	}
    	delete [] buf;
    }
    It compiles, but when I run it I get this
    First-chance exception at 0x7c91916a in book samples.exe: 0xC0000005: Access violation reading location 0x0037a8ed.
    First-chance exception at 0x7c812afb in book samples.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0012fc44..
    Unhandled exception at 0x7c812afb in book samples.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0012fc44..
    The program '[3552] book samples.exe: Native' has exited with code 0 (0x0).

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    You got some syntax issues with your use of new. One possible way to handle that part:
    Code:
    	char * buf = new char[sizeof(chaff) * 2];
    	chaff * c1 = new (buf) chaff[2];
    So c1 points to two chaff objects... no need for a c2 var.

    Code:
    chaff * c;
    for(...)
    {
        c = (chaff *) &buf[sizeof(chaff) * i];
    This part could be much simpler:
    Code:
    chaff * c;
    for(...)
    {
        c = c1+i;
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What am I doing worng? Segmentation fault, core dump
    By cookie in forum C Programming
    Replies: 4
    Last Post: 06-08-2007, 10:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21