Dynamic Allocation

Why does my program print 1.000000 for each array element?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <cstdio>

const int MaxSize = 25000;

//display the program intro/overview
void welcome();

//get the user to select an array size (from 1-MaxSize),
//allocate an array of that size
//record the size in the parameter,
//and return a pointer to the array
double* allocate(int &size);

//fill the given array with user-supplied values
void fill(double arr[], int size);

//display the current array contents, one element per line
void print(double arr[], int size);

int main()
{
   int arrSize = 0;
   welcome();
   double *array = allocate(arrSize);
   fill(array, arrSize);
   print(array, arrSize);
   delete [] array;
   return 0;
}

void welcome()
{
    printf("Welcome to the Dynamic Allocation program\n");
    printf("This program will let the user choose an array size,\n");
    printf("allocates an array of doubles, fills the array\n");
    printf("with user data, displays the array contents,\n");
    printf("and the deallocates the array\n");
    printf("\n");
}

double* allocate(int &size)
{
    double *arr = NULL;
    do {
        printf("Please enter your desired array size as an integer in the range 1-%d: \n", MaxSize);
        int valsRead = scanf("%d", &size);
        if(valsRead == 0) {
            printf("That was not a number, please try again\n");
            scanf("%*s");
        } else if((size < 1)||(size > MaxSize)) {
            printf("%d is not in range, please try again\n", size);
        } else {
            //try to allocate the array
            arr = new double[size];

            //it might fail, i.e. we're almost out of memory, in which case arr will still be NULL
            if(arr == NULL) {
                printf("Sorry, we are unable to allocate an array of that size, please try again\n");
            }
        }
    } while(arr == NULL); //note that when things work, arr will not be NULL

    //we have a successful size and arr, return the array address
    return arr;
}

void fill(double arr[], int size)
{
    printf("Array allocation successful, initializing contents\n");
    for(int i = 0; i < size; i++) {
        double fill;
        printf("Please enter a value for element %d: \n", i);
        arr[i] = scanf("%lf", &fill);
    }
    printf("\n");
}

void print(double arr[], int size)
{
    printf("Initializing complete, array contents are as follows:\n");
    for(int i = 0; i < size; i++) {
        printf("Array element %d: %lf\n", i, arr[i]);
    }
    printf("\n");
    printf("Deallocating the array\n");
}
Last edited on
Check line 73.
You mean
73
74
// if successful, n_read is initialized to 1.
int const n_read = scanf("%lf", arr + i); 



56
57
58
59
60
61
62
//try to allocate the array
arr = new double[size];

//it might fail, i.e. we're almost out of memory, in which case arr will still be NULL
if(arr == NULL) {
    printf("Sorry, we are unable to allocate an array of that size, please try again\n");
}

Not quite: if new fails, it throws std::bad_alloc instead of returning a null pointer.

new-expressions don't ever result in null pointers unless you request a non-throwing version of the allocation function. This is done by passing the tag std::nothrow to operator new():
new(std::nothrow) double[size];
You'll have to #include <new>.

In C++, null-pointer constants are written with the keyword nullptr instead of the macro NULL. It is easy to write non-portable code when using NULL; never use it again given the choice.
Last edited on
because you wrote the value in the function into the variable "fill" and then assigned arr[I] to true (which is 1 here) because the scanf was successful.

try scanf("%lf" &(arr[i]));

There may be other such issues, I didn't look.
Last edited on
Registered users can post here. Sign in or register to post.