Link to home
Start Free TrialLog in
Avatar of viperv80
viperv80

asked on

Built executable exits before end of program!

hello there,
I have compiled a program that finds the fog index (readability) of a text file. When I execute the code from the compiler everything is fine, except from a warning about returning a local array.
The problem is when I try to run the built executable in both debug and release version.
It just executes the first few statements which are to ask the name of the text file to be read and then the console window just disappears (no error message).
Any clue why is this happening?
Could it be the warning I get in compile time?
Avatar of GaryFx
GaryFx

What do you mean "execute the code from the compiler"?  Compilers don't execute code, they create code.  You need some other mechanism to execute the code.

Returning a local array is likely to be a very serious problem.

Try setting breakpoints in debug mode to see how far you get before the problem occurs.

Finally, what platform?

Gary
Avatar of viperv80

ASKER

Sorry I kinda wrote that post in a hurry...

what I meant was that when I compile the code and run the code from Visual Studio everything is fine. The program completes and the local array is returned fine.
But when I try to run the executable that is created from the dos console, only the first few statements of the code seem to be executedand then the console window exits without displaying the results as it does when run from inside visual studio.

This is C code run on windows XP pro.

Thanx a lot for the fast response
Can you post the exact compiler warning and the function that generates it?

If your function looks something like this:

char * somefunc(...) {
      char returnthis[100];
      ...
      return (returnthis);
}

Then you indeed have a problem.  The returnthis array is a local variable whose
scope is limited to somefunc().  Once you return from somefunc(), local variables
go out of scope and are no longer valid.  So the pointer returned points to some
location on the stack that will get clobbered as soon as anything uses the stack.

Yes this is similar to the function I have.
[code]
int* syllable_count(char* sentence)
{
      char *ptrSentence;
      char vowels[] = {'a','e','i','o','u','y','\0'};
      int syllables[2];
      int total_syllables = 0;
      int many_syllables_words = 0;
      int position = 0;
      int i = 0;
      ptrSentence = sentence;

      while(*ptrSentence != '.' && *ptrSentence != ';' && *ptrSentence != '!' && *ptrSentence != '?')
      {
            if(*ptrSentence != ' ')
            {
                  for(i = 0;i < 6 ;i++)
                  {            
                        if(sentence[position] == vowels[i] || sentence[position] == toupper(vowels[i]))
                        {
                              if(sentence[position + 1] != vowels[i])
                              {
                                    if(*ptrSentence != 'e' || sentence[position + 1] != ' ' && sentence[position + 1] != '.' &&
                                    sentence[position + 1] != '!' && sentence[position + 1] != '?' && sentence[position + 1] != ';')
                                          total_syllables++;
                              }
                        }
                  }
            
            }
            else
            {
                  if(total_syllables > 2)
                        many_syllables_words++;
            }
            position++;
            *ptrSentence++;
      }
      syllables[0] = total_syllables;
      syllables[1] = many_syllables_words;
      return syllables; //WARNING ON THIS LINE!!!
}
[/code]
and this is the warning:

warning C4172: returning address of local variable or temporary

any clue on how I could rewrite this function?

The thing is that I need to return both "total_syllables" & "many_syllables_words" so I thought of putting them into an array and returning that array.
ASKER CERTIFIED SOLUTION
Avatar of GaryFx
GaryFx

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I changed the array into a structure and I get no warnings any more (thanks for that), BUT
I still have the same problem with the executable!
Read my original post.  If counts is an array (or structure) that is a local stack
variable to syllable_count() you will not be able to return a pointer to that
structure because it will cease to exist when the function return is executed.
You need to do one of the following:

1) have the caller supply the memory for the structure like
      struct counts cnt;
      syllable_count(sentence, &cnt)

2) allocate the returned memory inside syllable_count() and trust the caller to free it when done. In syllable_count():
      struct counts *cnt = (struct counts *)malloc(sizeof(struct counts));
      ...
      return cnt;

3) make the structure a static variable rather than dynamic local variable.  This is easiest for you to do, but is not reentrant.  In syllable_count():
      static struct counts cnt;
      ...
      return cnt;
My suggestion and code samples show returning a struct, which is ok, and not a pointer to struct.

Gary