Archived

This topic is now archived and is closed to further replies.

Simple Questions

This topic is 6360 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

All things declared for the Free Store must be in a pointer, right? int x = new int; // invalid? int *x = new int; // valid? And the only advantage of declaring stuff to the Free Store is that it can be dynamically declared, right? It claims, in Teach Yourself C++ in 24 Hours, that a big advantage is that the variables declared in the Free Store will persist after the function''s returning (which is true), but wouldn''t it be a lot easier just to define the local variable in the function as static? So the only advantage is dynamically declaring something, for example:
    
int array_size = 10;
int *array;

array = new int[array_size];
array_size++; // Does that work?

delete[] array;
    
Sorry for asking so many questions at once, but, here''s what I want to know: 1) Do things in the Free Store have to be declared with pointers? 2) Is the only advantage to the Free Store dynamic variables? 3) Can you change the value of the size of an array with which a variable in the Free Store was defined after the declaration? Thanks! - Goblin "In order to understand the full depth of mankind, you must first seperate the word into its component parts: 'mank' and 'ind'."

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hmm, sorry I''m not answering any of your questions directly, but, the best way to learn is from experience.

Try to look up some information on linked lists, I remember wondering about the usefulness of pointers myself, but lists made the concept *click* for me, maybe it''ll do the same for you.

Share this post


Link to post
Share on other sites
Yes, the ''Free Store'' is worked with via pointers. That''s just how it works.

With some C++ referencing, you can turn it into a real variable, but you need to keep track of the original pointer for when you release the memory allocated.

For example,
    
int* pInt; // Pointer to int

pInt = new int; // Allocate 4 bytes for an int

*pInt = 4; // assign 4 to *pInt


int& rInt = *pint; // Define rInt as a reference to *pInt

rInt = 5; // Change the 4 to a 5..


delete pInt; // Need pInt to free it..



So..
1) Do things in the Free Store have to be declared with pointers?

Pretty much, yeah. And you need to save the pointer so you can release what you took when you''re done.

2) Is the only advantage to the Free Store dynamic variables?

Well, dynamically allocated memory, yes. That''s what it is.

3) Can you change the value of the size of an array with which a variable in the Free Store was defined after the declaration?

I think you''re asking if you can allocate (say) 100 bytes and then later expand it to (say) 200 bytes. The only way to do this is to "re-allocate" the memory. In the C Runtime library, you can use ''realloc()'' but I don''t think it preserves the values of any data in the original buffer. So this is pretty much the same as freeing your original block and allocating a new block. Note that realloc() will change the size, but if you make it bigger then you may not get the same ''pointer'' back that you originally had [in fact, i would count on it changing.]

HTH,
// CHRIS

Share this post


Link to post
Share on other sites
realloc () gives you a new and different pointer so anything which pointed to it, say a node pointer in a binary tree, would have to be reassigned. This of course, would be a headache to manage.

You can''t write anything very complex without dynamic memory allocation.

Share this post


Link to post
Share on other sites