Thread: [Dev-C++] memory violation....
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Raghunath Marella.S <ms...@ya...> - 2002-06-09 09:42:00
|
Hi, I have been facing a simple but strange problem in my C++ code. I have declared an integer pointer and whose size has been fixed at a large value dynamically. Like... seneles = 1000,ncn = 6; int *EleTopo = new int[seneles*ncn]; Once this block is filled with valid data, once those values are printed and observed they seem to be correct. But in between of the code, retrieving the same had returned some junk values for some of the indexes. I am not getting what could be the problem. Will any one please help me out in this. thanks in advance, regards, Raghunath __________________________________________________ Do You Yahoo!? Yahoo! - Official partner of 2002 FIFA World Cup http://fifaworldcup.yahoo.com |
|
From: Ioannis V. <no...@ho...> - 2002-06-09 18:07:20
|
> -----Original Message----- > From: dev...@li... > [mailto:dev...@li...] On Behalf > Of Raghunath Marella.S > Sent: Sunday, June 09, 2002 12:42 PM > To: dev...@li... > Subject: [Dev-C++] memory violation.... > > > Hi, > > I have been facing a simple but strange problem in my > C++ code. I have declared an integer pointer and whose > size has been fixed at a large value dynamically. > Like... > > seneles = 1000,ncn = 6; You forgot the type, e.g. int. > int *EleTopo = new int[seneles*ncn]; > > Once this block is filled with valid data, once those > values are printed and observed they seem to be > correct. But in between of the code, retrieving the > same had returned some junk values for some of the > indexes. Obviously you are doing something wrong. We must see the code. Ioannis * Ioannis Vranos * Programming pages: http://www.noicys.cjb.net * Alternative URL: http://run.to/noicys |
|
From: Jason H. <jas...@nt...> - 2002-06-09 22:12:35
|
You should check that "EleTopo" is non-null before using it, just in case the memory allocation failed. I can't remember if the current MinGW used with Dev-C++ is standards compliant enought to throw an exception when memory allocation fails. You need to be sure that you aren't trying to access beyond the bounds of your array also, or alternatively invalidating either the memory or the pointer at some point and then trying to reuse it. Jason. ----- Original Message ----- From: "Ioannis Vranos" <no...@ho...> To: "Dev-C++ Mailing List" <dev...@li...> Sent: Sunday, June 09, 2002 7:06 PM Subject: RE: [Dev-C++] memory violation.... > -----Original Message----- > From: dev...@li... > [mailto:dev...@li...] On Behalf > Of Raghunath Marella.S > Sent: Sunday, June 09, 2002 12:42 PM > To: dev...@li... > Subject: [Dev-C++] memory violation.... > > > Hi, > > I have been facing a simple but strange problem in my > C++ code. I have declared an integer pointer and whose > size has been fixed at a large value dynamically. > Like... > > seneles = 1000,ncn = 6; You forgot the type, e.g. int. > int *EleTopo = new int[seneles*ncn]; > > Once this block is filled with valid data, once those > values are printed and observed they seem to be > correct. But in between of the code, retrieving the > same had returned some junk values for some of the > indexes. Obviously you are doing something wrong. We must see the code. Ioannis * Ioannis Vranos * Programming pages: http://www.noicys.cjb.net * Alternative URL: http://run.to/noicys _______________________________________________________________ Don't miss the 2002 Sprint PCS Application Developer's Conference August 25-28 in Las Vegas - http://devcon.sprintpcs.com/adp/index.cfm?source=osdntextlink _______________________________________________ Dev-cpp-users mailing list Dev...@li... https://lists.sourceforge.net/lists/listinfo/dev-cpp-users |
|
From: Ioannis V. <no...@ho...> - 2002-06-09 23:08:11
|
> -----Original Message----- > From: dev...@li... > [mailto:dev...@li...] On Behalf > Of Jason Hardman > Sent: Monday, June 10, 2002 1:12 AM > To: Dev-C++ > Subject: Re: [Dev-C++] memory violation.... > > > You should check that "EleTopo" is non-null before using it, > just in case the memory allocation > failed. I can't remember if the current MinGW used with > Dev-C++ is standards compliant enought to > throw an exception when memory allocation fails. It is, and no, 0 can not be returned by new. Ioannis * Ioannis Vranos * Programming pages: http://www.noicys.cjb.net * Alternative URL: http://run.to/noicys |
|
From: Raghunath Marella.S <ms...@ya...> - 2002-06-10 07:25:17
|
Hi Ioannis/Jason/Others,
Please find the piece of code where the memory
violation is seen. I have not deleted the pointer and
accessed after that. No error has been done from my
side.
int *EleTopo=new int[seneles*sennds];
char *NodeSign=new char[this->NNodes+1];
Similarly few other variables have been dynamically
allocated....
for(int i=0;i<seneles;i++)
for(int j=0;j<ncn;j++)
{
int cuEl = (theSubEle+ise)->SEtopo[i];
*(EleTopo+i*ncn+j)=(theElement+cuEl-1)->topo[j];
NodeSign[(theElement+cuEl-1)->topo[j]] =
'P';//All nodes are marked as positive initially.
}
After this piece of code, 'EleTopo' values are
printed and they confirmed to be correct.
After few lines of other code.....
for(int iel=seneles-1;iel>=0 ; iel--)
{
int iLocalForEle=0;
int curElement = (theSubEle+ise)->SEtopo[iel];
if(iel==seneles-1)
coutf << "\n Final element in the SE is...:"<<iel;
//newAdd
int AnyNode;
int noOfDf = 0;
NoOfNegNodes =0;
for(int j=0;j<ncn;j++)
{
AnyNode = *(EleTopo+iel*ncn+j);
int isThisNodeABN = 0;// is this a boundary node
of SE.
for(int it=0;it<SeBNodes;it++)
{
if(AnyNode ==
((theSubEle+ise)->SEBounNodes[it]))
{
isThisNodeABN = 1;
break;
}
else
isThisNodeABN = 0;
}
if(isThisNodeABN==0)
{
****** if(NodeSign[*(EleTopo+iel*ncn+j)]=='N')
{
//Do nothing...
}
else
{
NodeSign[*(EleTopo+iel*ncn+j)]='N';
int nde = *(EleTopo+iel*ncn+j);
****** *(EleTopo+iel*ncn+j) = -*(EleTopo+iel*ncn+j);
NoOfNegNodes++;
//saving the the dfs that are going to be removed
after this element being assembled.
for(int ir=0;ir<NDF;ir++)
{
*(DfToRemove+iel*(ncn*NDF)+iLocalForEle)=
(nde-1)*NDF+ir;
iLocalForEle++;
}
noOfDf+=NDF;
}
}
else
cout <<"\n this Node is a BoundaryNode of
SE...:"<<AnyNode<<".."<<ise;
}
noOfDf = iLocalForEle;
NoNegDf[iel]=NDF*NoOfNegNodes;
}//sign check ends here.....
The problem appears in the line marked with ******.
Actually this problem is seen only in some of the
problems. To give you more idea, I have tested this
code as standalone, it worked fine for 'EleTopo'
sized upto 3200*6, then it worked properly and no
memory violation was observed.
But, when the code used as a server implementation in
CORBA environment, the so called memory violation
took place when 'EleTopo' was sized to 800*6. Briefly,
'EleTopo' is like an two dimentional array of size
[800][6]. I have to access data in 'EleTopo' from the
end, ie., from [800][]. The values are correct till
[48][6] and started giving junk values for some of the
indexes between [47][] to [0][].
I have shown in array form just for illustration
purpose.
I am clueless how this memory violation is taking
place in some cases only.
Will you pls suggest me what is going wrong in this.
thanks & regards,
Raghunath
--- Ioannis Vranos <no...@ho...> wrote:
> > -----Original Message-----
> > From: dev...@li...
> > [mailto:dev...@li...]
> On Behalf
> > Of Jason Hardman
> > Sent: Monday, June 10, 2002 1:12 AM
> > To: Dev-C++
> > Subject: Re: [Dev-C++] memory violation....
> >
> >
> > You should check that "EleTopo" is non-null before
> using it,
> > just in case the memory allocation
> > failed. I can't remember if the current MinGW used
> with
> > Dev-C++ is standards compliant enought to
> > throw an exception when memory allocation fails.
>
>
> It is, and no, 0 can not be returned by new.
>
>
> Ioannis
>
> * Ioannis Vranos
> * Programming pages: http://www.noicys.cjb.net
> * Alternative URL: http://run.to/noicys
>
>
_______________________________________________________________
>
> Don't miss the 2002 Sprint PCS Application
> Developer's Conference
> August 25-28 in Las Vegas -
>
http://devcon.sprintpcs.com/adp/index.cfm?source=osdntextlink
>
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
>
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
__________________________________________________
Do You Yahoo!?
Yahoo! - Official partner of 2002 FIFA World Cup
http://fifaworldcup.yahoo.com
|
|
From: Ioannis V. <no...@ho...> - 2002-06-10 09:50:27
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf
> Of Raghunath Marella.S
> Sent: Monday, June 10, 2002 10:25 AM
> To: Dev-C++ Mailing List
> Subject: RE: [Dev-C++] memory violation....
>
>
> Hi Ioannis/Jason/Others,
>
> Please find the piece of code where the memory
> violation is seen. I have not deleted the pointer and
> accessed after that. No error has been done from my
> side.
It is 100% some logical error (bug). To avoid situations like this and
have bullet proof code use the standard library containers instead of
explicit memory allocation and deallocation. Examples can be seen below.
#include <vector>
using namespace std;
> int *EleTopo=new int[seneles*sennds];
vector<int>EleTopo(seneles*sennds);
> char *NodeSign=new char[this->NNodes+1];
list, or vector, or string, or something...
> Similarly few other variables have been dynamically
> allocated....
>
> for(int i=0;i<seneles;i++)
> for(int j=0;j<ncn;j++)
> {
> int cuEl = (theSubEle+ise)->SEtopo[i];
>
> *(EleTopo+i*ncn+j)=(theElement+cuEl-1)->topo[j];
> NodeSign[(theElement+cuEl-1)->topo[j]] =
> 'P';//All nodes are marked as positive initially.
> }
> After this piece of code, 'EleTopo' values are
> printed and they confirmed to be correct.
It is too messy! *(EleTopo+i*ncn+j)? What about the equivallent
EleTopo[i*ncn+j] (which is somewhat messy too). Obviously you come from
a hardcore C background and carry all that low stuff bagage. :)
>
> After few lines of other code.....
>
> for(int iel=seneles-1;iel>=0 ; iel--)
> {
> int iLocalForEle=0;
> int curElement = (theSubEle+ise)->SEtopo[iel];
> if(iel==seneles-1)
> coutf << "\n Final element in the SE is...:"<<iel;
> //newAdd
> int AnyNode;
>
>
> int noOfDf = 0;
> NoOfNegNodes =0;
> for(int j=0;j<ncn;j++)
> {
> AnyNode = *(EleTopo+iel*ncn+j);
> int isThisNodeABN = 0;// is this a boundary node
> of SE.
> for(int it=0;it<SeBNodes;it++)
> {
> if(AnyNode ==
> ((theSubEle+ise)->SEBounNodes[it]))
> {
> isThisNodeABN = 1;
> break;
> }
> else
> isThisNodeABN = 0;
> }
>
> if(isThisNodeABN==0)
> {
>
> ****** if(NodeSign[*(EleTopo+iel*ncn+j)]=='N')
> {
> //Do nothing...
> }
> else
> {
> NodeSign[*(EleTopo+iel*ncn+j)]='N';
> int nde = *(EleTopo+iel*ncn+j);
> ****** *(EleTopo+iel*ncn+j) =
> -*(EleTopo+iel*ncn+j);
> NoOfNegNodes++;
>
> //saving the the dfs that are going to
> be removed
> after this element being assembled.
> for(int ir=0;ir<NDF;ir++)
> {
> *(DfToRemove+iel*(ncn*NDF)+iLocalForEle)=
> (nde-1)*NDF+ir;
> iLocalForEle++;
> }
> noOfDf+=NDF;
> }
> }
> else
> cout <<"\n this Node is a BoundaryNode of
> SE...:"<<AnyNode<<".."<<ise;
> }
> noOfDf = iLocalForEle;
> NoNegDf[iel]=NDF*NoOfNegNodes;
> }//sign check ends here.....
If any exception is thrown from somewhere there, much of all those
allocated resources remain allocated.
>
>
> The problem appears in the line marked with ******.
> Actually this problem is seen only in some of the
> problems. To give you more idea, I have tested this
> code as standalone, it worked fine for 'EleTopo'
> sized upto 3200*6, then it worked properly and no
> memory violation was observed.
>
> But, when the code used as a server implementation in
> CORBA environment, the so called memory violation
> took place when 'EleTopo' was sized to 800*6. Briefly,
>
> 'EleTopo' is like an two dimentional array of size
> [800][6]. I have to access data in 'EleTopo' from the
> end, ie., from [800][].
[799][].
The values are correct till
> [48][6] and started giving junk values for some of the
> indexes between [47][] to [0][].
>
> I have shown in array form just for illustration
> purpose.
>
> I am clueless how this memory violation is taking
> place in some cases only.
>
> Will you pls suggest me what is going wrong in this.
What is going wrong basically here is that you use C++ in the place of
assembly programming. Deal C++ as a high level programming language and
you can even gain some time/space! I suggest you read "The C++
Programming Language" 3rd Edition or Special Edition. And be prepared
for a life change. :)
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.cjb.net
* Alternative URL: http://run.to/noicys
|
|
From: Raghunath Marella.S <ms...@ya...> - 2002-06-10 11:24:02
|
Hi,
Please find my reply embedded...
--- Ioannis Vranos <no...@ho...> wrote:
> > -----Original Message-----
> > From: dev...@li...
> > [mailto:dev...@li...]
> On Behalf
> > Of Raghunath Marella.S
> > Sent: Monday, June 10, 2002 10:25 AM
> > To: Dev-C++ Mailing List
> > Subject: RE: [Dev-C++] memory violation....
> >
> >
> > Hi Ioannis/Jason/Others,
> >
> > Please find the piece of code where the memory
> > violation is seen. I have not deleted the pointer
> and
> > accessed after that. No error has been done from
> my
> > side.
>
>
> It is 100% some logical error (bug). To avoid
> situations like this and
> have bullet proof code use the standard library
> containers instead of
> explicit memory allocation and deallocation.
> Examples can be seen below.
Logical error in my code or VC++ COmpiler??
>
> #include <vector>
>
> using namespace std;
>
>
> > int *EleTopo=new int[seneles*sennds];
>
> vector<int>EleTopo(seneles*sennds);
>
>
> > char *NodeSign=new char[this->NNodes+1];
>
> list, or vector, or string, or something...
>
>
>
> > Similarly few other variables have been
> dynamically
> > allocated....
> >
> > for(int i=0;i<seneles;i++)
> > for(int j=0;j<ncn;j++)
> > {
> > int cuEl = (theSubEle+ise)->SEtopo[i];
> >
> >
> *(EleTopo+i*ncn+j)=(theElement+cuEl-1)->topo[j];
> > NodeSign[(theElement+cuEl-1)->topo[j]] =
> > 'P';//All nodes are marked as positive initially.
> > }
> > After this piece of code, 'EleTopo' values are
> > printed and they confirmed to be correct.
>
>
> It is too messy! *(EleTopo+i*ncn+j)? What about the
> equivallent
> EleTopo[i*ncn+j] (which is somewhat messy too).
> Obviously you come from
> a hardcore C background and carry all that low stuff
> bagage. :)
Yes, it is equivalent to EleTopo[i][j] where are 'j'
ranges between 0 to ncn.
>
>
>
> >
> > After few lines of other code.....
> >
> > for(int iel=seneles-1;iel>=0 ; iel--)
> > {
> > int iLocalForEle=0;
> > int curElement = (theSubEle+ise)->SEtopo[iel];
> > if(iel==seneles-1)
> > coutf << "\n Final element in the SE
> is...:"<<iel;
> > //newAdd
> > int AnyNode;
> >
> >
> > int noOfDf = 0;
> > NoOfNegNodes =0;
> > for(int j=0;j<ncn;j++)
> > {
> > AnyNode = *(EleTopo+iel*ncn+j);
> > int isThisNodeABN = 0;// is this a boundary
> node
> > of SE.
> > for(int it=0;it<SeBNodes;it++)
> > {
> > if(AnyNode ==
> > ((theSubEle+ise)->SEBounNodes[it]))
> > {
> > isThisNodeABN = 1;
> > break;
> > }
> > else
> > isThisNodeABN = 0;
> > }
> >
> > if(isThisNodeABN==0)
> > {
> >
> > ****** if(NodeSign[*(EleTopo+iel*ncn+j)]=='N')
> > {
> > //Do nothing...
> > }
> > else
> > {
> > NodeSign[*(EleTopo+iel*ncn+j)]='N';
> > int nde = *(EleTopo+iel*ncn+j);
> > ****** *(EleTopo+iel*ncn+j) =
> > -*(EleTopo+iel*ncn+j);
> > NoOfNegNodes++;
> >
> > //saving the the dfs that are going to
> > be removed
> > after this element being assembled.
> > for(int ir=0;ir<NDF;ir++)
> > {
> > *(DfToRemove+iel*(ncn*NDF)+iLocalForEle)=
> > (nde-1)*NDF+ir;
> > iLocalForEle++;
> > }
> > noOfDf+=NDF;
> > }
> > }
> > else
> > cout <<"\n this Node is a BoundaryNode of
> > SE...:"<<AnyNode<<".."<<ise;
> > }
> > noOfDf = iLocalForEle;
> > NoNegDf[iel]=NDF*NoOfNegNodes;
> > }//sign check ends here.....
>
>
>
> If any exception is thrown from somewhere there,
> much of all those
> allocated resources remain allocated.
>
>
>
> >
> >
> > The problem appears in the line marked with
> ******.
> > Actually this problem is seen only in some of the
> > problems. To give you more idea, I have tested
> this
> > code as standalone, it worked fine for 'EleTopo'
> > sized upto 3200*6, then it worked properly and no
> > memory violation was observed.
> >
> > But, when the code used as a server
> implementation in
> > CORBA environment, the so called memory violation
> > took place when 'EleTopo' was sized to 800*6.
> Briefly,
> >
> > 'EleTopo' is like an two dimentional array of
> size
> > [800][6]. I have to access data in 'EleTopo' from
> the
> > end, ie., from [800][].
>
> [799][].
yes, it is [799][]
>
>
>
> The values are correct till
> > [48][6] and started giving junk values for some of
> the
> > indexes between [47][] to [0][].
> >
> > I have shown in array form just for illustration
> > purpose.
> >
> > I am clueless how this memory violation is taking
> > place in some cases only.
> >
> > Will you pls suggest me what is going wrong in
> this.
>
>
> What is going wrong basically here is that you use
> C++ in the place of
> assembly programming. Deal C++ as a high level
> programming language and
> you can even gain some time/space! I suggest you
> read "The C++
> Programming Language" 3rd Edition or Special
> Edition. And be prepared
> for a life change. :)
Thank you.
>
>
> Ioannis
>
regards,
Raghunath
=== message truncated ===
__________________________________________________
Do You Yahoo!?
Yahoo! - Official partner of 2002 FIFA World Cup
http://fifaworldcup.yahoo.com
|