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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with allocation in memory

Status
Not open for further replies.

Skar027

Programmer
May 10, 2005
3
MX
hi, I'm writting a class in viual c++ 6.0, here're two of my functions

void CImagen::SetTamano(UINT m_Ancho, UINT m_Alto)
{
Ancho=m_Ancho;
Alto=m_Alto;
Data=new BYTE[Ancho*Alto];
}

BOOL CImagen::Binarizar(BYTE umbral)
{
UINT i,j,index;
BYTE b;
LPBYTE r=new BYTE[Ancho*Alto];
for (i=0;i<Ancho;i++){
for(j=0;j<Alto;j++){
index=j*Ancho+i;
b=Data[index];
if(b<=umbral)
r[index]=0;
else
r[index]=255;
}
}
delete [] Data;
Data=r;
return TRUE;
}

The first function works fine, the NEW statement works good, but in the second function the new statement returns always 0 or NULL.
Note:
Ancho=320
Alto=240
I think is has something to do with the memory available, does anyone have a solution???
 
That code looks kind of strange to me. You're assigning Ancho = m_Ancho; but if m_Ancho is the member variable of your class it should be the other way around...

If new was failing it should be throwing an exception, not returning 0 like malloc() does. I'd check to see that the size you're assigning to new isn't 0 in the place where it's failing.
 
the value of Ancho*Alto is 76800
so what im doing is

LPBYTE r=new BYTE[Ancho*Alto];

and the value returned by new is 0 or, in other words,NULL.
 
That doesn't make much sense since you're not using the nothrow version of new...
Try allocating a smaller amount of memory and see if that works.
 
> index=j*Ancho+i;
Should be
Code:
index=i*Alto+j;

If you examing the value of your index, my suspicion is that it goes over your Ancho*Alto limit.

As soon as you do that, all bets are off - the code can and will do all sorts of strange things, often in unrelated areas, as soon as you make the mistake of trashing one memory location which isn't yours to touch.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top