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(&midSect ion, 0, sizeof(midSecti on));
}
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? 8 3885
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(&midSect ion, 0, sizeof(midSecti on)); }
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(&midSect ion, ...
to
memset(&midSect ion[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
[....] If I were to nit-pick, I'd change
memset(&midSect ion, ...
to
memset(&midSect ion[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
"ma740988" <ma******@pegas us.cc.ucf.edu> wrote... [....] If I were to nit-pick, I'd change
memset(&midSect ion, ...
to
memset(&midSect ion[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
Victor Bazarov <v.********@com Acast.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(&midSect ion, 0, sizeof(midSecti on)); }
If I were to nit-pick, I'd change
memset(&midSect ion, ... to memset(&midSect ion[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).
"Old Wolf" <ol*****@inspir e.net.nz> wrote... Victor Bazarov <v.********@com Acast.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(&midSect ion, 0, sizeof(midSecti on)); }
If I were to nit-pick, I'd change
memset(&midSect ion, ... to memset(&midSect ion[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
"Victor Bazarov" <v.********@com Acast.net> wrote: "Old Wolf" <ol*****@inspir e.net.nz> wrote... Victor Bazarov <v.********@com Acast.net> wrote: spoc wrote: > void func() > { > static int midSection[ 10 ][ 10 ]; > memset(&midSect ion, 0, sizeof(midSecti on)); > }
If I were to nit-pick, I'd change memset(&midSect ion, ... to memset(&midSect ion[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.
Old Wolf wrote: "Victor Bazarov" <v.********@com Acast.net> wrote:
"Old Wolf" <ol*****@inspir e.net.nz> wrote...
Victor Bazarov <v.********@com Acast.net> wrote:
spoc wrote:
>void func() >{ > static int midSection[ 10 ][ 10 ]; > memset(&midSect ion, 0, sizeof(midSecti on)); >}
If I were to nit-pick, I'd change memset(&midSect ion, ... to memset(&midSect ion[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
Victor Bazarov <v.********@com Acast.net> wrote: "Old Wolf" <ol*****@inspir e.net.nz> wrote... Victor Bazarov <v.********@com Acast.net> wrote: >spoc wrote: > >> void func() >> { >> static int midSection[ 10 ][ 10 ]; >> memset(&midSect ion, 0, sizeof(midSecti on)); >> } > >If I were to nit-pick, I'd change > memset(&midSect ion, ... >to > memset(&midSect ion[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<cha rT *, 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]. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 words I
wouldn't overrun the array. I was told that this is dangerous and
that there could be alignment problems if, for example, I wanted to
access the char array elements from non-even multiples of sizeof(int).
For example, if I had the array:
...
|
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 macros, so please
don't yell at me):
#include <iostream>
#include <cstdarg>
|
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 advance,
Joe
|
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
DAMAGE: after Block number (#41) at 0x002F51C0
|
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()
| |
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 containing the main()
method.
I'd really appreciate any help in getting this error sorted out.
Thanks,
|
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)
|
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 leaking(thoug i
konw i just donot have to).Then i look inside in time.h
file(mingw) ,and i found notes say"These functions write to and return
pointers to static buffers that may be overwritten by other function
calls".So how is the"static buffers"...
|
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 free() functions provide a simple, general-purpose
memory allocation package. The malloc() function returns a pointer to
a block of at least size bytes suitably aligned for any use. If the
space assigned by malloc() is overrun, the results are...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |