Thread: Another pointer problem

  1. #16
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    maybe creating functions load and unload would be a better idea

  2. #17
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    First, let's correct some errors in your initial code:

    Code:
    class abc {
    	public:
    		int* a;
    		abc() {
    			a = 0;
    		}
    		void func() {
    			a = new int[100000];
    			//do stuff...
    		}
    		~abc() {
    			delete[] a;
    		}
    };
    Second, Let's try and make your class obey simple data encapsulation rules

    Code:
    class abc {
            private:
                    int* a;
    	public:
    		abc() {
    			a = 0;
    		}
    		void func() {
    			a = new int[100000];
    			//do stuff...
    		}
    		~abc() {
    			delete[] a;
    		}
    };
    Now... let's do it your way...
    Code:
    class abc {
            private:
                    int* a;
    	public:
    		abc() {
    			a = 0;
    		}
    		void func() {
                            delete[] a;
                            a = 0;
    			a = new int[100000];
    			//do stuff...
    		}
    		~abc() {
    			delete[] a;
    		}
    };
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #18
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114
    Hmm, I think that I have found what the problem is, but I'm not sure yet. Thanks for the help everyone, but (as I'm not sure yet), I think that the preoblem for deallocation comes from the fact that I tried to allocate more than I had needed! I mean that my pointer needed like 300k of memory (100000*3bytes) but I realized that somewhere in my loops I had something like this:

    Code:
    a[400000] = ...;
    But 400 000 is an address that is higher than what I had asked for, so I tried skipping that loop where I got this "over-assignation" and then my errors disappeared. So then, could it be possible that seeking memory farther then what we had asked for might do crashes when it comes to allocating/deleting?

  4. #19
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I've yet to see a system where an int is 3 bytes. I've seen 2, most popular systems today are 4, but I haven't seen 3.
    If you understand what you're doing, you're not learning anything.

  5. #20
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I would suggest something like this:

    Code:
    class abc {
    	public:
    		int* a;
    		abc()
    		: a(0) {
    			
    		}
    		void func() {
    			alloc(100000);
    			//do stuff...
    		}
    		~abc() {
    			dealloc();
    		}
    		void
    		alloc(unsigned n) {
    			dealloc();
    			a = new int[n];
    		}
    		void dealloc() {
    			delete [] a;
    			a = 0;
    		}		
    };
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #21
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> But 400 000 is an address that is higher than what I had asked for.
    Array indexes are just that, indexes, not addresses. If you create an array with 100000 spaces, then only indexes 0 through 99999 are allowed. It doesn't matter how many bytes an int is, the valid indexes are still the same.

  7. #22
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114
    I was always talking about "int"s, but in fact it's true, ints don't take 3bytes, but its because I said I used ints to keep it simple, but in reality, I don't use them, I use a 3 bytes class consisted of 3 chars, so that's were all the threes come from.

    But for the "array", it's not about being a "defined" array (not dynamic, NEW-ed), it's because I just used the (int again )
    Code:
    int[some_number]
    to represent that is was an address of type int, where we added the part of address [some_number], so it's not an array that I declared with a specific capacity...

    Sorry for all the misunderstanding I might have caused!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 08:53 PM
  2. Pointer problem
    By mikahell in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2006, 10:21 AM
  3. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. pointer problem
    By DMaxJ in forum C Programming
    Replies: 4
    Last Post: 06-11-2003, 12:14 PM