Page 2 of 2 FirstFirst 12
Results 11 to 20 of 20

Thread: C problem

  1. #11
    I faced the same problem that smirc has faced only half the time it crashes before even running. I'm using Bloodshed so you know.

  2. #12
    Senior Member
    Join Date
    Aug 2001
    Posts
    356
    hmm...i'm gonna have to go smack arround a few of my past teachers cause i've never been told anything about that. i wonder if thats cause its less of an issue now than it used to be, what with more RAM and processing power so the speed that the commands get exicuted and the ammount of space the chances are only miniscule that it will get overwritten.

    maybe more modern compilers wait till after the return value has been copied to release the memory? i dunno, just shooting in the dark cause i find it so odd that its never been mentioned by any teachers or in any books i've had thusfar.
    -8-

    There are 10 types of people in this world: those who understand binary, and those who dont.

  3. #13
    Senior Member
    Join Date
    Oct 2001
    Posts
    385
    smirc: hmm...interesting...guess I'll use globals instead of returns most of the time now...
    8*B@LL: I have those lines in there because I'm printing all chars from -127 to 255

    anyway, I did some changes in the code.
    In DJGPP: Now I'm getting the same error on options 1 and 2: General Protection Fault
    SIGSEGV.
    In Visual C++ Intro. Ed., I'm not getting any errors, so I expect it's the DJGPP compiler now (book versions..so untrustworthy)


    here's the new file:
    Preliminary operational tests were inconclusive (the dang thing blew up)

    \"Ask not what the kernel can do for you, ask what you can do for the kernel!\"

  4. #14
    The Iceman Cometh
    Join Date
    Aug 2001
    Posts
    1,209
    Just a suggestion for the process choice procedure: I would recommend using a switch rather than a if-else statement, like this:

    Code:
    int ProcessChoice(int choice)
    {
      switch (choice)
      {
        case 1: numToChar(); break;
        case 2: charToNum(); break;
        case 3: return 0;
        default:
          printf("\nPlease enter a valid choice.\n");
          break;
      }
      return 1;
    }
    Also, avoid using global variables at all costs. You had the right idea in your first program, locally assigning choice and x within the functions/procedures. If my students ever use global variables without expressly being told to, they get docked hard because it's a very sloppy way of coding. Go back to the way you had it before... it's much more efficient and easier to read. Generally, for my beginning students, I tell them to only use global variables when they have a constant that they need to use throughout the program (such as PI). Otherwise, stick with using local variables and passing either by reference or by value. If you don't want to return values, you should learn to pass by reference rather than value, to solve the memory management issues.

    AJ

  5. #15
    Senior Member
    Join Date
    Oct 2001
    Posts
    385
    thanks, but I haven't got up to switches yet...looks much better though

    nor am I up to referencing and such.
    Preliminary operational tests were inconclusive (the dang thing blew up)

    \"Ask not what the kernel can do for you, ask what you can do for the kernel!\"

  6. #16
    The Iceman Cometh
    Join Date
    Aug 2001
    Posts
    1,209
    Here try this program (I just modified your code... cleaned it up some... took out the global variable's, fixed up some of the functions/procedures. Try compiling this with your program and see if it works:

    AJ

    EDIT: Even if your not up to refencing and switches, etc., you should still get into good programming habits. Globals are bad practice (nearly any instructor will tell you so). Are you following a book or something? I find it strange that you are learning about functions/procedures, etc. without ever having covered switches. I generally cover switches a week or two after if-else statements, which is still a few weeks away from procedures and functions.

    Also, in response to smirc's posts. When I teach C++, I tend to tell the students that they should use return values whenever they only have to return one value from a function. Although you have a good point, malloc and calloc are not exactly "beginner" material. In fact, they are only covered very briefly in one of the upper division electives at my campus. If students write a procedure which requires more than one value to be returned, I tell them to use pass-by-reference. Although these do not hold true all of the time, generally students will find it easier to understand their own programs when done this way, and once they have gotten up to much more advancted programming techniques, they generally find their own ways of solving memory allocation issues.

  7. #17
    Senior Member
    Join Date
    Oct 2001
    Posts
    385
    avdven: your code fixed the problem with choice 1, however I'm still getting the same problem with choice two. Thank you very much for your help.

    EDIT: fixed it, changed char x to int x.

    thanks for your help everyone!

    I believe what happened was improper use of variables with scanf() and printf. For example, at the last function in conversion.txt from avdven, it probably reads the wrong place when it tries to use char x, since a char is smaller than an int, resulting in stack errors of some kind.
    Preliminary operational tests were inconclusive (the dang thing blew up)

    \"Ask not what the kernel can do for you, ask what you can do for the kernel!\"

  8. #18
    The Iceman Cometh
    Join Date
    Aug 2001
    Posts
    1,209
    The reason you're having these problems is because (apparently) the compiler in question is very specific about memory management. You had it for the first choice because because you were using a short and needed an integer (also, you were wasting memory with your loops). In the second one, you are using a character x, and waiting for the user to enter a string (%s). To solve this, you can change your function back to how it was originally:

    Code:
    void charToNum(void)
    {
      char x[1];
      x[0] = NULL;
    
      printf("\nCharacter: ");
      scanf("%s", &x);
    
      printf("\nYour character is: %c", x[0]);
      printf("\nYour number is:    %d", x[0]);
    }
    That should do it for ya.

    AJ

  9. #19
    Senior Member
    Join Date
    Oct 2001
    Posts
    385
    most of the problem came from me not being aware of the %s modifier for scanf(), I think

    that just led to everything else. my ineffeciency, well that's what you get when you're learning on your own (I really wish my schools would get a programming class)

    again, thank you all
    Preliminary operational tests were inconclusive (the dang thing blew up)

    \"Ask not what the kernel can do for you, ask what you can do for the kernel!\"

  10. #20
    Senior Member
    Join Date
    Aug 2001
    Posts
    356
    Originally posted here by Kezil
    8*B@LL: I have those lines in there because I'm printing all chars from -127 to 255
    i'm guessing you've already figured this out, but incase you havent mine works just fine. it doesnt matter if the number is positive, negitive, or really in what range it is.

    for instance, if you did this:
    Code:
    for(i = -1000; i<1000; i++)
          printf("\ncurrent char is: %c", i);
    what you would see was the entire ascii table over and over and over.
    -8-

    There are 10 types of people in this world: those who understand binary, and those who dont.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •