Non-pointer variables!!!

Dec 28, 2011 at 8:39pm
Hello!!!
I want to allocate memory dynamically to non-pointer variables(char,int) and later deallocate memory from non-pointer variables in c++...How can i do that?
Dec 28, 2011 at 10:20pm
(De)allocating an int dynamically:
1
2
int* i=new int;
delete i;


or just

int i;

You can't store addresses in non-pointer variables if that's what you mean.
(Edit: well, you can do it with an integer type large enough, but there's no good reason to do so)
Last edited on Dec 28, 2011 at 10:23pm
Dec 28, 2011 at 10:21pm
@Athar: He's trying to use the new keyword without pointers.

@hassaanid: I don't think it's possible, or if it is, it's definitely not advisable.
Dec 29, 2011 at 12:15am
This is just a terrible misunderstanding of what a pointer is and what new does. Once the OP understands these, he'll realise that this question makes no sense.
Last edited on Dec 29, 2011 at 12:35am
Dec 29, 2011 at 5:16am
Let's suppose we have two non pointer variables.. int x; char y; how can we allocate and deallocate memory for these two non pointer variables?
Dec 29, 2011 at 2:26pm
As told by Moschops, that is simply not in the programmer's reach. That is done by the compiler. Period. The instant you declare those variables and compile, you have the memory allocated. I am guessing the compiler will add code to deallocate the memory once the variables go out of scope. So there, that's the best you can do to control stack memory allocation: Control the scope of the variables.
Dec 29, 2011 at 4:40pm
Not sure if this is correct - it looks very risky and restrictive to me - personallyI use a pointer in its true form rather.

1
2
3
4
5
6
   int & a = *(new int());
   a = 5;
   
   cout << "a = " << a << endl;

   delete &a;
Dec 29, 2011 at 4:46pm

When you use new, you are not using it to allocate memory for the pointer that will point at that memory.

I'll repeat that.

When you use new, you are not using it to allocate memory for the pointer that will point at that memory.

Look at this code:

int* p = new int;

The new allocates memory for an int. For an int. It does not, repeat does not, allocate memory for the pointer p. The pointer is a complete object that exists on the stack; the memory it takes up does not, does NOT, get allocated by new. After this code, we now have TWO objects. A pointer called p, and an int that has no name that we get access to using the pointer p.

So, back to your question:

Let's suppose we have two non pointer variables.. int x; char y; how can we allocate and deallocate memory for these two non pointer variables?


Like this:

1
2
char* pointerOne = new char;
int * pointerTwo = new int;


The new here has allocated memory for an int, and memory for a char. The pointers are not, repeat not, allocated memory by new. The pointers are just how you get access to the memory new has allocated.

When you decide you want to deallocate them, you can use delete:

1
2
delete pointerOne;
delete pointerTwo;


However, you should only use new when you have to. Otherwise, just put them on the stack like this:

1
2
char variableOne;
int variableTwo;



Last edited on Dec 29, 2011 at 4:59pm
Topic archived. No new replies allowed.