sample code 1(no output) vs sample code 2 (output) why ?

There is no output in the first sample code, why ?

However, there is output in the second sample code.

Food for thought:
I do believe it is associated with the function initialize_ptr(), or maybe because I am sending an object of struct to the functions. ?

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
// Sample code 1:
#include <iostream>
#include <vector>
#include <string>
using namespace std;

struct access{
    int size = 0;
    int *p;
};

// Returns the size of the underlying array.
int set_size(access use){
    use.size = 10;
    return use.size;
}

// Returns the pointer which is pointing to the underlying array.
void initialize_ptr(int size, access use){
    use.p = new int[size];     // Pointer to the underlying array
}

// Sets value for each index of the underlying array.
void set_value(int x, int size, access use){

    initialize_ptr(size, use);
    
    // Sets:
    for (int i = 0; i < size; i++) {
        use.p[i] = x;
    }
    
    // Prints:
    for (int i = 0; i < size; i++) {
        cout << "p[" << i << "] = " << use.p[i] << endl;
    }
    
    cout << "\n size: " << size << endl;
}

int main() {
    access use;
    int save_sz = set_size(use);
    set_value(2, save_sz, use);
	
	return 0;
}


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
// Sample code 2:

#include <iostream>
#include <vector>
#include <string>
using namespace std;

struct access{
    int size = 0;
    int *p;
};

// Returns the size of the underlying array.
int set_size(access use){
    use.size = 10;
    return use.size;
}

// Sets value for each index of the underlying array.
void set_value(int x, int size, access use){

    // Initializing the pointer:
    use.p = new int[size];     // Pointer to the underlying array
    
    // Sets:
    for (int i = 0; i < size; i++) {
        use.p[i] = x;
    }
    
    // Prints:
    for (int i = 0; i < size; i++) {
        cout << "p[" << i << "] = " << use.p[i] << endl;
    }
    
    cout << "\n size: " << size << endl;
}

int main() {
    access use;
    int save_sz = set_size(use);
    set_value(100, save_sz, use);
	
	return 0;
}




p[0] = 100
p[1] = 100
p[2] = 100
p[3] = 100
p[4] = 100
p[5] = 100
p[6] = 100
p[7] = 100
p[8] = 100
p[9] = 100

 size: 10
Last edited on
In code 1 comments indicate initialize_ptr()
Returns the pointer which is pointing to the underlying array
whereas in actual fact it returns void (line 19)
However I'm not sure even code 2 is fine, by using a vocal destructor it seems that the object is destroyed before printing its array begins:
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
#include <iostream>
#include <vector>
#include <string>
using namespace std;

struct access{
    int size = 0;
    int *p;
    ~access (){std::cout << "Goodbye size: " << size << "\n";}
};

// Returns the size of the underlying array.
int set_size(access use){
    use.size = 10;
    return use.size;
}

// Sets value for each index of the underlying array.
void set_value(int x, int size, access use){

    // Initializing the pointer:
    use.p = new int[size];     // Pointer to the underlying array

    // Sets:
    for (int i = 0; i < size; i++) {
        use.p[i] = x;
    }

    // Prints:
    std::cout << "Start printing \n";
    for (int i = 0; i < size; i++) {
        cout << "p[" << i << "] = " << use.p[i] << endl;
    }

    cout << "\n size: " << size << endl;
}

int main() {
    access use;
    int save_sz = set_size(use);
    set_value(100, save_sz, use);

	return 0;
}/*
Output
Goodbye size: 10//object destroyed
Start printing//printing its array begins 
p[0] = 100
p[1] = 100
p[2] = 100
p[3] = 100
p[4] = 100
p[5] = 100
p[6] = 100
p[7] = 100
p[8] = 100
p[9] = 100

 size: 10
Goodbye size: 0
Goodbye size: 0*/


if the struct has just one data-member, size, it could do use that information to create the array and the data-member p is probably redundant
Last edited on
Topic archived. No new replies allowed.