By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,654 Members | 1,840 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,654 IT Pros & Developers. It's quick & easy.

error of memory

P: n/a
Hello everyone,

I have written the followin program:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{

#define M 40
#define R 5

srand(time(NULL));
srand(6887700);

int *HS=new int [R+1];
int *DT=new int [M+1];

int i, j, k, c, rep, r, m, Temp;
bool flag;

for(m=1;m<=M;++m) DT[m]=(rand()%1000)+1;

for (m=1;m<=M;++m)
{
k=m;
for (j=m+1;j<=M;++j)
{
if (DT[j]<DT[k]) k=j;
}
Temp=DT[m];
DT[m]=DT[k];
DT[k]=Temp;
}

for(m=1;m<=M;++m) printf("%d\n",DT[m]);

for(rep=1;;++rep)
{
flag=false;
for(r=1;r<=R;++r) HS[r]=(rand()%1000)+1;

for(r=1;r<=R;++r)
{
k=r;
for (j=r+1;j<=R;++j)
{
if (HS[j]<HS[k]) k=j;
}
Temp=HS[r];
HS[r]=HS[k];
HS[k]=Temp;
}
for(j=i=1;i<M && j<=R;++i)
{
if(DT[i]==HS[j])
{
flag=true;
break;
}

if(DT[i]<HS[j] && DT[i+1]>HS[j])
{
++j;
if(DT[i]<HS[j] && DT[i+1]>HS[j])
{
flag=true;
break;
}
}
}

if(flag) continue;
for(c=0,j=i=1;j<=R;++i,++c)
{
if(DT[i]>HS[j])
{
if(c<2)
{
flag=true;
break;
}
++j;
c=0;
}
}

if(flag) continue;
for(c=0;i<=M;++i,++c);
if(c<2) flag=true;
if(!flag) break;
}
printf("HS Simulator: %d Replication\n",rep);
for(r=1;r<=R;++r) printf("HS[%3d]:[%9d]\n",r,HS[r]);
printf("\n");

delete [] HS;
delete [] DT;
return 0;
}
and when I test it with "valgrind" it gives the errors:

==28678== Memcheck, a memory error detector.
==28678== Copyright (C) 2002-2005, and GNU GPL'd, by Julian Seward et
al.
==28678== Using LibVEX rev 1471, a library for dynamic binary
translation.
==28678== Copyright (C) 2004-2005, and GNU GPL'd, by OpenWorks LLP.
==28678== Using valgrind-3.1.0, a dynamic binary instrumentation
framework.
==28678== Copyright (C) 2000-2005, and GNU GPL'd, by Julian Seward et
al.
==28678== For more details, rerun with: -v
==28678==
==28678== My PID = 28678, parent PID = 28088. Prog and args are:
==28678== f
==28678==
==28678== Invalid read of size 4
==28678== at 0x400A59: main (foo.cpp:64)
==28678== Address 0x4A30048 is 0 bytes after a block of size 24
alloc'd
==28678== at 0x4905A0A: operator new[](unsigned long)
(vg_replace_malloc.c:201)
==28678== by 0x400765: main (foo.cpp:14)
==28678==
==28678== ERROR SUMMARY: 5 errors from 1 contexts (suppressed: 4 from
1)
==28678== malloc/free: in use at exit: 0 bytes in 0 blocks.
==28678== malloc/free: 2 allocs, 2 frees, 188 bytes allocated.
==28678== For counts of detected errors, rerun with: -v
==28678== No malloc'd blocks -- no leaks are possible.

Can anybody tell me what is the problem in here?
Thanks,
Mohsen

Sep 23 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 22 Sep 2006 18:13:17 -0700 in comp.lang.c++, "Mohsen"
<ja*******@gmail.comwrote,
for(m=1;m<=M;++m) DT[m]=(rand()%1000)+1;
Subscripts run from 0 to size-1

for(m=0;m<M;++m) DT[m]=(rand()%1000)+1;

Sep 23 '06 #2

P: n/a
and...
int *HS=new int [R];
int *DT=new int [M];
and more reindexing.

Nice code, it looks like something out of numerical recipes, what does
it do?

Sep 23 '06 #3

P: n/a
It is a small part of a big program for simulation on genetic
information data.
Thanks,
Mohsen

vf***@talktalk.net wrote:
and...
int *HS=new int [R];
int *DT=new int [M];
and more reindexing.

Nice code, it looks like something out of numerical recipes, what does
it do?
Sep 23 '06 #4

P: n/a
I did it. Still there is a problem in line 64.
Thanks,

David Harmon wrote:
On 22 Sep 2006 18:13:17 -0700 in comp.lang.c++, "Mohsen"
<ja*******@gmail.comwrote,
for(m=1;m<=M;++m) DT[m]=(rand()%1000)+1;

Subscripts run from 0 to size-1

for(m=0;m<M;++m) DT[m]=(rand()%1000)+1;
Sep 23 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.