PLease help me.Wots the problem with following code....?

A

alankrit

Helo
This is a code for Radix sort. n it is giving a run-time Error :
"Run-Time Check Failure #2 - Stack around the variable 'count' was
corrupted."

I have cross checked this code a millions of times.
what is the problem here..??????????????????

#include "stdafx.h"
#include<iostream>
#include<stdlib.h>

using namespace std;

#define CHAR_BIT 8
#define UCHAR_MAX 0xff
#define RANGE ((1U << CHAR_BIT)+1)
#define digit(x,i) ( (x) >> ( (i) * CHAR_BIT) ) & UCHAR_MAX
#define SIZE 100

void radixsort(long *a, long );
void radixpass(long *a, long *aux, long , int radix);
void makerandom(long *a, long );

int _tmain(int argc, _TCHAR* argv[])
{
long data[SIZE];
makerandom(data,SIZE);

radixsort(data,SIZE);

for(int i=0;i<SIZE;i++)
cout<<data<<"\n";

return 0;

}

void makerandom(long *a,long N)
{
for(int i=0;i<N;i++)
a=rand();

}

void radixsort(long *a,long N)
{
long *aux=(long *)malloc(N*sizeof(long));

for(int radix=0;radix< sizeof(*a);radix++)
radixpass(a,aux,SIZE,radix);

free(aux);

}

void radixpass(long *a,long *aux, long N, int radix)
{
long count[RANGE];
long *cp=count;

for(int i=0;i<RANGE;i++,cp++)
*cp=0;

for(int i=0;i<N;i++)
count[ digit( a, radix ) +1 ]++;

for(int i=0;i< RANGE;i++)
count[i+1]+=count;

for(int i=0;i<N;i++)
aux[ count[digit(a,radix)]++ ]=a;

for(int i=0;i<N;i++)
a=aux;
}
 
K

Kai-Uwe Bux

Helo
This is a code for Radix sort. n it is giving a run-time Error :
"Run-Time Check Failure #2 - Stack around the variable 'count' was
corrupted."

I have cross checked this code a millions of times.
what is the problem here..??????????????????

#include "stdafx.h"
#include<iostream>
#include<stdlib.h>

using namespace std;

#define CHAR_BIT 8
#define UCHAR_MAX 0xff
#define RANGE ((1U << CHAR_BIT)+1)
#define digit(x,i) ( (x) >> ( (i) * CHAR_BIT) ) & UCHAR_MAX
#define SIZE 100

These macros are poor form.
void radixsort(long *a, long );
void radixpass(long *a, long *aux, long , int radix);
void makerandom(long *a, long );

int _tmain(int argc, _TCHAR* argv[])

_tmain is non-standard -- like many things in this program.
{
long data[SIZE];
makerandom(data,SIZE);

radixsort(data,SIZE);

for(int i=0;i<SIZE;i++)
cout<<data<<"\n";

return 0;

}

void makerandom(long *a,long N)
{
for(int i=0;i<N;i++)
a=rand();

}

void radixsort(long *a,long N)
{
long *aux=(long *)malloc(N*sizeof(long));

for(int radix=0;radix< sizeof(*a);radix++)
radixpass(a,aux,SIZE,radix);


Are you sure you want SIZE here? Should that be N?
free(aux);

}

void radixpass(long *a,long *aux, long N, int radix)
{
long count[RANGE];
long *cp=count;

for(int i=0;i<RANGE;i++,cp++)
*cp=0;

for(int i=0;i<N;i++)
count[ digit( a, radix ) +1 ]++;

for(int i=0;i< RANGE;i++)
count[i+1]+=count;

for(int i=0;i<N;i++)
aux[ count[digit(a,radix)]++ ]=a;

for(int i=0;i<N;i++)
a=aux;
}



Best

Kai-Uwe Bux
 
C

codergem

Well i have made the following changes.But still its not working,
giving the same run-time error.

and header files are all correct.

// Radix_Sort_1.cpp : Defines the entry point for the console
application.
//

#include "stdafx.h"
#include<iostream>
#include<stdlib.h>

using namespace std;

#define digit(x,i) (( (x) >> ( (i) * 8 ) ) & 0xff )
#define SIZE 100

void radixsort(long *a, long );
void radixpass(long *a, long *aux, long , int radix);
void makerandom(long *a, long );

int _tmain(int argc, _TCHAR* argv[])
{
long data[SIZE];
makerandom(data,SIZE);

radixsort(data,SIZE);

for(int i=0;i<SIZE;i++)
cout<<data<<"\n";

return 0;

}

void makerandom(long *a,long N)
{
for(int i=0;i<N;i++)
a=rand();

}

void radixsort(long *a,long N)
{
long *aux=(long *)malloc(N*sizeof(long));

for(int radix=0;radix< sizeof(*a);radix++)
radixpass(a,aux,N,radix);

free(aux);

}

void radixpass(long *a,long *aux, long N, int radix)
{
long count[257];
long *cp=count;

for(int i=0;i<257;i++,cp++)
*cp=0;

for(int i=0;i<N;i++)
count[ digit( a, radix ) +1 ]++;

for(int i=0;i< 257;i++)
count[i+1]+=count;

for(int i=0;i<N;i++)
aux[ count[digit(a,radix)]++ ]=a;

for(int i=0;i<N;i++)
a=aux;

}

Syntax error.
#define CHAR_BIT 8
#define UCHAR_MAX 0xff

Hey BTW i dont know how to use these constants.Cud u plz temme how to
use
it?
these r defined in limits.h
thanks.
 
K

Kai-Uwe Bux

codergem said:
Well i have made the following changes.But still its not working,
giving the same run-time error.

and header files are all correct.

// Radix_Sort_1.cpp : Defines the entry point for the console
application.
//

#include "stdafx.h"
#include<iostream>
#include<stdlib.h>

Replace by:

#include <iostream>
#include <cstdlib>
#include <climits>


using namespace std;

#define digit(x,i) (( (x) >> ( (i) * 8 ) ) & 0xff )
#define SIZE 100

Replace by:

using namespace std;

template < typename T >
inline
T digit ( T x, unsigned short i ) {
return (( (x) >> ( (i) * CHAR_BIT ) ) & UCHAR_MAX );
}

const unsigned long SIZE = 100;


void radixsort(long *a, long );
void radixpass(long *a, long *aux, long , int radix);
void makerandom(long *a, long );

Ok.

int _tmain(int argc, _TCHAR* argv[])

Replace by:

int main(int argc, char* argv[])

{
long data[SIZE];
makerandom(data,SIZE);

radixsort(data,SIZE);

for(int i=0;i<SIZE;i++)
cout<<data<<"\n";

return 0;

}

void makerandom(long *a,long N)
{
for(int i=0;i<N;i++)
a=rand();

}

void radixsort(long *a,long N)
{
long *aux=(long *)malloc(N*sizeof(long));

for(int radix=0;radix< sizeof(*a);radix++)
radixpass(a,aux,N,radix);

free(aux);

}

void radixpass(long *a,long *aux, long N, int radix)
{
long count[257];
long *cp=count;

for(int i=0;i<257;i++,cp++)
*cp=0;

for(int i=0;i<N;i++)
count[ digit( a, radix ) +1 ]++;

for(int i=0;i< 257;i++)
count[i+1]+=count;

for(int i=0;i<N;i++)
aux[ count[digit(a,radix)]++ ]=a;

for(int i=0;i<N;i++)
a=aux;

}

Syntax error.
#define CHAR_BIT 8
#define UCHAR_MAX 0xff


#include said:
Hey BTW i dont know how to use these constants.Cud u plz temme how to
use
it?
these r defined in limits.h

???. I am not a native speaker of English. It will greatly ease
communication if you could write something that resembles more closely the
language I my teachers taught me.


Best

Kai-Uwe Bux
 
H

Heinz Ozwirk

void radixpass(long *a,long *aux, long N, int radix)
{
long count[RANGE];
long *cp=count;

for(int i=0;i<RANGE;i++,cp++)
*cp=0;

for(int i=0;i<N;i++)
count[ digit( a, radix ) +1 ]++;

for(int i=0;i< RANGE;i++)
count[i+1]+=count;


What do you think will happen when i + 1 == RANGE?
for(int i=0;i<N;i++)
aux[ count[digit(a,radix)]++ ]=a;

for(int i=0;i<N;i++)
a=aux;
}


Heinz
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top