Thread: dynamic array created in another method..

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    266

    dynamic array created in another method..

    Can anyone explain to me why when I use the new prefix on a pointer in another method than I created it, it does not work as intended?

    Code:
    #include <iostream>
    void magic(int *T, int sz)
    {
    	T = new int[sz];
    	for(int i=0;i<sz;++i)
    		T[i] = i;
    }
    
    int main()
    {
    	int *T;
    	magic(T , 10);
    	for(int i=0;i<10;++i)
    		std::cout << T[i] << " ";
    }
    output is random numbers from memory.

    does T no longer point to the start of array when the method magic reaches it end?

    Thanks!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That is correct. T no longer points to the start of the new array when the magic function ends. If you want changes to a variable to not disappear, you need to pass by reference, i.e., with
    Code:
    void magic(int* &T, int sz)

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    ah didnt catch that, I thought that when I passed a pointer that it would behave like that, thanks!

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Pointers aren't magic; they are subject to the same call-by-value semantics as everything else. (You can use that pointer to do magic with what it already points to, but you cannot try to make it point somewhere else, as that is just changing the local copy and not the pointer back in the original calling function.)

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    is there anything i should worry about when I pass a reference to a pointer like that ?

    I am trying to use similar code and I am getting crazy error about malloc fail,

    Any idea? i will post snippit of my actual code that does receive error on its own.

    compiles fine but has run-time error

    Code:
    #include <string>
    
    #include <iostream>
    using namespace std;
    void magic(int* &mpNext, string x)
    {
    	int m = x.length();
    	int i, j;
    	mpNext = new int[m];
    	for(int i=0;i<m;++i )
    		 mpNext[i] = 0;
    
    	i = 0;
    	j = mpNext[0] = -1;
    
    	while (i < m) {
    		while (j > -1 && x[i] != x[j])
    			j = mpNext[j];
    		mpNext[++i] = ++j;
    	}
    }
    
    
    int main()
    {
    	int *T;
    	string hay;
    	while(cin >> hay)
    	{
    		magic(T,hay);
    		cout << "method ran well" << endl;
    	}
    
    return 0;
    }
    input:
    Code:
    rod
    blue
    cpp
    output:
    Code:
    method ran well
    method ran well
    a.out: malloc.c:3074: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
    Aborted
    I am able to print out method ran well but then gives me big malloc error
    Last edited by rodrigorules; 04-03-2010 at 10:24 PM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Since you ask for m numbers, m-1 is the largest index you can use. You are guaranteeing yourself one overrun -- the last go-round of the loop ++i will equal m and you will go over. It's possible j is fine, but you may want to double-check that (there's certainly no check in your code that j won't all of a sudden be 235000).

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    thank you tabstop, you are great !

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    why not to use std::vector instead of dynamic array?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic array allocation
    By Maulrus in forum C++ Programming
    Replies: 7
    Last Post: 03-09-2010, 06:08 PM
  2. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  3. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  4. Dynamic Array Resizing
    By dld333 in forum C++ Programming
    Replies: 13
    Last Post: 11-04-2005, 12:13 AM
  5. Dynamic 2d array
    By Mithoric in forum C++ Programming
    Replies: 8
    Last Post: 12-29-2003, 09:19 AM