LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   What is the problem with the below C++ code? (https://www.linuxquestions.org/questions/programming-9/what-is-the-problem-with-the-below-c-code-281276/)

coolguy_iiit 01-23-2005 01:01 PM

What is the problem with the below C++ code?
 
Hi,
<code>
#include<iostream.h>
struct x{
int y;
float c;
};
typedef struct x x;
int main()
{
x *st1;
x *st2;
cout << st1 << endl<<st2<<endl; //First statement
st1 = (x*) malloc(sizeof(x));
st2 = (x*) malloc(sizeof(x));
cout << (int)st1 << (int)st2 << endl; // Second statement
}
</code>

The output of the above code gives st2 a value of 0 in the first cout statement...
I am not able to figure out the reason behind it.

can someone help me?

bye
cool

SciYro 01-23-2005 02:30 PM

probably because it points no wear (tho i don't know c++ very well) .... you declared st2 as a pointer to no were, so it will have undefined results i believe ... what else were you expecting it to do? .. does it always give the same results (even after you stop using it, do some gaming or other work for some time, or restart the computer ? )

Dark_Helmet 01-23-2005 04:52 PM

SciYro is right. When you declare a variable, the compiler only sets aside a location for it. It does not initialize the variable for you. You are likely to get random data if you use the variable before you initialize it (like you do in your program). The fact that st2 is 0 just means that 0 was stored there earlier by some other process.

As a side note, you're using the old C-style for memory allocation (using malloc()), C++ uses the new and delete keywords for that.

Also, I'm a bit confused why you typecast st1 and st2 as ints in one cout sequence, but not in the other. Were you expecting that to change the value? I just thought it was odd.
Code:

#include<iostream.h>

struct x{
  int y;
  float c;
};

typedef struct x x;

int main()
{
  x *st1;
  x *st2;

  cout << st1 << endl << st2 << endl; //First statement
  st1 = (x*) malloc(sizeof(x));
  st2 = (x*) malloc(sizeof(x));
  cout << (int)st1 << (int)st2 << endl; // Second statement
}


coolguy_iiit 01-24-2005 12:42 AM

Hi sciyro,
I rebooted my pc did some gaming and agian ran the program and saw the output even then the value of st2 is coming out to be 0.but if i declare more variables like st3 etc.. all those are printing some garbage values but st2 (i mean the second pointer ) is always printing 0.is there any fundoo?

bye

ldp 01-24-2005 12:28 PM

Like stated before, you should always initialize your variables or they will bogus on you. (verb bogussing: to bogus or not to bogus) Also, you should make a choice between c and c++ because they are not the same language. I mean that you should then use the correct headers whenever possible/available. like #include <iostream> (don't mention the .h) And defining a namespace can also save you a lot of trouble. I assume you will be using the standard namespace a lot? like: using namespace std; otherwise, you can refer to it like: std::cout << endl; And I'm also wondering about the same things that dark_helmet pointed out: why using the old 'malloc' if you have such a practical 'new' at your disposition? But the main point here remains: always initialize your variables like you did in c. ow yeah, if you plan to use c++, classes are really the key. (hint) ;)
kind regards,
Lieven


All times are GMT -5. The time now is 08:07 PM.