![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
|
Hi,
I'm having a problem with c++ - I have an object that needs to have as one of its variables an array - however, I won't know how big this array needs to be until instances of the class are constructed. So where I'm tripping up is over my confusion of arrays as pointers etc - probably quite a common problem! This is the relevant code I have in my header file (I've generalised it so when someone gives me a solution I'll understand better and not hassle you guys again): c++ Syntax (Toggle Plain Text)
And then in the .cpp file I have something like: c++ Syntax (Toggle Plain Text)
Thanks very much in advance for any help anyone can offer. ~deano |
|
|
|
|
|
#2 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
Re: How to make array with undefined size as object variable
>So where I'm tripping up is over my confusion of arrays as pointers etc
An array isn't a pointer and a pointer isn't an array. Anyway, C++ doesn't support variable length arrays. You have to provide a compile-time constant or don't use an array. One of the alternatives is to simulate an array by allocating memory to a pointer: c++ Syntax (Toggle Plain Text)
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Nov 2007
Posts: 86
Rep Power: 2
![]() |
Re: How to make array with undefined size as object variable
it seems as though you are actually tripping up on what happens to memory allocated on the stack after the function it was allocated for returns.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Doubling the size of an array | IceCold19 | C | 15 | Sep 16th, 2007 5:40 AM |
| changing size of an array | Eric the Red | Java | 3 | Apr 3rd, 2006 9:19 PM |
| Change the size of a character array dynamically? | bivhitscar | C | 23 | Nov 5th, 2005 6:55 AM |
| How to say the size of an array using printf ? | colt | C | 2 | May 19th, 2005 4:25 PM |
| Installing IPB 2.03 | bh4575 | Other Web Development Languages | 0 | Apr 23rd, 2005 3:36 AM |