
-----------------------------------
Cancer Sol
Thu Apr 04, 2013 4:45 pm

Memory Allocation
-----------------------------------
Today, I was learning about Memory Allocation, using new and delete.
However&#65292;when I made a test program, the value wasn't what I wanted it to be.

#include 
using namespace std;

int main()
{
    int number;
    cin >> number;
    int *ptr_number = new int
What did I do wrong there?

-----------------------------------
Panphobia
Thu Apr 04, 2013 5:03 pm

RE:Memory Allocation
-----------------------------------
You just initialized a new array of integers of size number, you did not store the value of number in it.

-----------------------------------
Cancer Sol
Thu Apr 04, 2013 5:43 pm

Re: RE:Memory Allocation
-----------------------------------
You just initialized a new array of integers of size number, you did not store the value of number in it.
Oh.. the example in the book is kinda confusing then :/

How do I assign it the value of what I input then?

-----------------------------------
Panphobia
Thu Apr 04, 2013 5:50 pm

RE:Memory Allocation
-----------------------------------
use ( ) instead of [  ] and everything else the same

-----------------------------------
Cancer Sol
Thu Apr 04, 2013 7:53 pm

Re: RE:Memory Allocation
-----------------------------------
use ( ) instead of 
Thanks, it works now :D
What if I left it like that so it's an array, how do I assign it a value? Let's just say we'll assign it to [0]

-----------------------------------
Panphobia
Thu Apr 04, 2013 8:31 pm

RE:Memory Allocation
-----------------------------------
you can use a for loop from 0 to the array length and add values to all indexes of the array

-----------------------------------
Cancer Sol
Fri Apr 05, 2013 12:49 am

Re: Memory Allocation
-----------------------------------
Oh wait, actually I think I know how to do it now.
Still not too sure, but I'll just try it out tomorrow.

-----------------------------------
wtd
Fri Apr 05, 2013 1:08 am

Re: Memory Allocation
-----------------------------------
Quick tip:

[code]#include 
#include 

int main()
{
        int *foo = new int[10];

        std::fill(foo, foo + 10, 0);

        for (int i = 0; i < 10; i++)
        {
                std::cout 