Thread: Create Array size Question

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    1

    Unhappy Create Array size Question

    Hi everybody,

    how can i create empty array? That means I want to create a array variable without specify the size of the array.
    In my program, I wrote:

    n1 = q - p + 1;
    n2 = r - q;

    int L[n1], R[n2];

    Unfortunately, c++ only allow constant inside the bracket of the array.
    I know it's possible in Java to write :
    int L[]; So, can somebody tell me how to do that in c++?

    Thanks

    From Allen

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    You declare a pointer and use "new" to create the array at runtime

    Like so

    Code:
    #include <iostream>
    
    
    int main(){
    
    	int *ptr = 0;
    	int p = 10,q = 50;
    	
    	int n1 = q - p + 1;
    	
    	ptr = new int[n1];
    	if(!ptr){/*Error handling*/}
    		
    	for(int i = 0;i < n1;++i)
    		ptr[i] = i * 2;
    		
    	for(int i = 0;i < n1;++i)
    		std::cout << ptr[i] << " ";
    	
    	delete[] ptr;	
    }

  3. #3
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719

    Be Careful

    Be Careful when using 'new'. It creates the memory allocations for the array in the heap, so if your heap isnt large enough the heap will begin getting inconsistent nodes and your application will likely crash. I also reccommend using calloc, malloc, and free. Or you could also just increase the size of the heap.

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793

    Re: Be Careful

    Originally posted by Xei
    Be Careful when using 'new'. It creates the memory allocations for the array in the heap, so if your heap isnt large enough the heap will begin getting inconsistent nodes and your application will likely crash. I also reccommend using calloc, malloc, and free. Or you could also just increase the size of the heap.
    :: Slaps forehead ::

    calloc and malloc also use the heap - infact many implementations of new call malloc.....

    The heap size recomended by your linker (assuming windows - linux may work differently) is usually enough........you have to be using an awful lot of memory for a heap to be too small

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 11:27 AM
  3. templates, unresolved external error
    By silk.odyssey in forum C++ Programming
    Replies: 9
    Last Post: 06-09-2004, 04:39 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. Class member array size with constuctor initalizer
    By rafe in forum C++ Programming
    Replies: 2
    Last Post: 10-14-2002, 10:09 AM