🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

3 total messages Started by Arthur Cinader Jr Sun, 17 Mar 1996 22:30
Why not? c++ Array of strings...
#930
Author: Arthur Cinader Jr
Date: Sun, 17 Mar 1996 22:30
93 lines
2721 bytes
Note Followup

I want a class to a have a member that is an array of
character strings that are dynamically allocated.  I have written 
a very simple program that shows the eveolution of my thought
that leads me to beleieve that I should be able to do what I
am trying, but I get a segmentation fault and core dump when I
try it.  Why?  I obviously am missing something.  I have kept
the code as brief as possible (I'm using g++ v2.4):


***foo.h
class Foo {
    public:
        Foo();      // constructor
        ~Foo();     // destructor
    private:
        char a[10];  // declare and define an array of characters
        char *b;     // declare a string pointer
        char *c[10]; // declare an array of ten strings?
};

***foo.C
nclude <iostream.h>
#include <assert.h>
#include <string.h>
#include "foo.h"

// Constructor -- all the action is here --
Foo::Foo()
{
        // populate the character array
        a[0] = 'a';
        a[1] = 'r';
        a[2] = 't';
        a[3] = 'h';
        a[4] = 'u';
        a[5] = 'r';

        // print the character array
        cout << "output from array of char: ";
        for(int i = 0 ; i < 6 ; i++)
                cout << a[i];

        cout << endl;

        // populate the string
        b = new char[7];                // allocate space
        assert( b != 0);                // make sure space was allocated
        strcpy(b, "arthur");

        // print the string
        cout << "output from *char: " << b;

        cout << endl;

        // So far so good.  This is where it all falls apart
        // populate array of strings
        c[0] = new char[10];    // allocate space
        assert(c[0] != 0);      // make sure space was allocated
        strcpy(c[0], "arthur");
        c[1] = new char[10];
        assert(c[1] != 0);
        strcpy(c[1], "cinader");
        c[2] = new char[10];
        assert(c[2] != 0);
        strcpy(c[2], "jr.");

        // output array of strings
        for (i = 0 ; i < 3 ; i--)
                cout << c[i] << " ";

        cout << endl;

}

Foo::~Foo() {
        // clean up the mess
        delete [] b;
        for(int i = 0; i < 3; i --)
                delete [] c[i];
}


**** foo.driver.C
#include "foo.h"

main()
{
        Foo f;

        return 0;
}
Re: Why not? c++ Array of strings...
#931
Author: Andreas Christoph
Date: Mon, 18 Mar 1996 10:39
118 lines
3472 bytes
In article <4ii3pp$4df@panix.com>,
	acinader@panix.com (Arthur Cinader Jr) writes:
>Note Followup
>
>I want a class to a have a member that is an array of
>character strings that are dynamically allocated.  I have written 
>a very simple program that shows the eveolution of my thought
>that leads me to beleieve that I should be able to do what I
>am trying, but I get a segmentation fault and core dump when I
>try it.  Why?  I obviously am missing something.  I have kept
>the code as brief as possible (I'm using g++ v2.4):
>
>
>***foo.h
>class Foo {
>    public:
>        Foo();      // constructor
>        ~Foo();     // destructor
>    private:
>        char a[10];  // declare and define an array of characters
>        char *b;     // declare a string pointer
>        char *c[10]; // declare an array of ten strings?
>};
>
>***foo.C
>nclude <iostream.h>
>#include <assert.h>
>#include <string.h>
>#include "foo.h"
>
>// Constructor -- all the action is here --
>Foo::Foo()
>{
>        // populate the character array
>        a[0] = 'a';
>        a[1] = 'r';
>        a[2] = 't';
>        a[3] = 'h';
>        a[4] = 'u';
>        a[5] = 'r';
>
>        // print the character array
>        cout << "output from array of char: ";
>        for(int i = 0 ; i < 6 ; i++)
>                cout << a[i];
>
>        cout << endl;
>
>        // populate the string
>        b = new char[7];                // allocate space
>        assert( b != 0);                // make sure space was allocated
>        strcpy(b, "arthur");
>
>        // print the string
>        cout << "output from *char: " << b;
>
>        cout << endl;
>
>        // So far so good.  This is where it all falls apart
>        // populate array of strings
>        c[0] = new char[10];    // allocate space
>        assert(c[0] != 0);      // make sure space was allocated
>        strcpy(c[0], "arthur");
>        c[1] = new char[10];
>        assert(c[1] != 0);
>        strcpy(c[1], "cinader");
>        c[2] = new char[10];
>        assert(c[2] != 0);
>        strcpy(c[2], "jr.");
>
>        // output array of strings
>        for (i = 0 ; i < 3 ; i--)
                              **** 
This should be i++, of course.

                               
>                cout << c[i] << " ";
>
>        cout << endl;
>
>}
>
>Foo::~Foo() {
>        // clean up the mess
>        delete [] b;
>        for(int i = 0; i < 3; i --)
                               *****
same here, i++

>                delete [] c[i];


>}
>
>
>**** foo.driver.C
>#include "foo.h"
>
>main()
>{
>        Foo f;
>
>        return 0;
>}

With these two changes it compiled and ran fine with GCC 2.7.0
under my Linux 1.2.13

Bye,
Andreas.


-- 
--------------------------------------------------------------------
Andreas Christoph
Alexanderstr. 292
26127 Oldenburg
--------------------------------------------------------------------
Re: Why not? c++ Array of strings...
#932
Author: Caius Martius
Date: Tue, 19 Mar 1996 00:10
93 lines
3133 bytes
Arthur Cinader Jr wrote:
> 
> Note Followup
> 
> I want a class to a have a member that is an array of
> character strings that are dynamically allocated.  I have written
> a very simple program that shows the eveolution of my thought
> that leads me to beleieve that I should be able to do what I
> am trying, but I get a segmentation fault and core dump when I
> try it.  Why?  I obviously am missing something.  I have kept
> the code as brief as possible (I'm using g++ v2.4):
> 
> ***foo.h
> class Foo {
>     public:
>         Foo();      // constructor
>         ~Foo();     // destructor
>     private:
>         char a[10];  // declare and define an array of characters
>         char *b;     // declare a string pointer
>         char *c[10]; // declare an array of ten strings?
> };
> 
> ***foo.C
> nclude <iostream.h>
> #include <assert.h>
> #include <string.h>
> #include "foo.h"
> 
> // Constructor -- all the action is here --
> Foo::Foo()
> {
>         // populate the character array
>         a[0] = 'a';
>         a[1] = 'r';
>         a[2] = 't';
>         a[3] = 'h';
>         a[4] = 'u';
>         a[5] = 'r';
> 
>         // print the character array
>         cout << "output from array of char: ";
>         for(int i = 0 ; i < 6 ; i++)
>                 cout << a[i];
> 
>         cout << endl;
> 
>         // populate the string
>         b = new char[7];                // allocate space
>         assert( b != 0);                // make sure space was allocated
>         strcpy(b, "arthur");
> 
>         // print the string
>         cout << "output from *char: " << b;
> 
>         cout << endl;
> 
>         // So far so good.  This is where it all falls apart
>         // populate array of strings
>         c[0] = new char[10];    // allocate space
>         assert(c[0] != 0);      // make sure space was allocated
>         strcpy(c[0], "arthur");
>         c[1] = new char[10];
>         assert(c[1] != 0);
>         strcpy(c[1], "cinader");
>         c[2] = new char[10];
>         assert(c[2] != 0);
>         strcpy(c[2], "jr.");
> 
>         // output array of strings
>         for (i = 0 ; i < 3 ; i--)  // *** This should be unary increment. ++ ***
>                 cout << c[i] << " ";
> 
>         cout << endl;
> 
> }
> 
> Foo::~Foo() {
>         // clean up the mess
>         delete [] b;
>         for(int i = 0; i < 3; i --) // *** This should be unary increment. ++ ***
>                 delete [] c[i];
> }
> 
> **** foo.driver.C
> #include "foo.h"
> 
> main()
> {
>         Foo f;
> 
>         return 0;
> }
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads