0

I'm a C++ noob and now just learning how to dynamically allocate. I am learing by myself mostly googling. As I almost mastered C, I basically know how to dynamically allocate double pointer structure. But I think in C++, the method used in C when dynamically allocating double structure pointer is not right. Here'a the code including main;

=============================================================================================

#include <iostream>

using namespace std;

typedef struct s2 {
    int data3;
    int data4;
}S2;

typedef struct s1 {
    int data;
    int data2;
    S2* ps2;
}S1;

int main()
{
    S1* pt1 = new S1;
    S2** pt2 = new S2*;
    cout << "data >> "; cin >> pt1->data;
    cout << "data2 >> "; cin >> pt1->data2;
    cout << "data3 >> "; cin >> pt1->ps2->data3;
    cout << "data4 >> "; cin >> pt1->ps2->data4;

    cout << pt1->data << " \n " << pt1->data2 << " \n " << pt1->ps2->data3 << " \n " << pt1->ps2->data4 << endl;


    return 0;
}

As y'all master coders may already, know when I build this solution, after I inserted integer into pt1->data2, the program suddenly stops and just terminate, not even giving me a chance to insert into variable pt1->ps2->data3 nor pt1->ps2->data4. Are there any solution to this problem? As you can see, my english is very bad because I'm korean and just 19 years old. So could you master coders please write down the entire solution code, and then tell me what is the problem? Thank you very much in advance.

  • pt1->data2 is fine. But pt1->ps2 is a pointer. What do you think pt1->ps2 points to? – user253751 Aug 31 '20 at 15:12
  • doesn't it points to S2? – douglas_min Aug 31 '20 at 15:15
  • As the previous comment says, you haven't initialized the pointer inside S1. You would need to do `pt1->ps2 = new S2;`. Then you would have a value being pointed to by pt1->ps2. – emegona Aug 31 '20 at 15:16
  • 1
    *I am learing by myself mostly googling* -- You cannot learn C++ by simply googling. It is a much too difficult language to learn it that way. Invest in peer-reviewed C++ books and materials, and not a random site where you have no idea the quality of what they're showing you. – PaulMcKenzie Aug 31 '20 at 15:17
  • oh i thought S2** pt2 = new S2* was the initializer for pointer inside S1. – douglas_min Aug 31 '20 at 15:18
  • *"doesn't it points to S2?"* - No, It *can* point to *an* `S2`, but until such time as you make that happen with code, technically it points to nothing determinate. Thus, dereferencing that pointer invokes *undefined behavior* . – WhozCraig Aug 31 '20 at 15:19
  • `typedef struct s2` -- If you got this from googling, then this is exactly what I was referring to. You are learning C++ from a `C` website, or the author does not know C++ well enough to know that `typedef struct` is not necessary in a C++ program. All you need is `struct s2`. – PaulMcKenzie Aug 31 '20 at 15:21
  • 2
    You'd have an easier time to learn C++ from a [good C++ book](https://stackoverflow.com/a/388282/4641116). If your code was subject to a code review by my team, almost every line would be dinged. – Eljay Aug 31 '20 at 15:21
  • The C equivalent of your shown C++ code would be `S1* pt1 = malloc(sizeof(S1)); S2** pt2 =malloc(sizeof(S2*))` and there `pt1->ps2->data3` wouldn't be valid either. – t.niese Aug 31 '20 at 15:23
  • @douglas_min S2 is a type. You can't point to S2 because you can't point to types. And no, `S2** pt2 = new S2*;` allocates a new S2*, allocates a new S2** variable called pt2, and stores a pointer to the new S2* inside pt2. None of that has anything to do with pt1->ps2. – user253751 Aug 31 '20 at 15:27
  • @PaulMcKenzie Oh I see. But no I learned C, Java, Python, basic algorithm using C in a academy in Korea. It's like kind of like a school you go for about 1 month for someone who wants to learn programming or getting a carrer in an IT world. They teach us basic rules? of programming and in the last 1 week, make a project. But after the COVID-19 outbreak, our academy was suspended and I have no source of education even though i want to learn. I think you seem like a good professor, so if you don't mind, could you recommend like a big book that i can learn c++ no matter how long it takes? – douglas_min Aug 31 '20 at 15:38
  • The books linked to with @Eljay's comments are a good start. The issue with C++ is that even after years of using it, there are still things that you may not know because of how big the language has gotten over the course of 30+ years. – PaulMcKenzie Aug 31 '20 at 15:42
  • Instead of learning c++ by googling, learn it by stackoverflowing. – Soleil Aug 31 '20 at 15:57
  • @Soleil-MathieuPrévot SO might be a good place if you already know a language get more in-depth information about certain topics and when you are able to distinguish between good and bad answers. But using StackOverflow to learn a language from scratch is most certainly not a good idea. Especially because there are various answers that are inaccurate or even wrong, but still exist with positive votes because they got lost in the mass of questions and answers. – t.niese Aug 31 '20 at 19:56
  • @t.niese Well, SO should not be the exclusive source, but we can find many excellent Q/A on basic subjects. I recognize that few topics are certainly not properly covered by anything on the web (eg., pthreads / Butenhoff, DI / Seeman, compilers). People may feel comfortable with strong guidance, and sometimes with little; but in the end the language is most of the time covered with docs, training videos, etc on the web, but good practices are another story. – Soleil Sep 01 '20 at 14:45
  • @t.niese Also, c++ evolved a lot, and an old book can be filled with many outdated practices for nowadays standards (eg., using new/delete), however those are also part of the history and associated to things we hide more and more (I'm thinking about c++ approaching c# for certain aspects as it matures). I myself never learned c++ from a book, but from elements from the web and SO (but also coming from c# and c); – Soleil Sep 01 '20 at 14:46
  • I believe @douglas_min may want to find his/her own way, and we don't need to be all of us dogmatic. douglas_min may also have lots of fun and interest by navigating from topic to topic, while working also on personal challenges and exercices. – Soleil Sep 01 '20 at 14:46
  • @Soleil-MathieuPrévot I didn't say that StackOverflow is a bad place to get information. And yes `we can find many excellent Q/A on basic subjects`, but there are many Q/A (especially about beginner questions) that are bad or just wrong. And if you are new to a language then it is hard to distinguish between good and bad answers. And the reputation of the users doesn't help much there either. There are 40k+ accounts that write bad answers, and there are answers of people with a rep under 1k or even 500 that are good. – t.niese Sep 01 '20 at 15:05
  • @Soleil-MathieuPrévot beginners often use the same wrong terminology and therefore find bad Q/A more often, because they find those questions, that are asked over and over again, which more than often are not marked as a duplicate to a way better Q/A. Those bad Q/A are sorted out over time, but IMHO exits way too long on the platform. So I still think a good up to date book is better to learn the language then SO. – t.niese Sep 01 '20 at 15:08
  • @t.niese You're perfectly right; but doesn't the wrong get washed away quickly ? I can see downvotes and votes for deletion being very dynamical ! Also there are better answers with fewer upvotes. Can tools be added to sort those sleeping bad Q/A ? or a quality label system over the voting system ? Maybe in the end all that is indeed out of scope of SO. – Soleil Sep 01 '20 at 15:20
  • @Soleil-MathieuPrévot those beginner questions sadly not. I more than often stumble across bad Q/A, when I search for duplicates (at that time I can downvote them). Especially beginner questions tend to pollute StackOverflow without getting sort out (fast enough), and because beginner questions also tend to be answered by beginners, those are often of bad quality. – t.niese Sep 01 '20 at 15:25

1 Answers1

0

new S2* dynamically allocates a pointer. Is this really what you intended? I think that, instead of

S2** pt2 = new S2*;

you probably want to do

pt1->ps2 = new S2;

Also, instead of this:

typedef struct s2 {
    ...
}S2;

in C++, it's more natural to just write:

struct S2 {
    ...
};

And learning C++ by googling is a really bad idea. Unlike, for example, Java, which was almost designed to be learned like this, C++ is very hard. What's worse, C++ compilers will allow you to compile incorrect code, so your program might not be valid C++ and still compile. I would recommend to just get a good textbook.

shananton
  • 508
  • 2
  • 12
  • As an extension to _'C++ compilers will allow you to compile incorrect code'_, it's worth noting in C++ semantics of similar syntax depends on context (due to overloading, overriding, polymorphism...). As a result, one may write a code which looks reasonable and is actually correct but it does something different than intended if the programmer doesn't know the language well enough. – CiaPan Aug 31 '20 at 15:57