943,724 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2570
  • C++ RSS
Oct 24th, 2008
0

Allocating an array of objects ?

Expand Post »
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?
Similar Threads
Reputation Points: 12
Solved Threads: 8
Junior Poster
gangsta1903 is offline Offline
111 posts
since May 2008
Oct 24th, 2008
0

Re: Allocating an array of objects ?

Don't know if it helps. I myself am not sure if it's ok to write it, but compiler doesn't complain:
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Oct 24th, 2008
0

Re: Allocating an array of objects ?

Click to Expand / Collapse  Quote originally posted by Sci@phy ...
Don't know if it helps. I myself am not sure if it's ok to write it, but compiler doesn't complain:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 12
Solved Threads: 8
Junior Poster
gangsta1903 is offline Offline
111 posts
since May 2008
Oct 24th, 2008
0

Re: Allocating an array of objects ?

Glad you've find answer. But this is what you asked (be careful what you ask ):
Quote ...
Is there another way to create an array of objects size of which is unknown without using new key(without malloc too)?
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Oct 25th, 2008
1

Re: Allocating an array of objects ?

Click to Expand / Collapse  Quote originally posted by Sci@phy ...
Don't know if it helps. I myself am not sure if it's ok to write it, but compiler doesn't complain:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008
Oct 25th, 2008
0

Re: Allocating an array of objects ?

Click to Expand / Collapse  Quote originally posted by grumpier ...
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
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Oct 25th, 2008
0

Re: Allocating an array of objects ?

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
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008
Oct 28th, 2008
0

Re: Allocating an array of objects ?

Click to Expand / Collapse  Quote originally posted by ddanbe ...
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?
Reputation Points: 12
Solved Threads: 8
Junior Poster
gangsta1903 is offline Offline
111 posts
since May 2008
Oct 28th, 2008
0

Re: Allocating an array of objects ?

I don't think so, ask the blog guy
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Make something move.
Next Thread in C++ Forum Timeline: std::copy a std::vector of std::pair to std::cout





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC