error of memory

M

Mohsen

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==HS[j])
{
flag=true;
break;
}

if(DT<HS[j] && DT[i+1]>HS[j])
{
++j;
if(DT<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>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
 
D

David Harmon

On 22 Sep 2006 18:13:17 -0700 in comp.lang.c++, "Mohsen"
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;
 
V

vfunc

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?
 
M

Mohsen

It is a small part of a big program for simulation on genetic
information data.
Thanks,
Mohsen
 
M

Mohsen

I did it. Still there is a problem in line 64.
Thanks,

David said:
On 22 Sep 2006 18:13:17 -0700 in comp.lang.c++, "Mohsen"
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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top