Built executable exits before end of program!

Posted on 2003-10-24
Last Modified: 2010-04-15
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?
Question by:viperv80
  • 3
  • 3
  • 2

Expert Comment

ID: 9614529
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?


Author Comment

ID: 9614881
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
LVL 23

Expert Comment

ID: 9615880
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.


Author Comment

ID: 9616592
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.
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails


Accepted Solution

GaryFx earned 50 total points
ID: 9617118
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.


Author Comment

ID: 9617341
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!
LVL 23

Expert Comment

ID: 9618584
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;

Expert Comment

ID: 9619366
My suggestion and code samples show returning a struct, which is ok, and not a pointer to struct.


Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now