Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
On one of the forums I found a way to create a pointer to an entire array , rather than the first value . But how to initialize and access the array elements ?
Here is code of declaration:
char (*p)[7];
When I'm trying to free memory I get (_BLOCK_TYPE_IS_VALID(pHead->nBlockUse) error on line:
delete[] p;
How to fix it? I read that I need correct initialization with 'new'. But how to initialize it with 'new'?
I don't know if I understand good your problem. You said create a pointer to an entire array, but every time you declare an array with fixed length in c/c++, the variable points to the begin of the allocated memory. In other words, the variable stores the memory address where the array starts.
So, if you write char p[7], you can get, for example, the third value in two different ways: p[2] or *(p + 2) . But, in this case, you can not use delete, because the variable has a statically memory allocation.
If you want to use delete, you have to create a dynamic array with the follow code:
char *p;
p = new char[7];
// using p var
delete[] p;
In the other side, with the statement char (*p)[7], p is a pointer to the the variable that stores the first memory address of a fixed length array. In this way, you can not delete the fixed memory with the statement delete p, delete (*p) etc. To use delete in this case, you would have to allocate the memory dynamically like the example I wrote above:
char **p, *a;
a = new char[7];
p = &a;
// use ..
delete *p;
I hope it was useful :)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
As the title suggests,
int [] a = new int[size];
constantly throws me an error.
I am not yet familiar with C++ so please help me tweak the code above to as closely similar to the syntax above (above was a given pseudo(?) code in a class) so I can create an array.
Thank you.
#include <iostream>
using namespace std;
// Test program
int main( )
{
int [] a = new int[10];
cout << a[0];
return 0;
}
Tried running the above code and it failed to compile.
new is for dynamic allocation (of an array on the heap in this case), and returns a pointer to the new array.
[] in the declaration are for declaring it to be a stack array (and the brackets belong the right side of the variable name).
So the two legal approaches your code is mixing would simplify to either:
int a[10]; // Declares a stack array of size 10
or:
int *a = new int[10]; // Dynamically allocates a heap array of size 10 and stores the pointer to it in `a`
The former is the better solution here (there's no benefit to a heap allocation for such a small array that is only used within the scope of this function, and it would require explicit delete[] a; later to properly clean it up).
That said, neither version initializes the values, so the contents of a[0] are uninitialized, and reading from them will get random garbage. Both versions can be changed to initialize the data with zeroes though, e.g.:
int a[10] = {0}; // Declares a stack array of size 10 and initializes to zero
or:
int *a = new int[10](); // Dynamically allocates a heap array of size 10 and stores the pointer to it in `a`, initializing to zero
Final note: Using raw new is generally frowned upon in modern C++. If you needed heap-allocated contiguous data storage, you're probably better off using a std::vector.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I'm a newbie. since my code is so long I can't edit it for being reproducible so I will show you what I did in a simple way
I have a Team class. I want to hold all my objects in an array so that I can reach them somewhere else and map for some data.
so I did a function basically doing this (exactly this part b[1] = a;)
int* a; // represent my object
int *b = new int[2]; //represent my static object pointer
b[1] = a;
error saying cant assign int = *int
yes absolutely true but I have to hold my object in the array. and I thought this could work but no... is there a way to hold an object in an array or can I say give me space for *int, during pointer initializing?
A better way would be to use std::vector to hold your class objects. The advantage of using a std::vector is that then you won't have to deal with memory management explicitly. vector will take care of it.
In addition, i will also recommend using smart pointers.
To solve your error above you can use the following example:
int i = 0, j = 0;//int objects
int* p = &i, *q = &j; //pointer to int objects
int **b = new int*[2]; //note i have added * on both the left and right hand side.
b[0] = p;
b[1] = q;
//dont forget to use delete below
Note you should use std::vector and smart pointers, but i have added an example so that you can see how to remove the error and use this example as a reference.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have 3 questions:
Q1. When we create the object using the new operator, following is the syntax:
pointer_variable = new data-type //To create an object
pointer_variable = new data-type(value); //To create an object with value
pointer_variable = new data-type[size]; //To create an array
Why always there is a pointer_variable on L.H.S?
Q2. What is the difference between declaring and assigning pointers with and without the new operator?
Consider the following code snippet and output to understand the question:
int a = 10, b=20;
int *p;
p = &a;
int *q = new int;
q = &b;
cout<<"P is: "<<p<<" : "<<*p<<endl<<"Q is: "<<q<<" : "<<*q<<endl;
Output of the above code:
P is: 0x61ff04 : 10
Q is: 0x61ff00 : 20
Q3. When we say, with a new operator we can dynamically allocate memory to the array at run time when we don't know the size of the array at compile time. We can do this without new operator as given below:
cout<<"Enter the size of an array"<<endl;
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
for(int i=0;i<n;i++)
{
cout<<arr[i];
}
Then what is exactly the need to use the new operator for arrays?
Why always there is a pointer_variable on L.H.S?
Because new-expression results in a pointer.
What is the difference between declaring and assigning pointers with and without the new operator?
new-expression (not operator new) constructs a new object (and, optionally, allocates memory for it).
We can do this without new operator as given below
In fact, we cannot, according to the C++ Standard. Some compilers just allow this construct as a non-standard language extension.
Every good C++ book for beginners will explain these in more details.
in C++, a typical new expression allocates memory on the heap, and returns a pointer to that memory.
Re Q1: you can save the resulting pointer to a local variable for immediate use: pointer_variable = new int. But you don't have to do that: you could instead use it as an argument to a function: use_pointer(new int).
Re Q2: your code allocates an int on the heap, stores its pointer in local variable q, and immediately overwrites it with the address of local variable b. So what you have done here is write a small memory leak.
Re Q3: variable-sized array is a nonstandard extension to C++, so it will not necessarily work in another compiler. However, when it does work it is just another automatic variable: it will be automatically de-allocated for re-use when you leave the local scope. This is different from new allocations, which last until they are explicitly delete-ed.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Assuming my pointer is not pointing to an array, how is this block:
int *ptr_a;
ptr_a = new int;
*ptr_a = 1;
different compared with:
int *ptr_a = 1;
Is there a difference in memory allocation and when would I use one over the other?
int *ptr_a = 1;doesn't create a new int, this creates a pointer ptr_a and assigns a value of 1 to it, meaning that this pointer will point to the address 0x00000001. It's not an integer. If you try to use the pointer later with *ptr_a = 2, you will get a segmentation fault because the pointer doesn't point to an allocated memory space (in this precise case, it points to kernel memory, which is a no-no).
A good principle nowadays is to used std::unique_ptr<int> ptr_a = std::make_unique<int>(1)which will allocate a new int with a value of 1 and deallocate the memory once ptr_a gets out of scope.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to resize a dynamic array without losing data.
Like this:
double * pVecDin;//POINTER
int num_values = 2;
pVecDin = new double[num_values];
pVecDin[0]=5;
pVecDin[1]=6;
int new_num_values=4;
pVecDin = new double[new_num_values];
//Next I lost value of pVecDin[0] and pVecDin[1]
pVecDin[2]=8;
pVecDin[3]=9;
Do I need make an Auxiliar Dynamic Array to copy the old values?
Like:
int new_num_values=4;
double * pVecDin_aux; //POINTER
pVecDin_aux = new double[new_num_values];
pVecDin_aux = pVecDin;
for(int i=0; i < n; i++)
{
pVecDin_aux[i] = pVecDin[i];
}
Make a new, empty array with the new desired size.
Copy everything in the old array to the new array.
Delete the old array.
Assign the pointer of the old array to the address of the new array.
Or use a vector.
As nhgrif has mentioned, you need to make copy of the array.
There is another way if you can use malloc for memory allocation, then you can use realloc for resizing the array.
There are better ways as suggested by other like to use std::vector or list.