Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 7th, 2008, 2:08 PM   #1
deanosrs
Programmer
 
deanosrs's Avatar
 
Join Date: Apr 2006
Location: Bristol, UK
Posts: 31
Rep Power: 0 deanosrs is on a distinguished road
Send a message via MSN to deanosrs
Question How to make array with undefined size as object variable

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)
  1. ...
  2. class image
  3. {
  4. public:
  5. ...
  6. int arrayName[];
  7. ...
  8. };
  9. ...

And then in the .cpp file I have something like:

c++ Syntax (Toggle Plain Text)
  1. image::image(string input)
  2. {
  3. // read in file containing image data determined by string
  4. // now put this data into array
  5. int newArray[(imgWidth * imgHeight)];
  6.  
  7. // now need to make the object's variable point to this local function variable
  8. ???!!
  9. }

Thanks very much in advance for any help anyone can offer.

~deano
deanosrs is offline   Reply With Quote
Old Jan 7th, 2008, 2:23 PM   #2
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
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)
  1. int *arrayName;
  2.  
  3. // In the constructor:
  4. arrayName = new int[imgWidth * imgHeight];
  5.  
  6. // In the destructor:
  7. delete [] arrayName;
A better alternative is to use the std::vector class instead. It handles the memory for you.
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Old Jan 7th, 2008, 2:57 PM   #3
mbd
Programmer
 
Join Date: Nov 2007
Posts: 86
Rep Power: 2 mbd is on a distinguished road
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.
mbd is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:12 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC