Advertisement

Last pointer / dyn. mem. alloc. questions ever

Started by January 17, 2001 07:54 PM
6 comments, last by paulcoz 24 years ago
Will someone please confirm this for me? I am using a pointer to allocate memory dynamically for a class like this: typedef struct { object *next; // next object in list object *prev; // previous object in list int polys; int something; } object; object *obj = new object; obj->polys = 1; If I want to set the pointer to NULL is it correct to use: &obj = NULL; And, if I want to set the memory area pointed to, to NULL (eg. clear the class memory) is it correct to use: obj = NULL; I''m not sure what obj = NULL will do, clear the pointer or the memory pointed to. I need to know how to do both because I want to loop through some lists with ''while (next != NULL)'' and I need to know how to set the last object next pointer to NULL. Also, how to clear the class contents pointed to by next. Thanks, Paulcoz.
uhhh.... you can''t change the address of the pointer.
heres an example:

int value = 5;
int* pointer = &value

//now the variable ''pointer'' contains the address of ''value''
//A pointer is actually an unsigned int (32bit sys) so the
//maximum value it can store is an unsigned int. When you put
//the * in front of pointer, you are telling the system to
//goto the position in memory that is contained in pointer
//if you set pointer = NULL (or 0) than it will point to what
//is at memory address 0, which is usually nothing because it
//is set aside so there will NEVER be anythin in it.

*pointer = 1000
//this will change ''value'' to 1000
pointer = 1000
//this will make ''pointer'' point to whats at mem address 1000

hope this helped
Advertisement
To set pointer to NULL, obj = NULL;
To delete object pointer points to, delete obj;
O.k. we can set the pointer to NULL, and delete the area of memory pointed to by the pointer.

How about clearing the memory address pointed to, to NULL values? eg. without deallocating that memory, clear it without referring to individual members of the class (obj->polys = NULL, obj->next = NULL). Is it:

*obj = NULL;

Thankyou CProgrammer0015 and acw83.
Paulcoz.
well...i hope this is what you want,
if you have some structure like

struct{
int x;
int y;
int z;
}3dpoint;

...and you do...

3dpoint* pointer;

then ...

pointer is pointing only at the first item !,
and that is x. pointer is not pointing on y or z,
just x.
you can then init to zero this variables like this:

pointer->x = 0;
pointer->y = 0;
pointer->z = 0;

or...like this

int* pointer = &3dpoint;
memset(pointer, 0, sizeof(3dpoint)); //sets memory to 0 begining from pointer + sizeof(3dpoint)..the same as 12bytes..3*4bytes

or like this
but you must declare pointer a little different,
if you say that pointer is just:
int* pointer;
then...
pointer = &3dpoint; (pointer just points to x)
*pointer = 0; (here it is...x
*(pointer++) = 0; (move to y)
*(pointer++) = 0; (move to z)

...or if you say that pointer is like this:

char* pointer;
pointer = &3dpoint;
(note: bcos pointer is char(1BYTE) when you ++ u go up for just one BYTE..so you must do that 4 time (int = 4 bytes))
*(pointer) = 0;
*(pointer++) = 0;
*(pointer++) = 0;
*(pointer++) = 0 (with this four statements you initialized x to
0, repeat the same for y and z)...

and you can do like...

char* pointer = &3dpoint;
memset(pointer, 0, sizeof(3dpoint));

well i hope you get the idea....have fun
byee

well...i hope this is what you want,
if you have some structure like

struct{
int x;
int y;
int z;
}3dpoint;

...and you do...

3dpoint* pointer;

then ...

pointer is pointing only at the first item !,
and that is x. pointer is not pointing on y or z,
just x.
you can then init to zero this variables like this:

pointer->x = 0;
pointer->y = 0;
pointer->z = 0;

or...like this

int* pointer = &3dpoint;
memset(pointer, 0, sizeof(3dpoint)); //sets memory to 0 begining from pointer + sizeof(3dpoint)..the same as 12bytes..3*4bytes

or like this
but you must declare pointer a little different,
if you say that pointer is just:
int* pointer;
then...
pointer = &3dpoint; (pointer just points to x)
*pointer = 0; (here it is...x
*(pointer++) = 0; (move to y)
*(pointer++) = 0; (move to z)

...or if you say that pointer is like this:

char* pointer;
pointer = &3dpoint;
(note: bcos pointer is char(1BYTE) when you ++ u go up for just one BYTE..so you must do that 4 time (int = 4 bytes))
*(pointer) = 0;
*(pointer++) = 0;
*(pointer++) = 0;
*(pointer++) = 0 (with this four statements you initialized x to
0, repeat the same for y and z)...

and you can do like...

char* pointer = &3dpoint;
memset(pointer, 0, sizeof(3dpoint));

well i hope you get the idea....have fun
byee

Advertisement
why would you want to set the pointer to NULL without deleting the memory first ? That is just a memory leak and is bad. If you do that a bunch of times the person who is running the program will eventually run out of memory and the program/OS will crash.......that isnt what you are trying to do is it ?

quote:
How about clearing the memory address pointed to, to NULL values? eg. without deallocating that memory



"Yo yo ma"
-Kramer
"Yo yo ma" -Kramer
Thankyou all for your help, I found what I need in your responses.

Paulcoz.

This topic is closed to new replies.

Advertisement