dynamic memory allocation

I have an error saying expression must have a constant value could any one please help me out

void transpose_vec(double matrix[size_b_row1], double matrix_t[1][size_b_row1])
{
int size_b_row1;
using namespace std;
std::vector<int> temp(size_b_row1);
int i;
for(i=0;i<size_b_row1;i++)
{
matrix_t[1][i] = matrix[i];
}
}
Last edited on
what is size_b_row1?

you have declared input matrices of size [size_b_row1]. This I assume should be a constant value. Then you have declared this same identifier as an int variable.

Also, without initializing this variable, you are using it in the for loop. I suggest you follow the matrix declaration as described in http://www.cplusplus.com/doc/tutorial/arrays/.
void transpose_vec (double matrix[size_b_row1],
double matrix_t[1][size_b_row1])

{
int size_b_row1;
int i;
for(i=0;i<size_b_row1;i++)
{
matrix_t[1][i] = matrix[i];
}
}
this is actually wat the code was i tried a few things like vector got confused but i still get the same error expression must have a constant value could you please help me i have been debugging this error from 4 days
Last edited on
When you have int foo[N] in your code, the compiler must know at compile time what the value of N is.

The value of size_b_row1 must be set before/outside your function definition.

When you have
1
2
3
void foo( int bar ) {
  int bar;
}

You show two different variables with same name. The first is the parameter to the function, and the second is a local variable. If you then use bar, which one do you mean?

You do not set any value to your local variable size_b_row1. Then you use that variable anyway.

You declare that you have a double matrix_t[1][size_b_row1], but then you reference matrix_t[1][0]. That item is outside of your array.


Study the std::vector harder. It may look confusing at first, but it will make your life simpler.


PS. Despite your thread title, there is nearly no dynamic allocation here.
Last edited on
Alternatively he could probably use a pointer instead.
Thank you so much for the response , the error is expression must have a constant value for that we need to allocate memory dynamically so i wrote the topic as dynamic memory allocation

here is my code again if you want to
i will get an error on the first line itself

void transpose_vec(double matrix[size_b_row1],
double matrix_t[1][size_b_row1])
{
int size_b_row1;
int i;
for(i=0;i<size_b_row1;i++)
{
matrix_t[1][i] = matrix[i];
}
}
Last edited on
We have already seen that bit. Use of code tags would be nice.

How do you declare the size_b_row1 in the first place?
my problem is that size_b_row1 is of type int
but it is not constant
i will follow it from the next time using code tags, thank you.
I guess your problem is as follows:

You want to input matrices of length size_b_row1. This value of size_b_row1 varies in your calling function. What I suggest is as follows:

1. Don't use "dynamic memory allocation". If you must use it, then you have to use vectors. There must be some maximum limit of this size_b_row1. Lets say that limit is MAX_LIMIT;

Your code would then look like follows. (Ref: http://www.cplusplus.com/doc/tutorial/arrays/)
1
2
3
4
5
6
7
8
9
void transpose_vec (double matrix[], double matrix_t[][MAX_LIMIT], int size_b_row1)
{
  //int size_b_row1;
  int i;
  for(i=0;i<size_b_row1;i++)
  {
    matrix_t[0][i] = matrix[i];
  }
}
Last edited on
oh my god!! it runs you are awesome!! thank you so much, but seriously my boss says he just wants dynamic memory allocation i did like this now i still get error
yeah size of row can maximum be 40 should i introduce a constant double some thing like this const double size_b_row1 =40; now i just get an error saying expression must have pointer to object type between the for loop(the error)


void transpose_vec()
{
std::vector<int> temp(size_b_row1);
using namespace std;
std::vector<double> matrix(size_b_row1);

std::vector<double> matrix_t(size_b_row1);

int i;
for(i=0;i<size_b_row1;i++)
{
matrix_t[1][i] = matrix[i];
}

}
Last edited on
We can suggest a vector implementation code for you but it will not solve the purpose. What i instead propose is:

1. As pointed out earlier -
- put your code in code tags
- you are again trying to modify the value of matrix_t[1][i]. Please note that array indices start from 0.

2. From your code it seems there is a lot of confusion in function declaration, variables scope, vector implementation etc. You need to get these concepts clarified.

3. A slight hint, to implement matrices using vectors, you can maybe use nested vectors like the following:
std::vector<std::vector<double> > matrix_t (size_b_row1);
Last edited on
heyy thank you soo much the nested std vector works, you really helped me great
Topic archived. No new replies allowed.