Allocating an array of objects ?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2008
Posts: 54
Reputation: gangsta1903 is an unknown quantity at this point 
Solved Threads: 1
gangsta1903 gangsta1903 is offline Offline
Junior Poster in Training

Allocating an array of objects ?

 
0
  #1
Oct 24th, 2008
Hi,
assuming that Element is a class,I prefer to create an array of objects size of which is unknown in this way:
Element *elementArray = new Element[length];
Is there another way to create an array of objects size of which is unknown without using new key(without malloc too)?

I am asking this question, because I have to create this array in a function.As you know, a variable created in a function scope is automatically destroyed at the end of the scope.

On the other hand,when we use new,we have to use delete.I think that using new-delete in a function scope is meaningless,if there is a way not to use them.

So,what are your suggestions?
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: Allocating an array of objects ?

 
0
  #2
Oct 24th, 2008
Don't know if it helps. I myself am not sure if it's ok to write it, but compiler doesn't complain:
  1. void func(int n){
  2. int a[n];
  3. for (int i = 1; i <= n; i++) a[i+1] = i*i;
  4. for (int i = 1; i <= n; i++) std::cout<<a[i+1]<<std::endl;
  5. return;
  6. }
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 54
Reputation: gangsta1903 is an unknown quantity at this point 
Solved Threads: 1
gangsta1903 gangsta1903 is offline Offline
Junior Poster in Training

Re: Allocating an array of objects ?

 
0
  #3
Oct 24th, 2008
Originally Posted by Sci@phy View Post
Don't know if it helps. I myself am not sure if it's ok to write it, but compiler doesn't complain:
  1. void func(int n){
  2. int a[n];
  3. for (int i = 1; i <= n; i++) a[i+1] = i*i;
  4. for (int i = 1; i <= n; i++) std::cout<<a[i+1]<<std::endl;
  5. return;
  6. }
What I mean was using new and delete.
anyway, I learned the solution:local pointers should be handled before going out of scope.
Last edited by gangsta1903; Oct 24th, 2008 at 5:27 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: Allocating an array of objects ?

 
0
  #4
Oct 24th, 2008
Glad you've find answer. But this is what you asked (be careful what you ask ):
Is there another way to create an array of objects size of which is unknown without using new key(without malloc too)?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: Allocating an array of objects ?

 
1
  #5
Oct 25th, 2008
Originally Posted by Sci@phy View Post
Don't know if it helps. I myself am not sure if it's ok to write it, but compiler doesn't complain:
  1. void func(int n){
  2. int a[n];
  3. for (int i = 1; i <= n; i++) a[i+1] = i*i;
  4. for (int i = 1; i <= n; i++) std::cout<<a[i+1]<<std::endl;
  5. return;
  6. }
The array declaration int a[n]; is not valid C++. Your compiler doesn't complain because it supports this as an extension. (Your compiler might also support the 1999 C standard (where such things are valid)).

To answer the original question, though, it depends on what you mean by "not using new key (without malloc too)". If you mean that your code does not invoke operator new or call malloc() directly, then you can use a standard container (std::vector, std::list, etc).

However, those containers work by performing dynamic memory allocation behind the scenes ... which means that code you write may not employ operator new or malloc(), but the containers themselves might.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: Allocating an array of objects ?

 
0
  #6
Oct 25th, 2008
Originally Posted by grumpier View Post
The array declaration int a[n]; is not valid C++. Your compiler doesn't complain because it supports this as an extension. (Your compiler might also support the 1999 C standard (where such things are valid)).
So, that's the thing! I knew something was wrong with it, but still
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,923
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Allocating an array of objects ?

 
0
  #7
Oct 25th, 2008
In C# this would be easy.
Use jagged arrays!
But yeah if you're sticking to C++ you might want to read http://blog.voidnish.com/?p=16
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 54
Reputation: gangsta1903 is an unknown quantity at this point 
Solved Threads: 1
gangsta1903 gangsta1903 is offline Offline
Junior Poster in Training

Re: Allocating an array of objects ?

 
0
  #8
Oct 28th, 2008
Originally Posted by ddanbe View Post
In C# this would be easy.
Use jagged arrays!
But yeah if you're sticking to C++ you might want to read http://blog.voidnish.com/?p=16
it seems interesting...does it require to include any third party header files?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,923
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Allocating an array of objects ?

 
0
  #9
Oct 28th, 2008
I don't think so, ask the blog guy
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC