0
struct abc
{
    float b;
};

abc* xyz;

xyz->b = 10;
printf("%f", xyz->b);

error at printf. What is the reason? How can I solve this?

Ghasem Ramezani
  • 2,683
  • 1
  • 13
  • 32
user6532
  • 1
  • 2
  • 6
    you forgot to allocate memory for xyz – Pepijn Kramer Sep 07 '21 at 15:53
  • Your pointer `xyz` is uninitialized. Who knows where it's pointing to (but using it it's worth a bus error or segmentation fault). – Scheff's Cat Sep 07 '21 at 15:54
  • 3
    No need to use pointers here. Just use `abx xyz; xyz.b=10;...` – ChrisMM Sep 07 '21 at 15:55
  • 1
    @ChrisMM That too :), C++ code should have minimum number of new/deletes anyway – Pepijn Kramer Sep 07 '21 at 15:58
  • Reading an uninitialized pointer, such as your `xyz` results in Undefined Behavior. – Drew Dormann Sep 07 '21 at 15:59
  • Please clarify whether you are aware of using a pointer which needs initialisation via appropriate `new`. If not, the answer is more along the comment by Chris than like the existing answers. – Yunnosch Sep 07 '21 at 16:01
  • @DrewDormann: Nitpick -- You can read (the value) of a pointer, which is defined behavior. *Dereferencing* an uninitialized pointer results in Undefined Behavior. Ex. `abc *tuv = xyz;` is OK. `xyz->b` is U.B. – Thomas Matthews Sep 07 '21 at 16:33
  • @ThomasMatthews • reading an *uninitialized* pointer is undefined behavior, just as reading an *uninitialized* `int` is undefined behavior. – Eljay Sep 07 '21 at 16:46

3 Answers3

4

You need to define an instance of structure.

abc *xyz only defines a pointer, it doesn't allocate memory(static or dynamic) for an instance of struct abc. Hence variable float b is not allocated space.

Solution:

abc xyz;
xyz.b=10;

Or

abc *xyz = new abc;
  • `malloc` is troublesome enough when it works, but in your example, [it does not compile](https://godbolt.org/z/e9oh49sEr). – Drew Dormann Sep 07 '21 at 16:02
  • I think it is more "define or create" than "declare", but otherwise yes, apart from the slip of thinking C in a C++ question. – Yunnosch Sep 07 '21 at 16:02
  • 1
    use `new` in preference to `malloc` in c++ and don't use `new` either unless you really have to – Alan Birtles Sep 07 '21 at 16:03
  • 1
    I like the non-pointer alternative you offer. If you fix the issues mentioned in comments I will happily upvote. Even happier if you improve your capitalisation and line wrapping. – Yunnosch Sep 07 '21 at 16:04
  • thanks everyone for letting me know, made the changes as you suggested – Sajal Mandrekar Sep 07 '21 at 16:20
  • Please incorporate all comments. At least the "declare" problem. Ideally formatting, capitalisation, line feeds. – Yunnosch Sep 07 '21 at 16:23
  • @Yunnosch I would really be grateful if u upvote my answer – Sajal Mandrekar Sep 07 '21 at 16:24
  • Grammar: *`abc *xyz` only creates a pointer, it doesn't allocate memory for that variable.* It does allocate memory for that variable if *that` refers to `xyz`. The pointer is a variable, and it has been allocated storage. What I think you meant to write is it doesn't point at a valid `abc`. – user4581301 Sep 07 '21 at 16:41
  • Happy to. You have spent some learning and effort on improvement. – Yunnosch Sep 07 '21 at 16:45
  • @user4581301 Your comment is not wrong. However, "It does allocate memory for that variable if *that` refers to xyz" I cannot 100% agree with. In my opinion "allocate memory" should only be used to describe what happens at runtime; i.e. `new`, `malloc` etc. It can be argued that local variables get memory allocated on stack (though the concept of a stack is not defined by standard as far as I know...). For the possibly applicable context of global variables I think "allocation" is ambiguous. Otherwise O agree with pointing out that the phrasing as is is ambiguous. I do often think embedded... – Yunnosch Sep 07 '21 at 16:48
  • @Yunnosch I just joined stackoverflow today, and your comments have helped me improve and understand typing accurate answers – Sajal Mandrekar Sep 07 '21 at 16:53
  • And because you and I are not the only ones of that opinion, you now have shiny new privileges. Enjoy. – Yunnosch Sep 07 '21 at 17:09
  • @Yunnosch I know what Sajal intended. You know it. I suspect many, maybe even most, people will. Might as well get that last few percent though. English is a terrible language for precision. – user4581301 Sep 07 '21 at 17:10
  • @user4581301 thanks for you're feedback too – Sajal Mandrekar Sep 07 '21 at 17:13
  • @user4581301 :-) To be fair, many, probably most, spoken languages are. I have heard convincingly told stories where 10 people of 4 different languages met. None of the available languages was spoken by all of them. Each statement had to be translated at least twice. They argued hours on an architecture decision. No result. Then somebody walked to the black board and wrote twenty lines of C. Silence, reading. Then ten people nodding in agreement. Done. Success. – Yunnosch Sep 07 '21 at 17:13
1

In code :

#include <iostream>

struct abc
{
    float b = 0.0f; // initialize!
};

int main()
{
    abc* xyz = new abc{}; // <-- new!
    // or use auto xyz = std::make_unique<abc>(); <-- recommended for c++

    xyz->b = 10.0f; 
    std::cout << xyz->b;
    // printf("%f", xyz->b); <-- std::cout is more 'c++'

    delete xyz;
}
Pepijn Kramer
  • 9,356
  • 2
  • 8
  • 19
  • both `new` and `std::make_unique` are equally C++ style. One simply uses regular pointers, the other – smart ones. – Anton Menshov Sep 12 '21 at 21:43
  • make_unique is really the recommended way nowadays, C++ evolves and better stuff is added each version. Unlike new, make_unique shows intent (who 'owns' the pointer), has better exception safety (passing intermediates), interaction with RAII etc.. etc.. So new is C++ < C++14, make_unique is C++14 and later. https://stackoverflow.com/questions/37514509/advantages-of-using-stdmake-unique-over-new-operator and follow up links – Pepijn Kramer Sep 13 '21 at 03:30
  • there are lots of situations when smart pointers are certainly better. However, raw pointers are still at play in many situations. Your statement is correct for, say, deprecated `auto_ptr`. It is incorrect to state that raw pointers are not [modern] C++ style in general. – Anton Menshov Sep 13 '21 at 03:35
  • I updated my statement, new is C++, and yes raw pointers are around a lot. That's because we have legacy code and books haven't been updated. I've spent 30+ years having to debug peoples code with new/delete, with the new smart pointers the number of bugs dropped enormously. So yes I say they are recommended. – Pepijn Kramer Sep 13 '21 at 03:40
  • ok. the edit makes things definitely better. thanks for considering it. – Anton Menshov Sep 13 '21 at 03:42
0

One possible solution is to just change: abc *xyz; into abc *xyz = new abc();

The reason for the crash is that you did not allocate any actual memory for abc::b, thus xyz is an uninitialized pointer.

Ralf Ulrich
  • 1,575
  • 9
  • 25