Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

run time memory location

Status
Not open for further replies.

JackMorris

Technical User
Jan 30, 2003
18
0
0
IE
I want to allocate run time memory with new operator inside a for loop. It does fine for the first time but wouldnt work for the second time (and then after) of the for loop. Is there any way out?

1.I dont want that memory after I finish that loop...I tried to delete with delete operator at the end of the loop but it doesnt work.

Help will be highly appreciated.

Jack
 
we need to see the "relevant" code.

-pete
 
let me know if it is enough...if not i will post more

for(no_mixing=0; no_mixing<NO_IMAGES; no_mixing++)
{

ptr_pri_img_red_part=new unsigned char
;
ptr_pri_img_green_part=new unsigned char
;
ptr_pri_img_blue_part=new unsigned char
;

-----------;
-----------;

}
 
so at the bottom of the for loop you did:
Code:
delete [] ptr_pri_img_red_part;
delete [] ptr_pri_img_green_part;
delete [] ptr_pri_img_blue_part;
}  // end for()
and &quot;it doesnt work.&quot;

what do you mean it doesn't work? compiler error? post the error.

-pete
 
>> it is runtime error.

Jack, your problem is likely a simple one and i know we can help you but, you have to be more forthcoming with information. That last post is useless in trying to solve your problem.

You need to post the line of code that causes the runtime error at the very least. After seeing that we might need more, but until we see it we have no idea what your doing wrong.

-pete
 
I'm not sure, but I think you have declared something like

unsigned char *ptr_pri_img_red_part;
unsigned char *ptr_pri_img_green_part;
unsigned char *ptr_pri_img_blue_part;

From what you posted I understand that the allocation doesn't work after the first time it executes the loop. Well, if you declared the pointers in the above-mentioned way, it's not going to work. You should use an array of pointers something like

unsigned char *ptr_pri_img_red_part[NO_IMAGES];
...;
...;


and then in the for loop


for(no_mixing=0; no_mixing<NO_IMAGES; no_mixing++)
{

ptr_pri_img_red_part[no_mixing]=new unsigned char
;
-----------;
-----------;

}

Hope this helps
Radu
 
thanx pete and Radu....

I will implement the suggestion of Radu. If it doesnt work I will explain the error in detail.

Jack
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top