LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-23-2005, 01:01 PM   #1
coolguy_iiit
Member
 
Registered: Aug 2004
Posts: 51

Rep: Reputation: 15
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
 
Old 01-23-2005, 02:30 PM   #2
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
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 ? )
 
Old 01-23-2005, 04:52 PM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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
}
 
Old 01-24-2005, 12:42 AM   #4
coolguy_iiit
Member
 
Registered: Aug 2004
Posts: 51

Original Poster
Rep: Reputation: 15
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
 
Old 01-24-2005, 12:28 PM   #5
ldp
Member
 
Registered: Apr 2004
Location: Belgium Antwerpen
Distribution: slackware - knoppix
Posts: 141

Rep: Reputation: 18
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

Last edited by ldp; 01-24-2005 at 12:31 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
small syntax problem with C code (implemented in Code Composer Studio) illiniguy3043 Programming 6 01-07-2008 02:14 AM
User Preferences: Use HTML code instead of vB code? (vB code is overrated) stefanlasiewski LQ Suggestions & Feedback 5 07-26-2005 01:37 AM
C Code problem ar1 Programming 3 05-09-2005 07:04 AM
what the... C code problem zaichik Programming 6 01-26-2005 02:08 PM
Problem with Code eggoz Linux - General 6 04-09-2003 09:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration