emptying memory when coming from a function

This is a discussion on emptying memory when coming from a function within the C Programming forums, part of the General Programming Boards category; I'm sure that there's been a thread with this question before (more or less), but I couldn't find it. My ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    207

    emptying memory when coming from a function

    I'm sure that there's been a thread with this question before (more or less), but I couldn't find it.

    My program has a function that shows the values in a project. When I call this function (caracteristics) it runs perfectly, but as soon as it goes back to main, there's something wrong. A menu should be displayed (calling the function MENU()), but it displayed twice, and also, when I enter an option, it doesn't work correctly.

    I thought that maybe I should have the memory emptied, when I finish the caracteristics() function, but I don't know how (this is actually what I've been looking for).

    I attach the whole code (which compiles and runs), and I post the function caracteristics() as well (in case you don't want to download the whole thing, even it's quite small).

    There it goes:

    Code:
    void
    caracteristics(void)
    {
    
       double getval;  // this variable GETs the VALues from the existing project
       char  file_name[PATH_MAX];
       FILE *input;
    
    
       clrscr();
       strcpy (file_name, actual);
    
       input = fopen(file_name, "r");
    
    
       if (( input = fopen(file_name, "r+")) == NULL ) // check it for errors
           {
           printf("There was an error reading from the file!! \n");
           printf("Press any key to continue ");
           getch();
           main();
    
           }
    
       printf("The caracteristics of the actual project are: \n");
    
       fscanf(input, "%lf\n", &getval);
       printf("1) LOA: %.2f\n", getval);
       fscanf(input, "%lf\n", &getval);
       printf("2) I: %.2f\n", getval);
       fscanf(input, "%lf\n", &getval);
       printf("3) J: %.2f\n", getval);
       fscanf(input, "%lf\n", &getval);
       printf("4) P: %.2f\n", getval);
       fscanf(input, "%lf\n", &getval);
       printf("5) E: %.2f\n", getval);
       fscanf(input, "%lf\n", &getval);
       printf("6) Displacement: %.0f\n", getval);
       fscanf(input, "%lf\n", &getval);
       printf("7) GM: %.3f\n", getval);
       fscanf(input, "%lf\n", &getval);
       printf("8) H1: %.2f\n", getval);
       fscanf(input, "%lf\n", &getval);
       printf("9) H2: %.2f\n", getval);
       fscanf(input, "%lf\n", &getval);
       printf("10) H3: %.2f\n", getval);
       fscanf(input, "%lf\n", &getval);
       printf("11) S1: %.2f\n", getval);
       fscanf(input, "%lf\n", &getval);
       printf("12) S2: %.2f\n", getval);
       fscanf(input, "%lf\n", &getval);
       printf("13) S3: %.2f\n", getval);
       fclose(input);
    
       printf("\n\n		Press any key to continue");
       getch();
    
       main();
    
    }
    Attached Files Attached Files

  2. #2
    Sayeh
    Guest
    Why are you calling 'main()' from within your function? That isn't how you bail out of a function.

    In caracteristics(), the first time you do this, you should instead have called- 'return;'.

    The second call in caracteristics(), is unnecessary.

    Do what you are doing might trash your stackframe.

    --

    Why do you call 'caracteristics()' from within 'new_project', when if you returned to main(), it would call it there?

    --

    Instead of doing an explicit check for choice '5' in main, you should create another variable called 'quitFlag', a boolean. Set it to false before you enter your while. Then put choice '5' in your switch/case. If they hit 5, then set quitFlag to TRUE, and let your application exit gracefully.

    ---

    You have some more fundamental stuff to learn before you try writing a menu system---

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    207
    >Why are you calling 'main()' from within your function? That isn't >how you bail out of a function.
    I guess I've misunderstood someone then, I tought that was the proper way. What is the meaning of "bail out of a function"? My English isn't perfect.

    >In caracteristics(), the first time you do this, you should instead >have called- 'return;'

    OK, now I've changed it now. But I don't see any difference.

    > The second call in caracteristics(), is unnecessary.
    What second call?

    >Do what you are doing might trash your stackframe
    What's the result of this?

    >Why do you call 'caracteristics()' from within 'new_project', when if you returned to main(), it would call it there?

    Sorry, I didn't see it. It's changed now.

    >Instead of doing an explicit check for choice '5' in main, you >should create another variable called 'quitFlag', a boolean. Set it >to false before you enter your while. Then put choice '5' in your >switch/case. If they hit 5, then set quitFlag to TRUE, and let your >application exit gracefully.
    I don't really know how to do it, so if I can get away without it, I'll leave it for later. I'll keep it in mind.

    >You have some more fundamental stuff to learn before you try >writing a menu system---

    I completely agree with you, I wish I had the time to do it, but I have to finish it for uni. If I had money I'd pay someone to do it for me! I'm going to keep on with C after I pass the subject anyway, because I enjoy it.

    I really appreciate your time, and I'm sorry I couldn't understand most of your points (because of my low knowledge of C and the difficulty of the English as well).

    Really, thanks a lot.

  4. #4
    Former Member
    Join Date
    Oct 2001
    Posts
    955
    I think I got it, it the function MENU, you have fgets on stdin, it's supposed to work, but for some strange reason it doesn't.

    use scanf instead!

    try

    Code:
    scanf("%s",buff);
    I tried it here and it works!

    Oskilian

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21