Thread: pointer problems (probably something dumb)

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    2

    pointer problems (probably something dumb)

    i'm new to serious programming and have been stumped by this weird behavior all morning

    in a nutshell: i pass a pointer in main to a public class function. it sets the pointer to point at a private dynamical class array and returns the array's length. inside the function, everything is fine -- i can access the elements of the array using the pointer. but once i'm back in main, i try to access the values using the pointer, but i see garbage.

    i will try and attach the relevant bits of code.

    -------------------------------------------------------------------

    from "synapses.h"

    Code:
    class synapses {
    
     private:
      
      int actcount[SIZE]; //contains the current length of each dynamical array actsyn[i]
      int *actsyn[SIZE]; //each of these points to a dynamical array initialized with "new"
      //SIZE is a global int
    
     public:
      int getpost(char p, int pre, int *post);
      //More declarations....
    
    };
    int synapses::getpost(char p, int pre, int *post){
      
      int res = -1;
      switch(p){
      case 'a':
        post = actsyn[pre]; //set pointer to point at dynamical array initalized with "new"
        res=actcount[pre]; //length of dynamical array
        break;
      case 's':
        post = supsyn[pre];
        res=supcount[pre];
        break;
      default: cout<<"synapses.getpost called with invalid char arg"<<endl;
        exit(1);
      }
      for (int i=0; i<res; i++){
        cout<<post[i]<<" ";  //**This statement prints the expected values 
      }
    
      return res;
    }
    from "main"

    Code:
    	  int *wack;
    	  int foo = (*connectivity).getpost('a',goo,wack); //goo is an int
              //connectivity is a pointer to a synapses object
    	  for(int k = 0; k<foo; k++){
    	    cout<<wack[k]<<" ";  //**This is the statement that prints garbage
    	  }

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by philoroggae View Post
    in a nutshell: i pass a pointer in main to a public class function. it sets the pointer to point at a private dynamical class array and returns the array's length. inside the function, everything is fine -- i can access the elements of the array using the pointer. but once i'm back in main, i try to access the values using the pointer, but i see garbage.
    In other words, your pointer is pointing to what it was prior to calling the function. Pointers are passed by value, so if you want to reset the pointer in the function, make the function take an int** (pointer to pointer to int).

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Pointers are passed by value, so if you want to reset the pointer in the function, make the function take an int** (pointer to pointer to int).
    Since this is C++, a simpler change would be to pass your pointer by reference.

    Incidentally, you can write connectivity->getpost('a',goo,wack) instead of (*connectivity).getpost('a',goo,wack).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Shows you how much I use raw pointers - I'm still "Thinking in C" regarding them...

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    2

    thanks!

    it works now. thanks for helping me out!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with passing an array of structures by pointer
    By raptor1770 in forum C Programming
    Replies: 9
    Last Post: 11-29-2008, 11:01 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. base class pointer problems
    By ... in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2003, 11:27 PM