code output(not believable)


 
Thread Tools Search this Thread
Top Forums Programming code output(not believable)
# 1  
Old 09-22-2008
code output(not believable)

Code:
#include<iostream> 
using namespace std; 

class base 
{ 
public: 
int bval; 
base(){ bval=0;} 
}; 

class deri:public base 
{ 
public: 
int dval; 
deri(){ dval=1;} 
}; 

void SomeFunc(base *arr,int size) 
{ 
for(int i=0; i<size; i++,arr++) 
cout<<arr->bval; 
cout<<endl; 
} 

int main() 
{ 
base BaseArr[5]; 
SomeFunc(BaseArr,5); 
deri DeriArr[5]; 
SomeFunc(DeriArr,5); 
}

could you pls. explain why the code output is like below:
00000
01010

Last edited by jim mcnamara; 09-22-2008 at 11:35 AM.. Reason: add code tags
# 2  
Old 09-24-2008
Because "arr++" assumes that the length of your array element is size of base, but actually it's size of deri.
Your deri looks like "bval,dval" in memory.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. What is on Your Mind?

Should we use CODE Tags for terminal input and output?

I've always used code tags for code but not for showing terminal input and output. I noticed a mod edited one of my threads and now I'm confused as to proper protocol. Mike (5 Replies)
Discussion started by: Michael Stora
5 Replies

2. UNIX for Beginners Questions & Answers

When I run the code yesterday I am getting output,when I run same code today I am getting error?

If run the below code today its creating all directory and getting output files,I f run same code tomorrow I am getting error. can any one give suggestion to sortout this error. OSError: no such file or directory : '062518'My code looks like this import paramiko import sys import os ... (8 Replies)
Discussion started by: haribabu2229
8 Replies

3. UNIX for Beginners Questions & Answers

Check output of command before outputing and keep exit code

so i have scripts that are piped and then run through one of the following mechanisms: cat myscript.sh | sh cat myscript.pl | perl what i want to do is, after either of the above commands are run, if the output from the command contains a certain string, i want it to avoid printing... (3 Replies)
Discussion started by: SkySmart
3 Replies

4. Shell Programming and Scripting

Same sed code prints(p) correct, but writtes(w) wrong output

Dear all, I am using sed as an alternative to grep in order to get a specific line from each of multiple files located in the same directory. I am using sed because it prints the lines in the correct order (unlike grep). When I write sed code that prints out the output I get it correct, but... (1 Reply)
Discussion started by: JaNaJaNa
1 Replies

5. Shell Programming and Scripting

Perl code to output data

Hello, I have a much larger data set like the attached example. Is there a code that will output the data to look like? Thank you. hg19_pseudoYale60_PGOHUM00000245279 chr1:242220709-242223574 hg19_pseudoYale60_PGOHUM00000243857 chr1:120820-133580 hg19_pseudoYale60_PGOHUM00000243858 ... (2 Replies)
Discussion started by: cmccabe
2 Replies

6. Programming

Strange characters in FORTRAN code output

Hi guys, After compiling a .f90 code and executing it, i get strange characters in the output file like : ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@ Are these windows characters? how can i get rid of this? Much appreciated. Paul (1 Reply)
Discussion started by: Paul Moghadam
1 Replies

7. Shell Programming and Scripting

Remove error code in output

Hi, i have the below code that will compare the value from 2 variables: ./wlst.sh JMSmon.py > out.dat w=`sed -e 's/\(.*!\)\(.*\)\(, Queue.*$\)/\2/' out.dat | awk '/'$1'/{n=0;{print $n}}'|head -n 1` if then x=`sed -e 's/\(.*!\)\(.*\)\(, Queue.*$\)/\2/' out.dat | awk '/'$1'/{n=2;next}n{print... (2 Replies)
Discussion started by: scripter123
2 Replies

8. UNIX for Dummies Questions & Answers

Remove area code using from awk output

I am back again with one more question, so I have a phonebook file with names, phone#s for example: smith, joe 123-456-7890 using awk I want to display all entries with a specific area code. here 's what i have tried so far: awk '$2~/^123/ {print}' phonebook I can't figure... (1 Reply)
Discussion started by: Nirav4
1 Replies

9. Shell Programming and Scripting

Better to Use Return Code or wc -l Output?

Hey All, Quick question... I'm writing a short script to check if a continuous port is running on a server. I'm using "ps -ef | grep -v grep | grep processName" and I was wondering if it was better/more reliable to just check the return code from the command or if its better to pipe to... (12 Replies)
Discussion started by: mrm5102
12 Replies
Login or Register to Ask a Question