?
Solved

Built executable exits before end of program!

Posted on 2003-10-24
8
Medium Priority
?
223 Views
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?
0
Comment
Question by:viperv80
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 6

Expert Comment

by:GaryFx
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?

Gary
0
 

Author Comment

by:viperv80
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
0
 
LVL 23

Expert Comment

by:brettmjohnson
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.

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:viperv80
ID: 9616592
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.
0
 
LVL 6

Accepted Solution

by:
GaryFx earned 200 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;

Declare
   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.

Gary
0
 

Author Comment

by:viperv80
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!
0
 
LVL 23

Expert Comment

by:brettmjohnson
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;
0
 
LVL 6

Expert Comment

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

Gary
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Suggested Courses

770 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