# Dynamic memory management

## Recommended Posts

I'm trying to make a dynamic array inside a dynamic structure:
typedef struct
{
float x,y,z;
}
VERTEX;

typedef struct
{
int v, max_v;
float x,y,z;
VERTEX *vertex;
}
OBJECT;
OBJECT *object;

How would i use malloc() with *vertex? (Trying to use it as object[#].vertex[#].x) [Edited by - biscuitz on April 23, 2006 6:34:27 PM]

##### Share on other sites
Are we talking C or C++?
- If C++, use std::vector for both, or at the very least do not use malloc (prefer new).
- If C, then the same way you allocated your OBJECT array.

Presumably, the OBJECT* called object was allocated like:
object = malloc(numberOfObjects * sizeof(OBJECT));

Well then, for each object in the list you do something similar:
object[n].vertex = malloc(numberOfVertices * sizeof(VERTEX));

Generally in C you'd provide a function called, for example, InitObject, that would initialize an object (including allocating enough space for its vertices). In C++ you'd do this in a constructor.

[Edited by - jpetrie on April 23, 2006 8:26:17 PM]

##### Share on other sites
I'm programming in C cuz C++ isnt compatible in C.

Would i have to use the arrows also to access x, y, and z since it's *object? (arrow = ->, i have hard time understanding that operator :/) I've been trying to use the period operator...

Is the -> operator like the period operator but for stuff with the *? (trying to understand how it works :/)

##### Share on other sites
I made a typo in my above post with respect to the -> operator; I previously had:
object[n]->vertex

But object[n] is of type OBJECT so the . operator should be used instead. I correct the original post.

-> is used when the left hand side is a pointer. In any other case . is used.

##### Share on other sites
But WHY are you using C? Any particular reason? There are free IDE's and compilers for C++ now so that's not a valid excuse. I was a long time C hold out but C++ is so worth the jump (or C#, also free IDE's and compilers).

##### Share on other sites
Ah, the joys of programming pointers and memory.

First, we have the pointer:

int* pointer;

It is a 32-bit, aka 4 byte, unsigned integer pointing to a memory address. They can point to anywhere in memory mind you. When working with pointers, though, remember that they are a variable, they can be changed, but that changes to WHERE it points, NOT the VALUE of where it points. If you would like to do that, you must dereference it, using the * operator. For example:

int ex = 1;int* pointer;pointer = &ex;(*pointer)++;  //Changes value of ex to 2, () isn't neccessary, just a little cleanerpointer++;  //Say ex is at 0x00000001 in memory, pointer would now point to 0x00000002

Now, we can also use pointers to allocate dynamic memory. In C, when we allocate memory, we allocate a number of BYTES, not a number of OBJECTS. If we would like to allocate a number of objects, we must determine how many BYTES make up the objects. If an object takes 4 bytes, and we want 3 objects, then we would have to malloc() 12 bytes. We can convert this data using the cast converter in C. For example:

OBJECT* pointer; //Pointer to object that takes up 4 bytes
pointer = (OBJECT*)malloc(sizeof(OBJECT) * 3); /*The first parenthesis determine what kind of data structure we want to cast the memory to, the malloc is the calling of the function, and the last () is the number of bytes we want, which is passed to malloc.*/

Onto the -> thing. Arrays and pointers in C/C++ are treated the same, they're the same type. In otherwords,

char ex[128];

is the same as

char* pointer;

When accessing elements in an array, you use ex[index]. to access a data member of that segment. To make our life easier, it's the same way with a pointer. When you want to just get a data member of the segment that the pointer is pointing to, you use "->"; however, when you would like to get an segment that's offseted to that pointer, we can treat it like a array. As a matter of fact, if you don't want to treat it as an array, you can just add the index to the pointer to give you the segment of data you want, and then access it. For example:

OBJECT ex[128];
OBJECT* pointer = ex;
pointer[68].x = 6; //Array-like access
(pointer+68)->x = 6; //Pointer math access

So, hope this helps!

##### Share on other sites
Quote:
 Original post by dbzprogrammerOnto the -> thing. Arrays and pointers in C/C++ are treated the same, they're the same type.

This isn't quite right. The name of an array can be treated like a pointer to the first element - but they aren't the same. In particular

char ex[128];

creates a contiguous chunk of memory containing space for 128 characters

On the other hand

char* pointer;

creates a 4 byte (on 32 bit platforms) variable that contains a memory address pointing to a character. This is especially noticable in the case of structures. The structure
struct{   int v1;   int* v2;}

will have size sizeof(int)+sizeof(int*) Furthermore even if you point v2 at an array the array will most likely not be contiguous with the structure

on the other hand

struct{   int v1;   int v2[128];}

will have size sizeof(int)+128*sizeof(int). In this case the array is certain (up to compiler padding) to follow v1.

##### Share on other sites
jpetrie: ye i had to use the period operator for it to work (tried arrows and got lotsa errors)

btw i think i learn best by examples and explanations of wut the code means (commented code/etc., trying to figure stuff out from paragraphs is hard and time consuming)

Quote:
 But WHY are you using C?
Quote:
 I'm programming in C cuz C++ isnt compatible in C.

I support open-source programs and want it so the code works in C and C++. Unless nobody uses C then i would go C++... :/ you think i should just go on to C++? (P.S. lol ye my IDE supports both C and C++ but i chose C for the above reasons)

##### Share on other sites
Quote:
 Original post by chowe6685This isn't quite right. The name of an array can be treated like a pointer to the first element - but they aren't the same. In particularchar ex[128];creates a contiguous chunk of memory containing space for 128 charactersOn the other handchar* pointer;creates a 4 byte (on 32 bit platforms) variable that contains a memory address pointing to a character. This is especially noticable in the case of structures. The structurestruct{ int v1; int* v2;}will have size sizeof(int)+sizeof(int*) Furthermore even if you point v2 at an array the array will most likely not be contiguous with the structureon the other handstruct{ int v1; int v2[128];}will have size sizeof(int)+128*sizeof(int). In this case the array is certain (up to compiler padding) to follow v1.

Technicality wasn't too high on my priority list there =P, but yes, you are right =D. I didn't want to get to technical, as they work together quite well, and share some notation & syntax.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628368
• Total Posts
2982293

• 10
• 9
• 13
• 24
• 11