deleting dummy global after use

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

deleting dummy global after use

Postby fpga » Thu Aug 31, 2006 6:00 pm

Code: Select all
/*
I define and parameterise a single global array object, arrayObj, using a dummy single object/pointer to invoke the constructor.

I want to define lots of them but dont want to have to create as many
dummy object/pointers as there are array objects.

1 How can I just use one dummy object/pointer to define n array objects?

2 How can I delete either the dummy pointer (or object see alternatives)
   in the macro. I'd like to keep it tight with a one line macro, near the
   top, if possible.

3 re the dummy object deletion, how do you deallocate dummy
   variables in the global namespace
   eg int a; //outside any variable.


Any ideas, much appreciated?

*/
#include <iostream>
using namespace std;

class A {
   public:
      int i,j;
      A(int ni,int nj) : i(ni),j(nj) { }
      void memFn() {cout<<i*j<<endl;}
};

#define NUARRYOBJ(nm,qty,ni,nj) \
A * pObj = new A(ni,nj); \
A nm[qty]=*pObj;

NUARRYOBJ(arrayObj,2,3,4)

//==============================================
//alternatives

//A arrayObj[3]=*new A(12,10);     

//A obj(ni,nj);               
//A arrayObj[qty]=obj;
//==============================================
   
void functionThatCanSee_arrayObj() {
   cout << arrayObj[0].i << endl;
}

int main() {
   /* i just want to delete of one of these, or better still do it
       in the macro.
   */
   delete pObj;


   /* little test */
   functionThatCanSee_arrayObj();
   arrayObj[1].memFn();
}

fpga
 
Posts: 5
Joined: Fri Aug 18, 2006 11:19 am

RE: deleting dummy global after use

Postby kunchok » Thu Sep 21, 2006 2:04 am

I think I can help you if this is still active.

1) I assume you need lots of arrays of 'A' objects? And you don't want to give each one of them a name at compile time?

2) It is not possible to deallocate a globally allocated object.
e.g.
int a;

main() {
delete a; // error
}

but

int *a = new(int);
main() {
delete a; // OK but variable 'a' will still have a value
}
kunchok
 
Posts: 3
Joined: Thu Sep 21, 2006 12:18 am

RE: deleting dummy global after use

Postby jjmac » Wed Sep 27, 2006 10:49 am

Howdy.

Yep, lots of these threads seem to just hang..


>>
I want to define lots of them but dont want to have to create as many
dummy object/pointers as there are array objects
>>

You will have to use a loop of soughts anyway, so why not just assign/create to each array element in turn. Using the loops increment to index the array elements.


>>
1 How can I just use one dummy object/pointer to define n array objects?
>>

Define the pointer globally, do all the work in a loop.
Or define the array globally and assign directly to each element in a loop, by passing the need for the pointer.

Or define the pointer in a function and the array globally. Either way, you will need to do it all in a loop of some kind. And if the array isn't global, you wont be able to reference the objects after the called function returns.


>>
2 How can I delete either the dummy pointer ...
>>

If you put the pointer in a function, it will either be overwritten on the next function call (stack based), or if globally declared, it will exist for the programs life. Thats the point to global variables.

Besides, it's really just a type of integer anyway. So it doesn't take up much space.

If you call delete on the pointer it will free the objects memory space. It wont get rid of the pointer. And the relevant array element will be pointing to a freed memory area.

Besides, the memory area wont be zeroed or anything anyway, justs its' memory space will go on the free list. What ever is there is still accesable via its' pointer, well, depending that is.

Try an exp. Create an object ... delete it by calling delete on the pointer that's assigned its' address. Then dereference the object. If the compiler doesn't impose some locks or something it should succeed. But as the memory is also on the free list, depending on what the program is doing the memory could be reused. And the dereference could be pointing within some other allocation. And depending on what kind of object it is. Potentially Exceptional Behaviour May Occur :)

Works with simple things like integer objects. Create and assign to them. delete them, then cout them (grin). If your program wants/needs a paranoid level of security, you would need to get the objects size, then treat it as a byte array. Passing it through a loop zeroing each byte in turn. Then calling delete on its' pointer. Sounds like the kind of thing that should be a lib function in itself really, if it isn't already hidden in there somewhere.


>>
dummy object deletion, how do you deallocate dummy variables in the global namespace
>>

When the program ends and the process space is freed.
You can make it specific to the unit file by declaring it static iirc.

You don't. Thats the whole point to a global type. Primitives like numbers, pointers, don't take up a lot of space and are usually things that are required for program life. If you declare the primitive in a function, it will be stack based and so will disappear on the functions return. The objects themselves will be on the heap though.

If you put both the pointer and array declarations in a function, you will end up with a global mem allocation that hasn't got a means of being referenced, on the functions return. So you would have to do all the work within the function, including the deletions. A bit messy really.



>>
3 re the dummy object deletion, how do you deallocate dummy
variables in the global namespace
eg int a; //outside any variable.
>>

Again, you don't need to. The class declaration wont allocate any space. Only the call to 'new' will involve a memory allocation. So any variables wrapped in the object will be freed (but still there) on the call to delete.

Once you have assigned the pointers value to the array element, then you have the objects address as the value of the array element. Creating a new object and assigning to the same pointer will just give it the new address. The pointer functions like an integer in that respect, but typed differently. You don't need to clean or do anything like that to the pointer in between object creations. Its value will just get overwritten with a new address on each iteration of the loop. At the end of the loop it will point to the same address as the last element in the array.

You could get into the trap of trying to delete an object twice, in that the pointer and the current array element will both point to the same place with each pass through the loop.

The use of the pointer is good because it helps make clear what your doing. It can still be understood this time next week. But i'd tend to just use a 'for' loops increment to index the array, and create each object assigning the address directly. It may be clear too that way, as less steps would be involved.

Hope i haven't miss-interpreted the question to much here (grin)


jm
http://counter.li.org
#313537

The FVWM wm -=- www.fvwm.org -=-

Somebody stole my air guitar, It happened just the other day,
But it's ok, 'cause i've got a spare ...
jjmac
LXF regular
 
Posts: 1996
Joined: Fri Apr 08, 2005 1:32 am
Location: Sydney, Australia


Return to Programming

Who is online

Users browsing this forum: No registered users and 2 guests

cron