Like Tree1Likes
  • 1 Post By whiteflags

While Loop Kills my Pointer Value. Please help.

This is a discussion on While Loop Kills my Pointer Value. Please help. within the C Programming forums, part of the General Programming Boards category; Using gdb, I know the while loop kills my pointer value when the check condition is no longer true. Why ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    33

    While Loop Kills my Pointer Value. Please help.

    Using gdb, I know the while loop kills my pointer value when the check condition is no longer true. Why is this, and how can I remedy this? My main function just prints 124231523 124323232. Main btw, just makes a size 10 array with numbers 0-9 for respective addresses in the matrix. My extreme function is supposed to print the least and greatest numbers in the array 0 and 9. Please help.

    Code:
    # include <stdio.h>
    # include<stdlib.h>
    void extreme( int ar[], int count, int *most, int *least );
    
    int main ()
    
    {
    
      int ar[10], i = 0, *most, *least;
    
      most = (int *)malloc(sizeof(int));
      least = malloc(sizeof(int));
      do
      {
        ar[i] = i;
        i++;
      } while ( i < 10 );
    
      extreme( ar, i, most, least );
      printf("%i %i\n", most, least);
      return(0);
    }
    
    void extreme( int ar[], int count, int *most, int *least )
    {
    
      int i = 0;
    
      *most = ar[i];
      *least = ar[i];
      while ( i < count )
      {
        i++;
        if ( ar[i] > *most )
           *most = ar[i];
        else
        if ( ar[i] < *least )
           *least = ar[i];
      }
    }
    The while loop is at line 34. Thanks!
    Last edited by Kurtanius21; 11-15-2012 at 02:21 PM.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,527
    [edit] Rather, pointers have addresses as values. If you mean the object they point to you have to dereference. [/edit]
    Code:
    void extreme( int ar[], int count, int *most, int *least );
    One thing to keep in mind is that a pointer is not required to be used by the calling code only because the function accepts pointers. The main function can also pass in the address of local variables instead.
    Last edited by whiteflags; 11-15-2012 at 02:25 PM.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    33
    I'm not giving the pointers something to point to at line 29? If that's not the case, doesn't malloc give the pointers something to point to?

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,527
    Quote Originally Posted by Kurtanius21 View Post
    I'm not giving the pointers something to point to at line 29? If that's not the case, doesn't malloc give the pointers something to point to?
    OK, I made a mistake.

    Reading your code more carefully I made edits to my last post, so refer to that.

    The problem is most likely here, where you meant to write
    Code:
    printf("%i %i\n", *most, *least);
    If you don't write that, the result looks like a memory address. Except that's not even how you print memory addresses.
    stahta01 likes this.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    As whiteflags said, the way you calling the function from main seems to be the problem.

    Also why do you cast what malloc returns?

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    33
    I entered in your suggested modification Whiteflags. Least gives me zero. It works. Most is still faulty. here's what gdb reads.

    37 if ( ar[i] > *most )
    1: *most = 9
    (gdb) s
    38 *most = ar[i];
    1: *most = 9
    (gdb) s
    34 while ( i < count )
    1: *most = 134520856

    I removed the type cast at malloc for most. No help.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    2,372
    Fence post error you can run off end of array with i equal to count.

    Code:
      while ( i < count )
      {
        i++;
    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    33
    So if I add to the while loop check condition

    Code:
    while ( i < count - 1 )
    I get the value 8. But 9 exists at the end of the array. gdb showed that. How can I get the 9 to print?

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    Maybe i am wrong, but are you sure you want to call the function like this?
    Code:
    extreme( ar, i, most, least );
    Normally we pass addresses like this
    Code:
    extreme( ar, i, &most, &least );

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    2,372
    Quote Originally Posted by Kurtanius21 View Post
    So if I add to the while loop check condition

    Code:
    while ( i < count - 1 )
    I get the value 8. But 9 exists at the end of the array. gdb showed that. How can I get the 9 to print?
    Edit2: Write a normal loop with the increment at the end; instead of subtracting one from count.

    Edit: Use a valid for loop

    Tim S.
    Last edited by stahta01; 11-15-2012 at 03:19 PM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    2,068
    Quote Originally Posted by std10093 View Post
    Maybe i am wrong, but are you sure you want to call the function like this?
    Code:
    extreme( ar, i, most, least );
    Normally we pass addresses like this
    Code:
    extreme( ar, i, &most, &least );
    They're already pointers.

    Code:
    int ar[10], i = 0, *most, *least;

  12. #12
    Registered User
    Join Date
    Oct 2012
    Posts
    33
    That worked! thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program that kills itself
    By sangi in forum C++ Programming
    Replies: 3
    Last Post: 11-18-2004, 06:00 AM
  2. closing the Save_As dialog kills the application?!?
    By chanyees in forum Windows Programming
    Replies: 2
    Last Post: 05-02-2004, 07:15 PM
  3. Neck bomb kills Pizza man. WTF?
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 09-03-2003, 07:40 PM
  4. A program that kills itself...and questions
    By Shadow in forum C Programming
    Replies: 5
    Last Post: 07-25-2002, 07:08 PM
  5. MSVC++ Debugger (it kills me)
    By lightatdawn in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 06-29-2002, 07:37 PM

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