Making returning a pointer to an array

Thread Starter

asilvester635

Joined Jan 26, 2017
73
I've created this makeArray function that makes an array in the heap memory via "new" operator, therefore when I return it won't get deleted. I tried testing my code to see that it has successfully created and returned the array by getting its size. The size that it gives me is 2, though I gave it size 10 inside my function. Why is that?

main.cc
Code:
int* makeArray() {
    int* array = new int[10];
    return array;

} // end of stringSize


int main( int argc, char** argv ) {

    int *array1 = makeArray();
    int size = sizeof(array1) / sizeof(array1[0]);
    printf("%d\n", size);
} // end of main

Output
2
[Finished in 4.5s]
 

spinnaker

Joined Oct 29, 2009
7,830
do this instead

#define ArraySize 10


int* makeArray() {
int* array = new int[ArraySize];
return array;

} // end of stringSize


int main( int argc, char** argv ) {

int *array1 = makeArray();
int size =ArraySiz;
printf("%d\n", size);
} // end of main
 

AlbertHall

Joined Jun 4, 2014
12,345
Your main function doesn't know how many elements there are in the array. As array1 is a int pointer 'sizeof(array1)' gives you the size of an integer pointer on your machine not what we would understand as the size of the array. As 'array1[0]', a single element of the array, is an integer, 'sizeof(array1[0])' gives you the size of an integer on your machine.
 

spinnaker

Joined Oct 29, 2009
7,830
I've created this makeArray function that makes an array in the heap memory via "new" operator, therefore when I return it won't get deleted. I tried testing my code to see that it has successfully created and returned the array by getting its size. The size that it gives me is 2, though I gave it size 10 inside my function. Why is that?

main.cc
Code:
int* makeArray() {
    int* array = new int[10];
    return array;

} // end of stringSize


int main( int argc, char** argv ) {

    int *array1 = makeArray();
    int size = sizeof(array1) / sizeof(array1[0]);
    printf("%d\n", size);
} // end of main

Output
2
[Finished in 4.5s]

To be more clear

sizeof(array1) returns the size of the integer pointer which is likely 4 bytes on your computer / compiler.

sizeof(array1[0]); returns the size of int which is 2 bytes.

4/2 =2
 

Papabravo

Joined Feb 24, 2006
21,159
That depends of the sizeof a pointer which is what array1 is. divided by the size of the first array element which is an integer. My guess is that the size of a pointer is 4 and the size of an integer is 2, or alternatively a pointer is 8 and an integer is 4.

BTW the size of that array on the heap is not well defined outside the makeArray method. Referencing an element that is out of bounds is a Bozo (the clown) no-no. You don't even know if there is any memory outside of the bounds. This is related to what is sometimes referred to as a memory leak which is not releasing memory that is no longer needed. Since you have kept no record of this allocation except in the pointer array1; if you change the value of array1 you will have no way to release the allocated storage.

If I were you I would return a pointer to a structure that had the starting address and the length of the array in a place that won't get overwritten. This will allow you to keep track of the things you have allocated so you can clean them up later.

https://en.wikipedia.org/wiki/Memory_leak
 
Top