Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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??
 
[From asker's "solution"]
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
Edited 3-Dec-12 5:54am
lewax0046.1K
v2
Comments
CPallini at 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: bad
good
Please Sign up or sign in to vote.

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.
 
Try this instead:
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
}
  Permalink  
v2
Comments
missak boyajian at 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 at 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 at 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 at 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 at 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 at 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 at 3-Dec-12 11:01am
   
I don't know. It's working fine and sorting fine without the delete. Thanks for the help.
Quirkafleeg at 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 at 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 at 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 at 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.
Sergey Alexandrovich Kryukov at 3-Dec-12 15:18pm
   
Of course. "Have only a little memory leak" is like being "a little pregnant". :-)
--SA
Albert Holguin at 3-Dec-12 15:55pm
   
lol, that's a scary analogy
Sergey Alexandrovich Kryukov at 3-Dec-12 16:41pm
   
Well, those who think a little leak would be fine need to get scared... :-)
--SA
Sergey Alexandrovich Kryukov at 3-Dec-12 15:17pm
   
Nicely explained, my 5.
--SA
Sergey Alexandrovich Kryukov at 3-Dec-12 16:47pm
   
"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 at 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: bad
good
Please Sign up or sign in to vote.

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);
 
...
 
}
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

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.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 7,800
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,790


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 6 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100