| | |
Allocating an array of objects ?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2008
Posts: 54
Reputation:
Solved Threads: 1
Hi,
assuming that Element is a class,I prefer to create an array of objects size of which is unknown in this way:
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?
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?
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)
void func(int n){ int a[n]; for (int i = 1; i <= n; i++) a[i+1] = i*i; for (int i = 1; i <= n; i++) std::cout<<a[i+1]<<std::endl; return; }
•
•
Join Date: May 2008
Posts: 54
Reputation:
Solved Threads: 1
•
•
•
•
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)
void func(int n){ int a[n]; for (int i = 1; i <= n; i++) a[i+1] = i*i; for (int i = 1; i <= n; i++) std::cout<<a[i+1]<<std::endl; return; }
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.
•
•
Join Date: Aug 2008
Posts: 206
Reputation:
Solved Threads: 31
•
•
•
•
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)
void func(int n){ int a[n]; for (int i = 1; i <= n; i++) a[i+1] = i*i; for (int i = 1; i <= n; i++) std::cout<<a[i+1]<<std::endl; return; }
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.
•
•
•
•
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)).
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
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
Make love, no war. Cave ab homine unius libri.
Danny
•
•
Join Date: May 2008
Posts: 54
Reputation:
Solved Threads: 1
•
•
•
•
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
![]() |
Similar Threads
- Safe Array (C++)
- Allocating Memory for a huge array (C++)
- reasons why malloc fails? (C)
- C++ Reading from a text file (C++)
- C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays (C++)
- Pointers (C++)
- Pointers (Part II) (C)
Other Threads in the C++ Forum
- Previous Thread: Make something move.
- Next Thread: std::copy a std::vector of std::pair to std::cout
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






