Verify a pointer to a structure that contains a pointer member

In summary: BUT I afraid it's like last time, it was so close and was so wrong!In summary, this program works, but it is very similar to the program that failed last time.
  • #1
yungman
5,718
241
TL;DR Summary
I just want to verify I am correct in my understanding of this.
Hi

I am working on Chapter 11 Structure, this is pointer of structure where the structure has a member that is a pointer variable. I just want to run by you guys to make sure my understanding is correct. Below is a program I wrote for verification. It works...BUT I afraid it's like last time, it was so close and was so wrong!
C++:
//pointer to struct with pointer
#include <iostream>
using namespace std;

struct test
{
    int x;//member variable x of integer
    int* pt;//member pointer variable pt of integer
};
void getMember(test*);

int main()
{
    int y = 20;// declare y = 20
    test t = { 15, &y }; // set initial value of x=15 and pt=&y ( address of y)
    test* stpt; //declare pointer stpt of test.
    stpt = &t;//initialize stpt to point to t where t is test type struct pointer
    cout << " address of y=&y = " << &y;// display address of variable y
    getMember(&t);

    return 0;
}

void getMember(test*stpt)
{
// on the same line as &y to show stpt->pt and (*stpt).pt all equal to &y which is address of y
//()has to be used because dot take precedence to indirectional operator *. (*stpt).pt means dereferencing
// pointer stpt that point to the address of pointer pt.
    cout << "      address of y=stpt->pt= " << stpt->pt << "     =(*stpt).pt= " << (*stpt).pt << "\n\n";

// show the content of x = 15 after passing to function
    cout << " content of x = " << (stpt->x) << "\n\n";

// show the content of y = 20 using *(stpt->pt) and *stpt->pt
//stpt->pt is dereference stpt to give address of pt. *stpt->pt = *(stpt->pt) is dereferencing
// the pointer pt to get the content of y = 20
    cout << " content y = *(stpt->pt) = " << *(stpt->pt) << "\n\n";
    cout << " content y = *stpt->pt = " << *stpt->pt << "\n\n";
}
// address of y=&y = 012FFD30      address of y=stpt->pt= 012FFD30     =(*stpt).pt= 012FFD30

//content of x = 15

//content y = *(stpt->pt) = 20

//content y = *stpt->pt = 20

I put as much comments in the program. Can you help me check my comments? I also show the output of running the program from line 40 to 46 to verify what I said.

Thanks
 
Technology news on Phys.org
  • #2
yungman said:
BUT I afraid it's like last time, it was so close and was so wrong!
What's wrong with it?
yungman said:
Can you help me check my comments?
I'm not sure if you want me to check your comments in general, or the comment block at the end of your code.

If it's the comments in general, several of your comments are useless. They don't add any information that couldn't be gotten from just reading the code. A comment should not just restate the obvious.
Line 7 - int x;//member variable x of integer
x is clearly a member of the struct, and just as clearly it is of type int.
Line 8 - int* pt;//member pointer variable pt of integer
Again, this comment doesn't add any information. For your example, you could have omitted both comments.
Line 14 - int y = 20;// declare y = 20
Useless comment. In fact, the code is defining y; i.e., declaring y and initializing it.
Line 15 - test t = { 15, &y }; // set initial value of x=15 and pt=&y ( address of y)
This one is still obvious, but not as bad as the others.
Lines 16 and 17 -
test* stpt; //declare pointer stpt of test.
stpt = &t;//initialize stpt to point to t where t is test type struct pointer
The first comment is not helpful, since clearly stpt is being declared as a pointer to a test struct.
The second comment is also unhelpful, as it doesn't do anything more than restate what the code is doing.
Also, why don't you combine the two statements, like so?
C++:
test* stpt = &t;  // Initialize stpt with the address of the struct t.
Line 27 - //()has to be used because dot take precedence to indirectional operator *.
The member access operators ( . ) and (->) are both of higher precedence that the indirection or dereference operator. That's why both of the following expressions evaluate to 20.
*(stpt->pt)
*stpt->pt
In the first expression above, the parentheses tell the compiler to evaluate stpt->pt before dereferencing, but that's what would happen anyway because of the higher precedence of -> relative to *.[/code]
 
  • Informative
Likes berkeman
  • #3
I guess I should not comment on the variables type. But from reading your reply, everything is correct, that make me feel better.

Remember last time when I was so sure I was right and I was wrong. This time, not only I show the codes, I repeat with comments to make sure I am right in both codes and comments. Now that you said I keep repeating what the code said, I guess I am right in both counts. This is important after the last time.

I read the book very carefully this time before I even write the code.

Thanks
 
  • #4
yungman said:
Now that you said I keep repeating what the code said
A comment should help a human reader understand why you are doing something. If it's obvious, the code probably doesn't need a comment at all.
Example of a bad comment, because all it is, is a more verbose version of what the code says.
C++:
int x = 32;   // set the value of the x integer to 32
Improved version:
C++:
int rectangleWidth = 32;
This code is self-documenting by virtue of an informative name. No comment is needed.

If you ever have a difficult time with a certain block of code, that is something that is really deserves a comment. Such comments are very helpful if you put the code aside and then look at it again six months later.

Here's a blog post that discusses good and bad comments: https://www.javajee.com/good-comments-and-bad-comments-in-your-program
yungman said:
I read the book very carefully this time before I even write the code.
Good!
 
  • #5
Mark44 said:
.....

If you ever have a difficult time with a certain block of code, that is something that is really deserves a comment. Such comments are very helpful if you put the code aside and then look at it again six months later.

Here's a blog post that discusses good and bad comments: https://www.javajee.com/good-comments-and-bad-comments-in-your-program
Good!
I actually had difficulty with the last part, I was up till 5am thinking got out of bed and read it to understand the pointer to a structure that contain pointer. Also the precedence. That's why I did so much redundant comments to make sure not only I code it right, that I understand it right.

I don't comment on program like this. Actually I am going to delete most of the comments. I put them into post here to make sure I am correct.

Thanks
 
  • #6
Sometimes programmers will embed data type info into the variable name so instead of naming your variable x they might use ix indicating that the variable is of type integer or pix to indicate that the variable is a pointer to an integer.

The notation was called Simonyi notation and was / maybe still is popular in Microsoft. It useful for when you are knee deep in code and are having difficulty remembering the data type of some variable. Nowadays the IDE tool reduces the need for this type of naming aid although it is still useful in many contexts.

https://en.wikipedia.org/wiki/Hungarian_notation

As an aside, Simonyi was quite a character. I had no idea of his contributions until I saw his Wikipedia biography.

https://en.wikipedia.org/wiki/Charles_Simonyi
 
  • #7
With respect to structs containing pointers wait until you get to doubly linked lists or tree nodes or worse multiply linked structs like neural nets.
 
  • #8
jedishrfu said:
The notation was called Simonyi notation and was / maybe still is popular in Microsoft. It useful for when you are knee deep in code and are having difficulty remembering the data type of some variable. Nowadays the IDE tool reduces the need for this type of naming aid although it is still useful in many contexts.
It was called Hungarian notation since its author, Simonyi, was Hungarian. As far as I know, it's not used all that much at MSFT, although it still shows up quite a bit in Windows code; e.g., wszTaskName, for a string of wide characters, zero-terminated. Steve McConnell's "Code Complete" lists some advantages and disadvantages of Hungarian notation. Two of the disadvantages are
1) "it virtually ignores the use of abstract data types as base types. Instead, they set up base types based on programming language: integers, floating-point number, and strings. The result is of little value, one that forces programmers to worry about manual type checking instead of letting the compiler check the types more rapidly and accurately."
2) "it combines data meaning with data representation. When you declare a variable to be an integer, you should have to change the variable's name if you change it to a long integer."
(p. 206).
 
  • #9
When I first learned about it was in my OS/2 days where MS and IBM collaborated on a new OS. I went to a NYC seminar where they gave me a box full of manuals on how to program each subsystem. While I don't have the manuals anymore, I still have the MS OS/2 mug.

I think later the Taligent system used the same notation for its examples. This was in the days before the IDEs came into being although I think Visual Studio had just come out.

Wrt to the type checking that is so true. However, when programming with a simple editor then the aid was useful in keeping track of groups of variables. I do recall the ridiculous prefixes I saw in the OS/2 and Windows docs though where it took a bit of expertise to figure out what it was exactly and how to use it.

And now back to our original thread discussion...
 
  • #10
jedishrfu said:
With respect to structs containing pointers wait until you get to doubly linked lists or tree nodes or worse multiply linked structs like neural nets.
That's why I particularly taking the time to work through pointer stuffs. I made up this problem, it's not in the book. Most of the questions I asked here are programs I made up. I don't have the answer and all made it much harder than the examples from the book. If I follow only what's in the book, I can move a lot faster with much less questions.

In fact I just finished another program that I made up, having a function allocate dynamic memory for a structure and pass it back to the main program.

C++:
//pointer to struct with pointer
#include <iostream>
using namespace std;

struct test
{
    int x;//member variable x of integer
    int y;//member pointer variable pt of integer
};
void getMember(test**);

int main()
{
    test* stpt;
    getMember(&stpt);//passing to function to allocate dynamic memory.
    cout << " Verify the address of structure stpt is the same from function = " << stpt << "\n\n";
    cout << " x = " << stpt->x << ",     y = " << stpt->y << "\n\n";
    delete[] stpt;
    stpt = 0;
    return 0;
}

void getMember(test** sfpt)
{
    test* Ar = new test;//allocate memory.
    Ar->x = 200;
    Ar->y = 400;
    *sfpt = Ar;
    cout << " Dereference sfpt     x = " << (*sfpt)->x << ",     y = " << (*sfpt)->y << "\n\n";
    cout << " Address of struct in function *sfpt = " << *sfpt << "\n\n";
}

No question, just show what I am doing. The only way to get less confuse is to keep practicing and thinking through the stuffs and build up to the rats nest.

I perfectly understand to name the variables that make more sense, as I said, this is my exercise, just want to make it as short as possible to make it faster to type. This is not meant to be read by others.
 
  • Like
Likes jedishrfu
  • #12
yungman said:
C++:
    delete[] stpt;
This should have been simply delete stpt;.

In general, when you allocate a single object, e.g. p = new test; you deallocate it with simply delete p;. When you allocate an array of objects, e.g. p = new test[10]; you deallocate it with delete[] p.
 
  • Like
Likes yungman and jedishrfu
  • #13
Another aside for the benefit of @yungman:

 
  • Like
Likes yungman

1. What is a pointer to a structure?

A pointer to a structure is a variable that stores the memory address of a structure. It allows you to indirectly access and manipulate the data within that structure.

2. What is a pointer member in a structure?

A pointer member in a structure is a variable that stores the memory address of another variable or object. It is declared using an asterisk (*) before the variable name.

3. Why do we need to verify a pointer to a structure that contains a pointer member?

Verifying a pointer to a structure is important because it ensures that the pointer is pointing to a valid and allocated memory address. If the pointer is not verified, it could potentially lead to errors or unexpected behavior.

4. How do we verify a pointer to a structure that contains a pointer member?

To verify a pointer to a structure, you can use the NULL keyword to check if the pointer is pointing to a null or empty memory address. You can also use memory management functions, such as malloc, to allocate memory for the structure and its members.

5. What are the potential risks of not verifying a pointer to a structure that contains a pointer member?

If a pointer to a structure is not verified, it could lead to memory leaks, segmentation faults, or other runtime errors. It could also result in data corruption or unintended changes to the structure's data. Therefore, it is crucial to always verify pointers to structures that contain pointer members.

Similar threads

  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
5
Views
885
  • Programming and Computer Science
Replies
3
Views
734
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
2
Replies
66
Views
4K
Replies
10
Views
961
  • Programming and Computer Science
Replies
5
Views
820
  • Programming and Computer Science
Replies
32
Views
2K
  • Programming and Computer Science
Replies
20
Views
1K
Back
Top