This question already has answers here:
How to find the size of an array (from a pointer pointing to the first element array)?
(17 answers)
Closed 2 years ago.
Hi guys I tested c++ dynamic array and I changed 2 inputs every time.
but array size is not changed. Why?
#include <iostream>
using namespace std;
int main()
{
int r = 0;
int c = 0;
int cnt = 1;
cin >> r;
cin >> c;
int** arr1 = new int* [r];
for (int i = 0; i < r; i++)
{
arr1[i] = new int[c];
}
cout << sizeof(arr1) << endl;
cout << sizeof(arr1[0]);
}
I knew that If I entered two value 3 and 4 then results are 3 and 4
but the results are 4 and 4
You are testing the size of a pointer which is always 4 on 32 bit.
That's because sizeof is a compile time thing; it cannot determine an array size.
Generally arrays do not carry size information, that is why functions like strlen need a null terminator.
Suggestion: use std::vector.
Related
My problem is when I declare an array int** arr =new* int[n] and I want to assign to it pointer to array and later change that pointer to a different pointer which is copy of it values + one other number ,it brakes down and appears (probably) infinite loop . Can you say how to do this in proper way using some low tools with c++/c or can you correct my code?
Additional explenation: the code is producing very simple output but it is not important. I want to create program to change in array pointer(int*arr) in specific index pointer to diffrent pointer . But additionally pointers direct first element in arrays .Also diffrennce beetween new and old array (which is changed in int**arr in index for example 0) is that new is bigger on a new element (int this case new number).So this output is only checking if it works.
Below is my whole code
#include <iostream>
using namespace std;
void stepwise_fill_array(int ** arr, int N, int index)
{
for(int j=1;j<=10;j++)
{
int* poi=arr[index];//getting pointer to array which i wannna change
int size=0;
while(poi){poi++;size++;}//getting size of pointer array from arr
int* n= new int[size+1];//declaring the new array
for(int i=0; i<size;i++)//copying from all values from old array to new one
n[i]=poi[i];
delete[] poi;
n[size]=j;//adding to the end new value
arr[index]=n;//asigning arr[0] to new diffrent array
}
for(int i=0;i<10;i++)
cout<<arr[0][i]<<" ";
//should print 1 2 3 4 5 6 7 8 9 10
}
int main(){
int N = 10; // how big array should be and how many times it should expand
int** arr = new int*[N];//declaring our array to pointer
for(int i=0;i<N;i++)
{
arr[i]=nullptr;
}
int index =0;//index where I would change the pointer of arr
stepwise_fill_array(arr,N,index);
}
In advance thanks for your help :)
Your style of coding and explaining of problem is tragic , but fortunately I copied with it. When you are trying to get size from while(poi){poi++;size++;} you are getting in trouble. In C\C++ is no possibility to check size of array from pointer to this array. Instead you need increment size in every iteration of function stepwise_fill_array.
Below I give you correct solution(in code are leaks but I doesn't affect in much way on efficiency):
void stepwise_fill_array(int **arr, int N, int index)
{
int size = 0;
for (int j = 1; j <= 10; j++)
{
int *poi = arr[index]; //getting pointer to array which i wannna change
int *n = new int[size + 1]; //declaring the new array
for (int i = 0; i < size; i++)
{
n[i] = poi[i]; //copying from all values from old array to new one
}
n[size] = j; //adding to the end new value
arr[index] = n; //asigning arr[0] to new diffrent array
size++;
}
for (int i = 0; i < 10; i++)
cout << arr[0][i] << " ";
//should print 1 2 3 4 5 6 7 8 9 10
}
This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Why can freed dynamically allocated memory still be accessed after a delete operation in C++? [duplicate]
(5 answers)
Closed 2 years ago.
I'm learning C++ from Stroustrop's A Tour of C++ 2E and I'm playing around with his examples.
I have a struct Vector, with a member elements that is a pointer to an array of doubles. I'm trying to delete[] the array which I allocated with new[], but even if there are no compile- or run-time errors, the array does not seem to be getting deleted. What's going on here?
#include <iostream>
struct Vector {
int size;
double* elements;
};
void print_array(double* first, int size) {
double* cursor = first;
for (int i = 0; i < size; ++i) {
std::cout << *cursor << " ";
++cursor;
}
std::cout << "\n";
}
int main() {
Vector v;
v.size = 5;
v.elements = new double[5];
for (int i = 0; i < v.size; ++i) {
v.elements[i] = 9-i;
}
print_array(v.elements, v.size);
delete[] v.elements;
std::cout << "v.elements==nullptr ? " << (v.elements==nullptr) << "\n";
print_array(v.elements, v.size);
return 0;
}
The output I'm getting is:
9 8 7 6 5
v.elements==nullptr ? 0
9 8 7 6 5
I do not yet know what will happen if I print out a deleted array, but I'm baffled as to why the third line in the output is happening at all. Thanks in advance.
This question already has answers here:
Accessing an array out of bounds gives no error, why?
(18 answers)
How do I find the length of an array?
(30 answers)
Closed 2 years ago.
int main()
{
int *array = new int; //Should provide space to store only one integer right?
for(int i =0; i < 10; i++)
{
cout << "Assigning" << i << "th value\n";
array[i] = i + 1;
}
for(int i = 0; i < 10; i++)
cout << array[i] << '\t';
delete array;
cout << '\n';
return 0;
}
Output
Assigning0th value
Assigning1th value
Assigning2th value
Assigning3th value
Assigning4th value
Assigning5th value
Assigning6th value
Assigning7th value
Assigning8th value
Assigning9th value
1 2 3 4 5 6 7 8 154274097 154405171
I know pointer size on my system is 8 bytes, checked with sizeof(int*) and integers take 4 bytes. I want to know how many chunks of memory in heap or free store (like literally boxes they use to teach students) does int* array = new int created, because books say I can store just one integer in it; to store more(like 10 ints) I need to do int* array = new int[10]. So how does this memory location pointed to by array can store 8 integers instead of one. Please note, I ran for loop 10 times as shown. Please tell me why are there just 2 garbage values, instead of 9 (since new should only allocate one integer worth space). Using Ubuntu with g++ 9.3.0.
int *array = new int; //Should provide space to store only one integer right?
Allocates a single int not an array.
for(int i =0; i < 10; i++)
{
cout << "Assigning" << i << "th value\n";
array[i] = i + 1;
}
Since there is no array but only a single element any access beyond the first (array[0]) array elements causes undefined behavior.
Any action, even formatting your hard drive, would be a valid program behavior.
Edit:
adapted to nearly completely rewritten question
This question already has answers here:
getting size of array from pointer c++
(6 answers)
What is array to pointer decay?
(11 answers)
Closed 6 years ago.
This definitely has a really simple answer, but I can't work it out. I've just made a simple function that outputs an array, but it only ever outputs the first two values of the array. I think it has something to do with the way I'm passing the array into the function.
#include <iostream>
using namespace std;
void outputArray(int arrayOut[]){
int size = sizeof(arrayOut)/sizeof(arrayOut[0]);//size is only 2??
cout << "{";
for(int i = 0; i < size; i++){
cout << arrayOut[i];
if(i != size-1){
cout << ", ";
}
}
cout << "}";
}
int main(){
int myArr[6] = {0, 1, 2, 3, 4, 5};
outputArray(myArr);
return 0;
}
Thank you very much!
Array variables are treated like pointers, so sizeof(arrayOut) is actually returning the size of the pointer arrayOut, not the size of the array. In 64-bit code, pointers are 8 bytes. sizeof(arrayOut[0]) returns the size of int, which is 4 bytes. So you get 2.
As #0x499602D2 states in their comment, you need to pass the array length as a separate parameter.
This question already has answers here:
C++: getting the row size of a multidimensional array passed to a function
(6 answers)
Closed 7 years ago.
in the code below, I want to find the size of the 2d array(matrix), however, when I try to do that, eventhough I was declared the size of the matrix just before testing it, it always outputs 1. And size of the factorMatrix and factorMatrix[0] looks same as 8. Any idea what may be causing this? Thanks.
factorMatrix = new int* [3];
for(i=0; i<3; i++)
{
/**/
factorMatrix[i] = new int [1];
}
factorCountMatrix = new int* [3];
for(i=0; i<3; i++)
{
/**/
factorCountMatrix[i] = new int [1];
}
factorMatrix[0][0] = 2;
factorCountMatrix[0][0]= 0;
factorMatrix[1][0] = 3;
factorCountMatrix[1][0]= 0;
factorMatrix[2][0] = 5;
factorCountMatrix[2][0]= 0;
//test = checkFactorMatrix(3);
test = (sizeof(factorCountMatrix) / sizeof(factorCountMatrix[0]));
cout << test << endl << endl;
When you use the sizeof operator on a pointer, you get the size of the pointer, and not what it points to. You need to keep track of the size of the memory you allocate yourself, or use something else like std::array or std::vector (which I recommend).