Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Memory Allocation
Index -> Programming, C++ -> C++ Help
Goto page 1, 2, 3, 4  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Cancer Sol




PostPosted: Thu Apr 04, 2013 4:45 pm   Post subject: Memory Allocation

Today, I was learning about Memory Allocation, using new and delete.
However,when I made a test program, the value wasn't what I wanted it to be.
c++:

#include <iostream>
using namespace std;

int main()
{
    int number;
    cin >> number;
    int *ptr_number = new int[number];//I tried to assign *ptr_numbers the value of number
    cout << *ptr_number;//The output wasn't what I entered though
}

What did I do wrong there?
Sponsor
Sponsor
Sponsor
sponsor
Panphobia




PostPosted: Thu Apr 04, 2013 5:03 pm   Post subject: 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




PostPosted: Thu Apr 04, 2013 5:43 pm   Post subject: Re: RE:Memory Allocation

Panphobia @ 4/4/2013, 5:03 pm wrote:
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




PostPosted: Thu Apr 04, 2013 5:50 pm   Post subject: RE:Memory Allocation

use ( ) instead of [ ] and everything else the same
Cancer Sol




PostPosted: Thu Apr 04, 2013 7:53 pm   Post subject: Re: RE:Memory Allocation

Panphobia @ 4/4/2013, 5:50 pm wrote:
use ( ) instead of [ ] and everything else the same

Thanks, it works now Very Happy
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




PostPosted: Thu Apr 04, 2013 8:31 pm   Post subject: 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




PostPosted: Fri Apr 05, 2013 12:49 am   Post subject: 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




PostPosted: Fri Apr 05, 2013 1:08 am   Post subject: Re: Memory Allocation

Quick tip:

code:
#include <iostream>
#include <algorithm>

int main()
{
        int *foo = new int[10];

        std::fill(foo, foo + 10, 0);

        for (int i = 0; i < 10; i++)
        {
                std::cout << foo[i] << std::endl;
        }

        return 0;
}
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Fri Apr 05, 2013 4:35 pm   Post subject: Re: Memory Allocation

wtd @ Fri Apr 05, 2013 1:08 am wrote:
Quick tip:

code:
#include <iostream>
#include <algorithm>

int main()
{
        int *foo = new int[10];

        std::fill(foo, foo + 10, 0);

        for (int i = 0; i < 10; i++)
        {
                std::cout << foo[i] << std::endl;
        }

        return 0;
}


Why didn't they ever teach me this in school? So many for loops I didn't have to write!
wtd




PostPosted: Fri Apr 05, 2013 4:43 pm   Post subject: RE:Memory Allocation

The STL (Standard Template Library) is not for the faint of heart. It makes deep use of templates, which are tricky, and do not result in pleasant error messages.

Teachers avoid it because C++ is complicated enough to teach as it is, but also because many employers who hire programmers to work in C++ also avoid it. To some extent that situation is a chicken and egg problem.
Cancer Sol




PostPosted: Fri Apr 05, 2013 4:48 pm   Post subject: Re: Memory Allocation

wtd @ 4/5/2013, 1:08 am wrote:
Quick tip:

code:
#include <iostream>
#include <algorithm>

int main()
{
        int *foo = new int[10];

        std::fill(foo, foo + 10, 0);

        for (int i = 0; i < 10; i++)
        {
                std::cout << foo[i] << std::endl;
        }

        return 0;
}

I saw that in my ebook already, thanks anyways Razz
The example I saw didn't say it was for arrays, so I was kind of confused.

Btw, is there a way to delete a non-integer pointer? Since I can't do something like this:
c++:

POINT *cursorpos = new POINT[amount];

since it's not an enum or integer... is it even possible to get memory from the free store for the structure POINT?
Edit: Nevermind, reread it.. what does this do?
code:

std::fill(foo, foo + 10, 0);

and what does the algorithm header file allow me to do?
nullptr




PostPosted: Fri Apr 05, 2013 5:01 pm   Post subject: Re: Memory Allocation

Including <algorithm> lets you use a bunch of useful, generalized functions, like swapping, sorting, searching, etc. You can find a description of all these algorithms (including fill) at http://www.cplusplus.com/reference/algorithm/
Cancer Sol




PostPosted: Fri Apr 05, 2013 5:36 pm   Post subject: Re: Memory Allocation

I'm so stupid, I could've just googled that -_-
Thanks Razz
Cancer Sol




PostPosted: Fri Apr 05, 2013 5:40 pm   Post subject: Re: RE:Memory Allocation

wtd @ 4/5/2013, 4:43 pm wrote:
The STL (Standard Template Library) is not for the faint of heart. It makes deep use of templates, which are tricky, and do not result in pleasant error messages.

Teachers avoid it because C++ is complicated enough to teach as it is, but also because many employers who hire programmers to work in C++ also avoid it. To some extent that situation is a chicken and egg problem.

What do you mean by The STL is not for the faint of heart? like it's just really hard?
Should I even bother with it then?
wtd




PostPosted: Fri Apr 05, 2013 5:50 pm   Post subject: RE:Memory Allocation

The STL requires a reasonably deep knowledge of C++. Of course, trying to figure it out can be a good incentive to investigate the more interesting concepts in C++.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 4  [ 47 Posts ]
Goto page 1, 2, 3, 4  Next
Jump to:   


Style:  
Search: