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 Previous  1, 2, 3, 4  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Cancer Sol




PostPosted: Fri Apr 05, 2013 6:00 pm   Post subject: Re: RE:Memory Allocation

wtd @ 4/5/2013, 5:50 pm wrote:
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++.

Alright thanks Smile
So, is it possible to get the memory the size of POINT from the free store? Is it possible to even get memory from the free store
if it's not an integer or enum?
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




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

Dunno how it's done in c++, but in C, the sizeof() function will return the size of the type passed to it. sizeof(int) returns the size of an int. sizeof (POINT) will return the size of POINT.
Cancer Sol




PostPosted: Fri Apr 05, 2013 6:44 pm   Post subject: Re: RE:Memory Allocation

Insectoid @ 4/5/2013, 6:40 pm wrote:
Dunno how it's done in c++, but in C, the sizeof() function will return the size of the type passed to it. sizeof(int) returns the size of an int. sizeof (POINT) will return the size of POINT.

Well, that wasn't what I was really wondering, although that might help me in the future.
Here's an example for an integer:
code:

int *example = new int;

Which I tried this:
code:

POINT *example = new POINT;

but I found out that New and Delete only works with integers and enums.
How do I request for more memory and delete the pointer with POINT?
Insectoid




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

We could do things the C way again, I suppose.

code:
Point* example = (POINT*) malloc (sizeof (POINT));


malloc is a C function that directly allocates a block of memory of a given size.
Cancer Sol




PostPosted: Fri Apr 05, 2013 7:13 pm   Post subject: Re: RE:Memory Allocation

Insectoid @ 4/5/2013, 6:50 pm wrote:
We could do things the C way again, I suppose.

code:
Point* example = (POINT*) malloc (sizeof (POINT));


malloc is a C function that directly allocates a block of memory of a given size.

I guess I can try that...
Does C++ not have its own function for it though?
DemonWasp




PostPosted: Fri Apr 05, 2013 7:18 pm   Post subject: RE:Memory Allocation

In C++, new and delete DO work with things other than int and enum: they work with pretty well everything!

Assuming that POINT is declared...
code:
POINT *example = new POINT(); // needs the brackets!


Later...
code:
delete example;


You can even use new to create arrays of whatever you want:
code:

POINT **ex_arr = new POINT[size];  // creates an array of (size) pointers to POINT objects on the heap
for ( int i = 0; i < size; ++i ) {
    ex_arr[i] = new POINT();   // constructs a new POINT object on the heap and puts the pointer to it into position i of ex_arr
}


Later...
code:
delete [] ex_arr;  // the square brackets are very important, because they tell the compiler that you want to delete ex_arr AND all of its contents
Cancer Sol




PostPosted: Fri Apr 05, 2013 9:33 pm   Post subject: Re: RE:Memory Allocation

DemonWasp @ 4/5/2013, 7:18 pm wrote:
In C++, new and delete DO work with things other than int and enum: they work with pretty well everything!

Assuming that POINT is declared...
code:
POINT *example = new POINT(); // needs the brackets!


Later...
code:
delete example;


You can even use new to create arrays of whatever you want:
code:

POINT **ex_arr = new POINT[size];  // creates an array of (size) pointers to POINT objects on the heap
for ( int i = 0; i < size; ++i ) {
    ex_arr[i] = new POINT();   // constructs a new POINT object on the heap and puts the pointer to it into position i of ex_arr
}


Later...
code:
delete [] ex_arr;  // the square brackets are very important, because they tell the compiler that you want to delete ex_arr AND all of its contents

Well, I'm using GNU GCC compiler, and it said that that new can only be used with enums and int, idk why.
I'll try it again, but I think my error might be that I'm missing a * for the pointer array.
I thought it was just one pointer like this:
c++:

POINT *ex_arr = new POINT [size];

What do you mean by heap though?
DemonWasp




PostPosted: Fri Apr 05, 2013 10:22 pm   Post subject: RE:Memory Allocation

First, there are two main places you allocate memory in C++ (and in programs in general, though most other languages choose to hide these details): the "stack" and the "heap".

Stack allocation is "cheaper" in terms of time, but may cause scoping problems (you cannot return a stack-allocated object or variable from a function and expect it to still be valid).

Heap allocation (using new or malloc or any of malloc's family) is "more expensive" (only a little bit), but has the advantage that you can allocate something within a function and then return it.

There are a lot of schools of thought on how to use each kind of allocation best. The only obviously-true things seem to be: allocate on the stack when you can, and use the RAII pattern when you can (you *should* be reading about that in your C++ textbook around when it discusses allocating memory for objects or where it talks about destructors; don't worry about it until you get there).

This:
code:
POINT **ex_arr = ...

declares a pointer to a pointer to a POINT object.

This:
code:
POINT *example = ...

declares a pointer to a POINT object.

These correspond to two different ways to lay out the memory you have allocated.

For the example pointer, that can either be a pointer to a single POINT object, or it can be treated like an array of POINT objects. If you allocated example as an array, then you can access a member 'x' of POINT #i with this syntax: example[i].x. All of the POINT objects will be contiguous in RAM, meaning they will be stored side-by-side.

For the ex_arr pointer, that is generally used to mean "an array of pointers to POINT". That means that ex_arr[i] is a *POINT, so if you want member 'x' of POINT #i, use this syntax: ex_arr[i]->x. The POINT objects will probably not be contiguous in RAM, and in fact may be stored all over the place. Because ex_arr is an array of pointers, you can also have ex_arr[i] == nullptr, which may or may not be desirable, depending on what you want to do.


Note: I haven't tested any of the code I posted, so the syntax may not be perfect.
Sponsor
Sponsor
Sponsor
sponsor
Cancer Sol




PostPosted: Sat Apr 06, 2013 4:46 pm   Post subject: Re: RE:Memory Allocation

DemonWasp @ 4/5/2013, 10:22 pm wrote:
First, there are two main places you allocate memory in C++ (and in programs in general, though most other languages choose to hide these details): the "stack" and the "heap".

Stack allocation is "cheaper" in terms of time, but may cause scoping problems (you cannot return a stack-allocated object or variable from a function and expect it to still be valid).

Heap allocation (using new or malloc or any of malloc's family) is "more expensive" (only a little bit), but has the advantage that you can allocate something within a function and then return it.

There are a lot of schools of thought on how to use each kind of allocation best. The only obviously-true things seem to be: allocate on the stack when you can, and use the RAII pattern when you can (you *should* be reading about that in your C++ textbook around when it discusses allocating memory for objects or where it talks about destructors; don't worry about it until you get there).

This:
code:
POINT **ex_arr = ...

declares a pointer to a pointer to a POINT object.

This:
code:
POINT *example = ...

declares a pointer to a POINT object.

These correspond to two different ways to lay out the memory you have allocated.

For the example pointer, that can either be a pointer to a single POINT object, or it can be treated like an array of POINT objects. If you allocated example as an array, then you can access a member 'x' of POINT #i with this syntax: example[i].x. All of the POINT objects will be contiguous in RAM, meaning they will be stored side-by-side.

For the ex_arr pointer, that is generally used to mean "an array of pointers to POINT". That means that ex_arr[i] is a *POINT, so if you want member 'x' of POINT #i, use this syntax: ex_arr[i]->x. The POINT objects will probably not be contiguous in RAM, and in fact may be stored all over the place. Because ex_arr is an array of pointers, you can also have ex_arr[i] == nullptr, which may or may not be desirable, depending on what you want to do.


Note: I haven't tested any of the code I posted, so the syntax may not be perfect.

So if I declare a pointer like this:
c++:

int main()
{
    int amount = 0;
    POINT *ex_array = new POINT [amount];
    *ex_array[amount] = NULL;//Will this work even work?
    GetCursorPos(*ex_array[amount]);
}

Then if I want to request x and/or y, do I use this syntax:
[syntax=cpp"]
*ex_array[amount].x;
//and
*ex_array[amount].y;
[/syntax]
Thanks for helping by the way!
Panphobia




PostPosted: Sat Apr 06, 2013 4:56 pm   Post subject: RE:Memory Allocation

That would not work, because when you initialize an array of size n lets say, there are 0 - (n-1) indexes, so giving a value to "array[n]" would give an index out of bounds error, or something like that. Also I am pretty sure an array of size 0 won't hold anything either.
Cancer Sol




PostPosted: Sat Apr 06, 2013 7:06 pm   Post subject: Re: Memory Allocation

Can I assign all of *ex_array 's indexes to the value NULL and then assign amount a different value?

Edit: Actually, nvm, I know what you're talking about now. And, I thought an array of size 0 is able to hold data, but I guess I could be wrong.
Panphobia




PostPosted: Sat Apr 06, 2013 7:33 pm   Post subject: RE:Memory Allocation

Yes you can assign the values to NULL and then assign different values, but I am pretty sure this is the same thing as assigning the value of 0 to it.
Insectoid




PostPosted: Sat Apr 06, 2013 8:33 pm   Post subject: RE:Memory Allocation

If you try to assign an integer the value of NULL, you'll probably get an error. You can set pointers to NULL though, which is actually really useful in certain situations.
crossley7




PostPosted: Sat Apr 06, 2013 9:40 pm   Post subject: RE:Memory Allocation

Assigning the value 0 and NULL are very different things. NULL is something generally used for pointers and can be super useful for things such as linked lists where you need to know you are at the end.

You can also use this assignment for C-strings (and possibly regular strings... not sure of that) since they are technicaly pointers themselves.

0 is a valid value in general and is only invalid if your program defines it to be while NULL by definition is an invalid memory location.
Cancer Sol




PostPosted: Sat Apr 06, 2013 9:53 pm   Post subject: Re: RE:Memory Allocation

Insectoid @ 4/6/2013, 8:33 pm wrote:
If you try to assign an integer the value of NULL, you'll probably get an error. You can set pointers to NULL though, which is actually really useful in certain situations.

That's what I was trying to do (in the example).
Wasn't that a pointer that points at a certain address in the free store?
I'm going to have to re-read this stuff to understand it better.
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 2 of 4  [ 47 Posts ]
Goto page Previous  1, 2, 3, 4  Next
Jump to:   


Style:  
Search: