• C

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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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?

viperv80Author Commented:
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.

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

viperv80Author Commented:
Yes this is similar to the function I have.
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] != ';')
                  if(total_syllables > 2)
      syllables[0] = total_syllables;
      syllables[1] = many_syllables_words;
      return syllables; //WARNING ON THIS LINE!!!
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.
Avoid using arrays for cases where the elements don't have very similar meanings.  Is this case, both elements are counts, but they're counting different things, and therefore shouldn't be in an array.

Instead, use a struct:
   typedef struct counts {
         int total_syllables;
         int multisyllable_words;
    } Counts;

   Counts counts;
in your function, and change the function header to be

   Counts syllable_count(char* sentence) ...

Then you'll naturally change the assignments.  Instead of syllables[0] you'll use counts.total_syllables, and instead of syllables[1] you'll use counts.multisyllable_words.  Finally, instead of returning syllables, return counts.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
viperv80Author Commented:
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.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.