Making returning a pointer to an array

Discussion in 'Programmer's Corner' started by asilvester635, Feb 12, 2017.

  1. asilvester635

    Thread Starter Member

    Jan 26, 2017
    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?
    Code (Text):
    2. int* makeArray() {
    3.     int* array = new int[10];
    4.     return array;
    6. } // end of stringSize
    9. int main( int argc, char** argv ) {
    11.     int *array1 = makeArray();
    12.     int size = sizeof(array1) / sizeof(array1[0]);
    13.     printf("%d\n", size);
    14. } // end of main

    [Finished in 4.5s]
  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    You are getting the size of the pointers not the array.
  3. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    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
  4. AlbertHall

    AAC Fanatic!

    Jun 4, 2014
    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.
  5. spinnaker

    AAC Fanatic!

    Oct 29, 2009

    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
  6. Papabravo


    Feb 24, 2006
    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.