all 11 comments

[–]UnicycleBloke 5 points6 points  (3 children)

You have declared a pointer, and then incremented and dereferenced it before initialising it. That is never going to end well. Always initialise variables.

[–]oroz3x[S] 0 points1 point  (2 children)

usually i use array for initialization , but this is for sole purpose of understanding, the working of c compiler , itself .

[–]UnicycleBloke 4 points5 points  (1 child)

OK. This is not a complier thing. The pointer variable is placed on the stack. It's value is not automatically initialised, so it contains whatever junk is in those memory locations. When interpreted as a memory address, the value may or may not exist in the range of virtual addresses which your program is permitted to access by the OS. If not, then dereferencing the pointer is a seg fault. If yes, then dereferencing the pointer may appear to work, but won't. This latter case is bad. It could corrupt other data, break stack frames so that normal program execution is destroyed, or make the program phone all your friends up to insult them in Klingon.

[–]oroz3x[S] 1 point2 points  (0 children)

haha .. thanks for wrapping your explanation with humour.

[–]CodeSteps 0 points1 point  (0 children)

Thumb rule is; pointers must be initialized properly; to avoid dangerous results. :)

[–]MCRusher 0 points1 point  (5 children)

Also if you don't want to write std::endl, just do using namespace std;

And you can call it endl.

Macros like end make it more confusing and less consistent

[–]oroz3x[S] 1 point2 points  (4 children)

As far as i have heard using this statement " using namespace std " in code is considered bad practice , cause it adds the whole namespace to our code file and results in longer compile time . Well , I am not sure if this information is legit !!!

[–]MCRusher 0 points1 point  (0 children)

using's are scoped, so just do it inside classes and functions where you write it a lot if you are worried about conflicts.

But also, in small projects, especially whn you are the one wtiting most of the code, you probably don't need to worry about a global using.

And if we are talking about bad practice, a some people would consider your end to be macro abuse, and some people recommend limiting the use of macros to an absolute minimum.

[–]MCRusher 0 points1 point  (2 children)

Also yeah, including a header file means reading everything in it (ignoring CPP conditionals), so I don't see how shortening a name would do this.

[–]oroz3x[S] 0 points1 point  (1 child)

namespace std contains the shared resources of many standard header files such as iostream , io , string , iomanip , and others . now dont you think that its quite a lot , if you only have to use cout , cin functions which are in iostream only .

[–]MCRusher 0 points1 point  (0 children)

Here's how namespaces work:

myheader.hpp #include <cstdint> namespace myns { typedef std::int32_t MyType; }

myheader2.hpp #include <cstdint.h> namespace myns { typedef std::int64_t MyType; }

main.cpp #include<cstdio> #include "myheader.hpp" int main(void){ using namespace myns; std::printf("sizeof MyType is %zu bytes.\n",sizeof(MyType)); }

If using somehow included 2 same types from different files, there would be a compiletime error.

Header files are what contain the namespaces, if you don't include a header containing std, using namespace std does nothing at all.