12,749,754 members (34,731 online)
Rate this:
See more:
```void CountSort(int A[],int B[], int k, int size)
{
const int mx=k;
int C[mx];//error:expression must have a constant value!!!

...

}
```

mx is constant! What is wrong here??

```void CountSort(int[],int[], int , int);

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

const int size=12;

int A[size]={0,2,3,1,7,9,6,45,12,10,20,15};
int B[size];

int max=45;

CountSort(A,B,max,size);

//for(int i = 0 ; i<size;> // cout<<A[i]<<" ";
for(int i = 1 ; i<size;> cout<<B[i]<<" ";

return 0;
}

void CountSort(int A[],int B[], int k, int size)
{

int *C = new int[k];

for(int i =0;i<=k;i++)
C[i]=0;

for(int j =1; j C[A[j]] = C[A[j]] + 1 ;

for (int i = 1; i<=k ;i++)
C[i]= C[i] + C[i-1] ;

for (int j = size-1 ;j>0;j--)
{
B[C[A[j]]] = A[j] ;
C[A[j]] = C[A[j]] - 1 ;
}

//delete[] C;

}```
Posted 3-Dec-12 5:06am
Updated 3-Dec-12 5:54am
lewax0046.1K
v2
CPallini 3-Dec-12 12:23pm

Please post the actual code (with proper formatting - for instance `for` loop line is incomplete in the above code).

Rate this:

## Solution 1

No, it doesn't, not really anyways. I'm a little surprised the error isn't the line before that honestly.

But let's break it down: the value of mx is k, but the compiler does not know what k will be until runtime. This might be allowable, but the compiler has no idea how big to make C, which means it can't continue. If you need a size that's unknown at compile time, you need a dynamic array.

```void CountSort(int A[],int B[], int k, int size)
{
int* C = new int[k];

...

delete[] C; // clean up the memory you allocated when you're done with it
}```
v2
missak boyajian 3-Dec-12 10:26am

Thanks it worked. But one more problem. When i use delete[] C ==> its giving error when running the program. When I do not use it, it works fine.
lewax00 3-Dec-12 10:30am

If it's a small program it probably won't matter (i.e. if this function is only run a few times) because the memory will be reclaimed when the program exists, but what is the error?
Albert Holguin 3-Dec-12 15:02pm

I'd fix the problem though... it's better to spend the time to figure out what's wrong than to learn to live with memory leaks.
missak boyajian 3-Dec-12 10:34am

Debug Error!
Program:...

HEAP CORRUPTION DETECED: after Normal block...
CRT detected that the application wrote to memory after end of heap buffer.
lewax00 3-Dec-12 10:49am

That sounds like you're writing something after the end of C, it's hard to say without seeing the rest of the code.
lewax00 3-Dec-12 10:56am

In a few places while accessing C you use "i <= k", this should be "i < k", because the indexes for an array of size k will be 0 to (k - 1). Replacing those should fix that problem I think.
missak boyajian 3-Dec-12 11:01am

I don't know. It's working fine and sorting fine without the delete. Thanks for the help.
Quirkafleeg 3-Dec-12 12:12pm

Heed the advice of lewax00... THE most common problem in any commercial C++ project are memory leaks. If you are happy to program with bad habits then you are just asking for a painful future for you and all work colleagues.

Also the final for loop with "j" should be "j >= 0", not "j > 0" - unless you want to ignore the first element.
missak boyajian 3-Dec-12 13:00pm

Hey I already found the solution myself. At the beginning I wrote :
int *C = new int[k];
It should be:
int *C = new int[k+1];
and now it works perfectly with delete[] function. Thanks again.
Albert Holguin 3-Dec-12 15:04pm

This will only work fine in debug mode... if you run it in release mode, it will crash. This is because debug always allocates an extra bit of memory to all of your allocations so that it has the opportunity to alert you of your error without crashing when you've overrun the buffer.
Albert Holguin 3-Dec-12 15:05pm

+5... but I'd never recommend to someone to leave a memory leak, even if small... builds bad coding habits.

Of course. "Have only a little memory leak" is like being "a little pregnant". :-)
--SA
Albert Holguin 3-Dec-12 15:55pm

lol, that's a scary analogy

Well, those who think a little leak would be fine need to get scared... :-)
--SA

Nicely explained, my 5.
--SA

"Nobody is answering" sounds offensive to lewax00... :-) What makes you think so?

You got more than enough of attention and a valuable advice. If you need clarification and have other concerns, just ask some more questions, instead of blaming... who knows who.

And please realize -- it's not to hurt you, but to help you to look at it more realistically -- having your problems with creation of yet another Web site is not exactly the most critical issue for the community. All of us have a lot of problems to address, and some of them are more important...

Good luck,
--SA
lewax00 3-Dec-12 22:21pm

I don't know anything about ASP.Net and very little about networking, so I don't have any answers on that topic.
Rate this:

## Solution 4

Arrays can only be initialized by using compile time constants like
```static const int mx = 9;
int C[mx];```

But you cannot assigned a non-static variable to a static constant.
`static const int mx = k; // Invalid if k is a function parameter`

The most simple solution is to use the Standard Template Library which provides the std::vector template:
```#include <vector>

void CountSort(int A[],int B[], int k, int size)
{
std::vector<int> C(k);

...

}```
v2
Rate this:

## Solution 3

```void CountSort(int A[],int B[], int k, int size)
{
const int mx=k;
int C[mx];//error:expression must have a constant value!!!

...

}```

try using new for this

void CountSort(int A[],int B[], int k, int size)
{
const int mx=k;
//int C[mx];//error:expression must have a constant value!!!

int* C = new int[k];//or new int[mx] anyway is fine
//You can use it as a normal arrray
C[somevalue]=someoperation;

...

}

and while you're at it do have a look at dynamic memory allocation.

Top Experts
Last 24hrsThis month
 Karthik Bangalore 275 Graeme_Grant 255 F-ES Sitecore 215 OriginalGriff 165 Afzaal Ahmad Zeeshan 125
 OriginalGriff 3,833 Peter Leow 2,871 Karthik Bangalore 2,065 ppolymorphe 2,052 Richard MacCutchan 1,409