C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Static memory overrun help

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
spoc
Guest





PostPosted: Mon Aug 30, 2004 2:29 pm    Post subject: Static memory overrun help Reply with quote



I am using VC++6 and Numega bounds checker and have been getting many STATIC
MEMORY OVERRUN errors (boundschecker) while trying to track down bugs. An
example is below:

void func()
{
static int midSection[ 10 ][ 10 ];

memset(&midSection, 0, sizeof(midSection));
}

For the life of me I can't see why this should cause an error. I also get
the error for the following:


void func()
{
static char f[ 100 ];

strcpy(f, funcb() ); // where funcb() is guaranteed to return a string <
100
}

Maybe it's just bounds checker? Anyone help?


Back to top
Victor Bazarov
Guest





PostPosted: Mon Aug 30, 2004 2:45 pm    Post subject: Re: Static memory overrun help Reply with quote



spoc wrote:
Quote:
I am using VC++6 and Numega bounds checker and have been getting many STATIC
MEMORY OVERRUN errors (boundschecker) while trying to track down bugs. An
example is below:

void func()
{
static int midSection[ 10 ][ 10 ];

memset(&midSection, 0, sizeof(midSection));
}

For the life of me I can't see why this should cause an error. I also get
the error for the following:


void func()
{
static char f[ 100 ];

strcpy(f, funcb() ); // where funcb() is guaranteed to return a string
100
}

Maybe it's just bounds checker? Anyone help?

There is nothing in this code that would suggest memory overrun. However,
if you intended to just _initialise_ 'midSection' in the first 'func', you
shouldn't memset it every time the function runs. You just need to do

static int midSection[10][10] = { 0 };

which initialises it to 0s. Although, IIRC, even that is unnecessary due
to the fact that all objects of static storage duration are zero-
initialised anyway. Again, that's if you just needed it to initialise. If
you do need it cleaned up every time, your code is fine.

If I were to nit-pick, I'd change

memset(&midSection, ...

to

memset(&midSection[0][0], ...

but it really shouldn't make any difference in the execution because the
address of an array and the address of the first element of the array are
the same (although their types are different).

Victor

Back to top
ma740988
Guest





PostPosted: Thu Sep 02, 2004 12:23 pm    Post subject: Re: Static memory overrun help Reply with quote



[....]
Quote:

If I were to nit-pick, I'd change

memset(&midSection, ...

to

memset(&midSection[0][0], ...

but it really shouldn't make any difference in the execution because the
address of an array and the address of the first element of the array are
the same (although their types are different).

Could you elaborate on the type differences here? I think I'm

following you but ....

Quote:
Victor

Back to top
Victor Bazarov
Guest





PostPosted: Sat Sep 04, 2004 7:10 pm    Post subject: Re: Static memory overrun help Reply with quote

"ma740988" <ma740988 (AT) pegasus (DOT) cc.ucf.edu> wrote...
Quote:
[....]

If I were to nit-pick, I'd change

memset(&midSection, ...

to

memset(&midSection[0][0], ...

but it really shouldn't make any difference in the execution because the
address of an array and the address of the first element of the array
are
the same (although their types are different).

Could you elaborate on the type differences here? I think I'm
following you but ....

If 'a' is declared as

T a[N];

then its type is "array of N T". The expression '&a' then has the type
"a pointer to an array of N T". The expression 'a[0]' has the type "T&",
and '&a[0]' has the type "pointer to T", which is, incidentally, the type
of the expression a. The name of an array decays to the pointer to the
type of the element of the array.

With a two- and more-dimensional arrays, the address where the array
begins is the address of the first element, and if you need to iterate
over those elements using a pointer arithmetic, it's better to use the
pointer to an element than a pointer to the entire array. T* when
incremented will point to the next element. T (*)[N] when incremented
will point past the end of the array.

For memset there is no need to worry because it operates in terms of
bytes internally. But if you somehow encounter a function template,
then correct types can be very important.

Victor



Back to top
Old Wolf
Guest





PostPosted: Sun Sep 05, 2004 11:49 pm    Post subject: Re: Static memory overrun help Reply with quote

Victor Bazarov <v.Abazarov (AT) comAcast (DOT) net> wrote:
Quote:
spoc wrote:
I am using VC++6 and Numega bounds checker and have been getting many STATIC
MEMORY OVERRUN errors (boundschecker) while trying to track down bugs. An
example is below:

void func()
{
static int midSection[ 10 ][ 10 ];
memset(&midSection, 0, sizeof(midSection));
}

If I were to nit-pick, I'd change

memset(&midSection, ...
to
memset(&midSection[0][0], ...


Why? The first is fine, the second is technically UB (you are
passing it a pointer to one int, and you overflow bounds by
writing 100 ints to that pointer).

Back to top
Victor Bazarov
Guest





PostPosted: Mon Sep 06, 2004 4:33 pm    Post subject: Re: Static memory overrun help Reply with quote

"Old Wolf" <oldwolf (AT) inspire (DOT) net.nz> wrote...
Quote:
Victor Bazarov <v.Abazarov (AT) comAcast (DOT) net> wrote:
spoc wrote:
I am using VC++6 and Numega bounds checker and have been getting many
STATIC
MEMORY OVERRUN errors (boundschecker) while trying to track down bugs.
An
example is below:

void func()
{
static int midSection[ 10 ][ 10 ];
memset(&midSection, 0, sizeof(midSection));
}

If I were to nit-pick, I'd change

memset(&midSection, ...
to
memset(&midSection[0][0], ...


Why? The first is fine, the second is technically UB (you are
passing it a pointer to one int, and you overflow bounds by
writing 100 ints to that pointer).

This is nonsense. Since there are 100 ints at that address, there
is no overflow of any bounds and there is no UB.

V



Back to top
Old Wolf
Guest





PostPosted: Wed Sep 08, 2004 4:29 am    Post subject: Re: Static memory overrun help Reply with quote

"Victor Bazarov" <v.Abazarov (AT) comAcast (DOT) net> wrote:
Quote:
"Old Wolf" <oldwolf (AT) inspire (DOT) net.nz> wrote...
Victor Bazarov <v.Abazarov (AT) comAcast (DOT) net> wrote:
spoc wrote:
void func()
{
static int midSection[ 10 ][ 10 ];
memset(&midSection, 0, sizeof(midSection));
}

If I were to nit-pick, I'd change
memset(&midSection, ...
to
memset(&midSection[0][0], ...

Why? The first is fine, the second is technically UB (you are
passing it a pointer to one int, and you overflow bounds by
writing 100 ints to that pointer).

This is nonsense. Since there are 100 ints at that address, there
is no overflow of any bounds and there is no UB.

That doesn't answer the first question: why do you prefer
midSection[0] (equivalent to &midSection[0][0]) to &midSection?
For me, the least error-prone method of using the mem* functions
is to pass the address of the object that is being set.

Back to top
Victor Bazarov
Guest





PostPosted: Wed Sep 08, 2004 3:03 pm    Post subject: Re: Static memory overrun help Reply with quote

Old Wolf wrote:
Quote:
"Victor Bazarov" <v.Abazarov (AT) comAcast (DOT) net> wrote:

"Old Wolf" <oldwolf (AT) inspire (DOT) net.nz> wrote...

Victor Bazarov <v.Abazarov (AT) comAcast (DOT) net> wrote:

spoc wrote:

void func()
{
static int midSection[ 10 ][ 10 ];
memset(&midSection, 0, sizeof(midSection));
}

If I were to nit-pick, I'd change
memset(&midSection, ...
to
memset(&midSection[0][0], ...

Why? The first is fine, the second is technically UB (you are
passing it a pointer to one int, and you overflow bounds by
writing 100 ints to that pointer).

This is nonsense. Since there are 100 ints at that address, there
is no overflow of any bounds and there is no UB.


That doesn't answer the first question: why do you prefer
midSection[0] (equivalent to &midSection[0][0]) to &midSection?
For me, the least error-prone method of using the mem* functions
is to pass the address of the object that is being set.

I prefer using &midSection[0][0] because it has the correct type --
a pointer to int. Not a pointer to an array of 10 arrays of 10 ints,
not a pointer to an array of 10 ints.

See my reply to the OP earlier in this thread.

Victor

Back to top
Old Wolf
Guest





PostPosted: Thu Sep 09, 2004 9:41 pm    Post subject: Re: Static memory overrun help Reply with quote

Victor Bazarov <v.Abazarov (AT) comAcast (DOT) net> wrote:
Quote:
"Old Wolf" <oldwolf (AT) inspire (DOT) net.nz> wrote...
Victor Bazarov <v.Abazarov (AT) comAcast (DOT) net> wrote:
spoc wrote:

void func()
{
static int midSection[ 10 ][ 10 ];
memset(&midSection, 0, sizeof(midSection));
}

If I were to nit-pick, I'd change
memset(&midSection, ...
to
memset(&midSection[0][0], ...

Why?
I prefer using &midSection[0][0] because it has the correct type --
a pointer to int. Not a pointer to an array of 10 arrays of 10 ints,
not a pointer to an array of 10 ints.

See my reply to the OP earlier in this thread.

"pointer to int" isn't the correct type. You aren't filling a
block of ints with int 0. You are filling an object with char 0.
memset fills byte by byte (even though the memset function takes
a parameter of type int for the fill char, it converts it to a
char in order to fill).

You mentioned needing correct types for template functions: the
equivalent of memset is std::fill_n<charT *, size_t, charT>().
If you call std::fill_n(ptr, 100, '') then ptr should be a
pointer to char (not a pointer to int).

I interpret memset as "fill an object (given a pointer to it
and its size). So passing a pointer to the object (ie. &midSection)
is correct.

This is of course all moot, as memset takes a (void *) and the
standard practically guarantees that (void *)&midSection ==
(void *)&midSection[0][0].

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.