TingRay
TingRay

Reputation: 29

Trouble with a Pointer to an Array of Structures in C++

My assignment is to translate a phrase from pig Latin to English using a structure. My project thus far takes in a string, removes all capitalization, all punctuation besides the ending punctuation, and splits the string into an array of structures comprised of words. I am then supposed to, through the return statement specifically, return a pointer to my array of structures. When back in main, I want to create another array of structures identical to that in my pigLat function that I will be able to send to a new function for the second part of my project (which will consist of translating the pig latin to english).

The problem: trying to create a new array using the pointer causes a core segmentation fault.

Any help explaining why this doesn't work, and explaining what might work better would be appreciated!

#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
using namespace std;

struct Word                                           //Variable type to hold each word
{
    string piglatin;
    string english;
};

Word *pigLat(int &);

int main()
{
    int size;                                      
    Word *phrase;
    phrase = pigLat(size);                //Passes value of size by reference, and returns ptr to structure
    Word pigSent[size];

    //THIS IS WHERE I GET SEGMENTATION FAULT************************************
    for (int count = 0; count < size; count++)
    {
        pigSent[count].piglatin = phrase[count].piglatin;
    }
     //*************************************************************************
    return 0;
}

//Receives a phrase in pig latin, finds # of words in phrase, seperates pig  latin from english, returns pig latin
Word *pigLat(int &sizeOf)
{
    string phrase;                                  //Variable to hold pig latin phrase 

    cout << "Enter a phrase in pig latin: ";        //User enters pig latin   phrase
    getline(cin, phrase);

    char punctuation = phrase[phrase.length() - 1]; //Assumes last char is punctuation, and saves it

    //Removes all characters besides last period
    char removch[] = "&,'?.!-";
    for (int count = 0; count < 7; count++)
    {
        phrase.erase(remove(phrase.begin(), phrase.end(), removch[count]),  phrase.end());
    }

    int length = phrase.length();                   //Number of elements in updated string
    phrase.insert(length, 1, punctuation);          //Inserts final punctuation at end of phrase

    //Removes all capitalization
    for (int count = 0; count < length; count++)
    {
        if(phrase[count] >= 'A' && phrase[count] <= 'Z')
        {
        phrase[count] = tolower(phrase[count]);
        }
    }

    int index = 0;
    int count = 0;
    int *spaceElements = 0;
    spaceElements = new int[length];                //Dynamically allocates  spaceElements memory

    for (count; count < length; count++)            //Gives number of white spaces in phrase
    {
        if (phrase.find(' ', count) != -1)
        {
        int space = phrase.find(' ', count);
        count = space;
        spaceElements[index] = space;
        index++;

        }
    }
    sizeOf = (index + 1);
    Word sentence[sizeOf];
    int start = 0;
    int end = 0;
    count = 0;

    //Copies, word by word, into Word array sentence
    for (count; count < sizeOf; count++)
    {
        for (count; count < index; count++)
        {
            end = spaceElements[count] - start;
            sentence[count].piglatin = phrase.substr(start, end); 
            start = spaceElements[count] + 1;
        }
        sentence[count].piglatin = phrase.substr(start, length);
    }

    //Testing***************************************************
    for (count = 0; count < sizeOf; count++)
        cout << endl << sentence[count].piglatin << endl;
    //**********************************************************

    delete [] spaceElements;

    Word *ptrToSet = sentence;             //assigns pointer to memory address of sentence array

    return ptrToSet;
}

Upvotes: 1

Views: 171

Answers (1)

Sam Varshavchik
Sam Varshavchik

Reputation: 118300

The pigLat() function instantiates sentence in the local function scope:

Word sentence[sizeOf];

PigLat() returns a pointer to the first element of this array:

Word *ptrToSet = sentence;
return ptrToSet;

However, once pigLat() returns, because sentence was a locally-scoped object, it goes out of scope and gets destroyed. Subsequent attempts to dereference the returned pointer is undefined behavior. This is the likely reason for your application crash. There may be other reasons too, I didn't look any further.

Upvotes: 1

Related Questions