0

I want to declare an array that is stored in a pointer A. I have the following code.

int length = 8;
int *A;
A = (int*) malloc(length*sizeof(int));
A = {5, 1, 3, 5, 5, 2, 9, 8};

However, the array cannot be initialized like above. The error says "cannot convert to 'int' in assignment". How do I fix this issue?

Additionally, are malloc and memset necessary in c++ when declaring an array (for a pointer)?

Thanks!

4
  • 2
    This would work much better with a std::vector. std::vector<int> A{5, 1, ..., 8}; Commented Apr 20, 2014 at 20:04
  • @chris your suggested implementation std::vector<int> A{...} does not seem to compile correct. error is "extended initializer lists only available with ....." Commented Apr 20, 2014 at 20:13
  • With -std=c++11, right? Yes, it's a C++11 feature. Commented Apr 20, 2014 at 20:19
  • why are you using malloc when the question is tagged c++ ? you should use new or even better std::vector Commented Apr 20, 2014 at 21:32

4 Answers 4

2

Quick answer:

A[0] = 5;
A[1] = 1;
A[2] = 3;
A[3] = 5;
A[4] = 5;
A[5] = 2;
A[6] = 9;
A[7] = 8;

Basically, when you say "A = " you are changing "what A is pointing to". If you want to change "the value of what A is pointing to" you must use [] or *.

cplusplus.com has a good article on that topic

Edit

I must warn you that it is not a good pratice to use malloc in C++ because it will not initialize neither destruct complex objects.

If you have:

int length=8;
class C_A {
    C_A() {
        std::cout << "This cout is important" << std::endl;
    }
    ~C_A() {
        std::cout << "Freeing is very important also" << std::endl;
    }
};

C_A* A;
A = (C_A*) malloc(length*sizeof(C_A));
free(A);

You will notice that the cout will never happen, while the correct is:

A = new C_A[length];
delete[] A;
Sign up to request clarification or add additional context in comments.

1 Comment

@return0 I edit my answer with why malloc may be bad in C++, I advice you to read it.
1

NO. You don't need malloc to declare an array as a pointer since an array in its nature is a pointer. The difference between using malloc or no is that when using malloc the array is declared in the heap instead of the stack.

Second, you can fill an array directly if and only if you are filling it when declaring e.g. This is right : int a[3]={1,2,3};

This is wrong :

int a[3]; a= {1,2,3};

Comments

0

A reasonably efficient way to do what you want, with malloc() and memcpy(), is

int initializer[] = {5, 1, 3, 5, 5, 2, 9, 8};
int *A;
A = (int*) malloc(length*sizeof(int));
memcpy(A, initializer, length*sizeof(int));

Comments

-2

Use new instead of malloc, it returns T* and not void*, and support exception:

int *A = new int[length];

2 Comments

Forget malloc() in C++. For memset, you can use std::fill(A, A + sizeof(A), 0).
@return0, malloc only works in specific cases. I would definitely advise staying away at least until you understand why.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.