Archived

This topic is now archived and is closed to further replies.

Memory allocation

This topic is 5653 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hail I have some questions about memory. If i allocate *ptaData = new int[15]; do i have a array with adresses to 16 different int locations or do i have an pointer to an array on the same memoryplace? Does it look lika this? *ptaData Pointers adress data [0] an andress to int any where [1] an andress to int any where [2] an andress to int any where Or *ptaData Pointer adress an andress to int array any were, but the array will be on the same place after an other. |----------------- | | | | |_______________________ |45 int data | |46 int data | |47 int data | |48 int data | |49 int data | |50 int data | |... in this case i goto 45 then if I want to access the next item num 46 i use ptrData[2] or ptrdata+1 (+sizeof ptrData). One last question. I want to dynamic allocate int *ipData[10](not dynamic); But if i use *ptrData = new int[10]; I allocate 10 int locations. I only want the first pointer array (*ipData) not what it is pointing to. I want the address to be NULL only a var...... *ipData [0] address NULL [1] address NULL

Share this post


Link to post
Share on other sites
But if i want an array where i can store many different adresses to ex dynamic allocated memory places.

Mkay i get it...
But please answer this...

if i use int *ipData[10] i get a array that i can store adresses in. But if i use *ipData = new int[10]; i get a array with allready allocated adresses to 10 int....or am I wrong?.

But I want a dynamic arraysize that i can reallocate, i dont want the 10 mapped int memory. just the parray.

Share this post


Link to post
Share on other sites
If i want to delete this adresses ive put in the int **pointer_array = new int *[10]; how do i do that?

delete [] *pointer_array;?=??????

Share this post


Link to post
Share on other sites
No, there is no other way with arrays of pointers [nystagmus is crouching down waitning for the blows to come]. You have to delete each single pointer. Watch out though so that you don''t accidentally try to delete a pointer that hasn''t been allocated.

Share this post


Link to post
Share on other sites
quote:
Original post by Leadorn
But is there a better way?

Of course. It looks like:


  
std::vector<boost::shared_ptr<int> > v;


Check out the Boost website for their smart pointers.

Share this post


Link to post
Share on other sites
Isent the adress NULL an adress? Should I repoint the pointerarray to NULL all of them?

Dosent any program store data on adress NULL (0)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If you try to read or write to a pointer which points at NULL you will cause an invalid page fault

Share this post


Link to post
Share on other sites
NULL == 0;
It''s not a memory address. It means empty. if you do this:

a_ptr = new int* [10];

that means you''re allocating 10 pointers to int which are not yet initialized. so, you have to initialize them again:

for (int i=0; i<10; i++)
a_ptr = new int [100];

by doing this, each pointer in a_ptr points to an array of int of size 100.

if you want to delete it, you do it in the reverse order:
for (int i=0; i<10; i++)
if (a_ptr[i] != NULL) delete [] a_ptr[i];
delete [] a_ptr;

however, people usually do it this way:

for (int i=0; i<10; i++) {
if (a_ptr[i] != NULL) {
delete [] a_ptr[i];
a_ptr[i] = NULL;
}
}

delete [] a_ptr;
a_ptr = NULL;

You don''t have to set it equal to NULL, but it''s just the good way to do it. Sometimes, you''ll also find some benefits by doing that, so it''s a good practice.




My compiler generates one error message: "Doesn''t compile."
-Albert Tedja-

Share this post


Link to post
Share on other sites
i wonder...how come part of my post is in italic?? oh, i see. some parts are recognized as tags. btw, here's the complete code from the above post. some of them are removed accidentally.


    
a_ptr = new int* [10];

for (int i=0; i<10; i++)
a_ptr[i] = new int [100];


//if you want to delete it, you do it in the reverse order:

for (int i=0; i<10; i++)
if (a_ptr[i] != NULL) delete [] a_ptr[i];
delete [] a_ptr;

//however, people usually do it this way:

for (int i=0; i<10; i++) {
if (a_ptr[i] != NULL) {
delete [] a_ptr[i];
a_ptr[i] = NULL;
}
}

delete [] a_ptr;
a_ptr = NULL;



[edited by - nicho_tedja on June 25, 2002 5:23:33 AM]

[edited by - nicho_tedja on June 25, 2002 5:24:19 AM]

Share this post


Link to post
Share on other sites

Why do it this way?
int **pointer_array = new int *[10];


what is the difference between them?


a_ptr = new int* [10];
that means you''re allocating 10 pointers to int which are not yet initialized. so, you have to initialize them again:




Share this post


Link to post
Share on other sites