473,405 Members | 2,415 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Static memory overrun help

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?
Jul 22 '05 #1
8 3864
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));
}

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
Jul 22 '05 #2
[....]

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 ....
Victor

Jul 22 '05 #3
"ma740988" <ma******@pegasus.cc.ucf.edu> wrote...
[....]

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
Jul 22 '05 #4
Victor Bazarov <v.********@comAcast.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).
Jul 22 '05 #5
"Old Wolf" <ol*****@inspire.net.nz> wrote...
Victor Bazarov <v.********@comAcast.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
Jul 22 '05 #6
"Victor Bazarov" <v.********@comAcast.net> wrote:
"Old Wolf" <ol*****@inspire.net.nz> wrote...
Victor Bazarov <v.********@comAcast.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.
Jul 22 '05 #7
Old Wolf wrote:
"Victor Bazarov" <v.********@comAcast.net> wrote:
"Old Wolf" <ol*****@inspire.net.nz> wrote...
Victor Bazarov <v.********@comAcast.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
Jul 22 '05 #8
Victor Bazarov <v.********@comAcast.net> wrote:
"Old Wolf" <ol*****@inspire.net.nz> wrote...
Victor Bazarov <v.********@comAcast.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, '\0') 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].
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: J. Campbell | last post by:
I posted a question some time back about accessing a char array as an array of words. In order not to overrun the char array, I padded it with enough 0x00 bytes to ensure that when accessed as...
12
by: jois.de.vivre | last post by:
Hi, I have the following piece of code that is designed to help me add debug traces to my program (I wanted to use purely C++ code, but the only way I know how to do something like this is with...
12
by: Joe Narissi | last post by:
I know how to create and use static constructors, but is there a such thing as a static destructor? If not, then how do you deallocate memory intialized in the static constructor? Thanks in...
5
by: prashantkhoje | last post by:
hi ppl, i am developing an C application in Microsoft visual C++ 6.0. i get following error while running it in debug mode: /* here's the error message */ program: my_application.exe...
6
by: laikon | last post by:
Hi, everyone, below is my program to test static pointer data member; class A { private: static A* p; protected: A() {} public: static A* init()
3
by: schizoid_man | last post by:
Hi, I have the following code snippets and I get a std::bad_alloc error where I think there should be none. I've attached the relevant bits of the base class, derived class and the .cpp file...
23
by: sam_cit | last post by:
Hi Everyone, I have the following program unit, #include <stdlib.h> int main() { char *p = (char*)malloc(100); if(p==NULL)
2
by: chenxinleo | last post by:
Hi, When i use some standard library functions and fields,which return char* type(like ctime in time.h, optarg in getopt.h)and do not have to be freed after calling,i always worry about memory...
1
by: petercable | last post by:
On Oct 21, 5:19 pm, Rolf Wester <rolf.wes...@ilt.fraunhofer.dewrote: <snip> AFAIK, python uses malloc behind the scenes to allocate memory. From the malloc man page... "The malloc() and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.