new with out-of-bounds test?

G

Gernot Frisch

Hi,

I want to write a class that is returned by a debug version of new(),
that overloads an [] operator and can check the bounds at runtine,
know what I mean?

class AllocClass
{
public:
AllocClass(int sz, int bs)
{m_dat = malloc(sz); m_sz = sz; m_bs=bs;}
~AllocClass()
{if(m_dat) free(m_dat);}

void* operator[](int i)
{
if(i<0 || i>=m_sz)
__throw(42);
return (void*)m_dat+ i*m_bs;
}

char* m_dat;
int m_sz;
int m_bs; // size of a block;
};

void * __cdecl operator new(unsigned int size)
{
return (void*) AllocClass(size);
}

int main()
{
double* pD = new double[5];
pD[3] = 125.0; // call AllocClass.operator[]() ???
}

I somehow get the feeling I got such an error. On my PC implementation
everything works fine, on the ARM PocketPC, the graphics are messed
up - exaclty the same piece of code!
 
R

Rade

How do you expect it to work at all? The code is completely broken. Can you,
please, post some example which at least compiles?

Even if you make it compile, I still don't understand how you expect to:

1) Retrieve the value of bs (apparently the object size), as it is lost in
the operator new() call. Operator new gets only the total size in bytes, not
the number of objects and an object size. BTW probably you should have
replaced operator new[] instead of operator new,
2) Make the code that uses plain old double pointer pD to think that it has
anything to do with some AllocClass?

IMHO that cannot be done - you have to use an object instead of a pointer,
and then you can do what you want. However, why not using
std::vector<double> then?

Rade
 
R

Rolf Magnus

Gernot said:
Hi,

I want to write a class that is returned by a debug version of new(),
that overloads an [] operator and can check the bounds at runtine,
know what I mean?
No.

class AllocClass
{
public:
AllocClass(int sz, int bs)
{m_dat = malloc(sz); m_sz = sz; m_bs=bs;}
~AllocClass()
{if(m_dat) free(m_dat);}

void* operator[](int i)
{
if(i<0 || i>=m_sz)
__throw(42);
return (void*)m_dat+ i*m_bs;
}

char* m_dat;
int m_sz;
int m_bs; // size of a block;
};

void * __cdecl operator new(unsigned int size)
{
return (void*) AllocClass(size);
}

This will return the address of a temporary object. That object gets
destroyed after operator new returns. So the returned pointer cannot be
used for anything. Also operator new is supposed to return raw memory, not
objects.
int main()
{
double* pD = new double[5];
pD[3] = 125.0; // call AllocClass.operator[]() ???

No. pD is a pointer to double, so operator[] gets called for a pointer to
double. It is equivalent to *(pD + 3).
 
R

Rolf Magnus

Rolf said:
Gernot said:
Hi,

I want to write a class that is returned by a debug version of new(),
that overloads an [] operator and can check the bounds at runtine,
know what I mean?
No.

class AllocClass
{
public:
AllocClass(int sz, int bs)
{m_dat = malloc(sz); m_sz = sz; m_bs=bs;}
~AllocClass()
{if(m_dat) free(m_dat);}

void* operator[](int i)
{
if(i<0 || i>=m_sz)
__throw(42);
return (void*)m_dat+ i*m_bs;
}

char* m_dat;
int m_sz;
int m_bs; // size of a block;
};

void * __cdecl operator new(unsigned int size)
{
return (void*) AllocClass(size);
}

This will return the address of a temporary object.

I really need to watch out more when replying. Of course, this won't take
the address of the object. It tries to convert the object into an address,
which should fail with a compiler error message.
 
G

Gernot Frisch

IMHO that cannot be done - you have to use an object instead of a
pointer,
and then you can do what you want. However, why not using
std::vector<double> then?

Well, the problem is I wanted to do it very quickly for a bug-testing.
The vector should _not_ appear in the release version. But... Maybe I
can use a macro that uses either vector or new, depending on
release/debug version... Would be lot of code to fix, though.
-Gernot
 
G

Gernot Frisch

Rolf Magnus said:
Gernot said:
Hi,

I want to write a class that is returned by a debug version of
new(),
that overloads an [] operator and can check the bounds at runtine,
know what I mean?

No.

Basically I want some mechnism that converts this code:

int* p = new int[4];
p[12] = 0;

into:

int* p = new int[4];
if(sizeof(p)>12)
p[12]=0;
else
error(xy);

without touching the actual code, but only redefining the new keyword.
 
R

Rolf Magnus

Gernot said:
Rolf Magnus said:
Gernot said:
Hi,

I want to write a class that is returned by a debug version of
new(),
that overloads an [] operator and can check the bounds at runtine,
know what I mean?

No.

Basically I want some mechnism that converts this code:

int* p = new int[4];
p[12] = 0;

into:

int* p = new int[4];
if(sizeof(p)>12)
p[12]=0;
else
error(xy);

without touching the actual code, but only redefining the new keyword.

Sorry, but that doesn't work. new does only allocate raw memory and return
it. There is no way to use it for converting raw array accesses into
checked ones.
If you want range checking, you can use std::vector<int> instead of an
array. It's at() member function works similar to operator[], but throws an
exception if the index is out of bounds.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top