446,413 Members | 996 Online
Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

# will this work??

 P: n/a hey is anything wrong with this program??? int *array(int n){ return new int(n); } int main(){ int *p = array(10); for( int i = 0; i < 10; i++ ) { p[i] = 0; } printf( "%d\n", p[0] ); p = array(10); printf( "%d\n", p[0] ); return 0; } Mar 7 '07 #1
21 Replies

 P: n/a hijkl wrote: hey is anything wrong with this program??? Yes. It's not C. int *array(int n){ return new int(n); And here's the non-C construct. } int main(){ int *p = array(10); for( int i = 0; i < 10; i++ ) { p[i] = 0; } printf( "%d\n", p[0] ); p = array(10); printf( "%d\n", p[0] ); return 0; } Mar 7 '07 #2

 P: n/a >is anything wrong with this program??? Yes. There is no 'new' in C. int *array(int n){ return new int(n); } int main(){ int *p = array(10); for( int i = 0; i < 10; i++ ) { p[i] = 0; } printf( "%d\n", p[0] ); p = array(10); The above line causes a memory leak (except for the problem that it shouldn't compile in the first place). You have allocated memory and then throw away the last pointer to it. > printf( "%d\n", p[0] ); return 0; } Mar 7 '07 #3

 P: n/a does it matters if its c or c++?? ok consider it as c++..:) Mar 7 '07 #4

 P: n/a "hijkl" . (The Google bug originally described on that page has been corrected. so there's no excuse.) ok consider it as c++..:) This is comp.lang.c; we don't discuss C++ here. Finding a newsgroup where they do is left as an exercise. -- Keith Thompson (The_Other_Keith) ks***@mib.org San Diego Supercomputer Center <* "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Mar 7 '07 #5

 P: n/a On 6 Mar 2007 21:38:52 -0800, "hijkl"

 P: n/a On 6 Mar 2007 21:38:52 -0800, "hijkl" does it matters if its c or c++??ok consider it as c++..:) If you want to discuss c++, go to a newgroup where it is topical. Remove del for email Mar 7 '07 #7

 P: n/a hijkl wrote: hey is anything wrong with this program??? Yes. int *array(int n){ return new int(n); ^^^^^^^^^^^^^^^^^ Syntax error. No reason to go further (unless it's to comp.lang.c++ where they may like this C syntax error). } int main(){ int *p = array(10); for( int i = 0; i < 10; i++ ) { ^^^^^^^^^ I hope your C compiler is one for C99, else this is a syntax error. p[i] = 0; } printf( "%d\n", p[0] ); ^^^^^^^^ Using a variadic function with no prototype in scope is a naughty thing to do. p = array(10); printf( "%d\n", p[0] ); return 0; } Mar 7 '07 #8

 P: n/a hijkl wrote: does it matters if its c or c++?? ok consider it as c++..:) Then it is off-topic in comp.lang.c. We don't answer questions about how to program in Algol, Fortran, or Cobol either. Mar 7 '07 #9

 P: n/a hijkl said: hey is anything wrong with this program??? Yes. It won't compile as C90, as C99, or even (off-topically) as C++. My suggestions? (a) decide on a language, and then (b) learn it. -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at the above domain, - www. Mar 7 '07 #10

 P: n/a hehe u guys making issue :) anyways i will post in c++ grp.. u guys better have invested time in solving problem :) enjoyy Mar 7 '07 #11

 P: n/a hijkl said: hehe u guys making issue :) anyways i will post in c++ grp.. Good. Problem solved. u guys better have invested time in solving problem :) We already did. -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at the above domain, - www. Mar 7 '07 #12

 P: n/a hijkl wrote: hehe u guys making issue :) anyways i will post in c++ grp.. u guys better have invested time in solving problem :) enjoyy Please quote context and refrain from meaningless abbreviations like 'u', 'grp' etcetera. If not, you're unlikely to get helpful responses in comp.lang.c++. Mar 7 '07 #13

 P: n/a hijkl wrote: > hey is anything wrong with this program??? int *array(int n){ return new int(n); } int main(){ int *p = array(10); for( int i = 0; i < 10; i++ ) { p[i] = 0; } printf( "%d\n", p[0] ); p = array(10); printf( "%d\n", p[0] ); return 0; } Yes. -- "A man who is right every time is not likely to do very much." -- Francis Crick, co-discover of DNA "There is nothing more amazing than stupidity in action." -- Thomas Matthews Mar 7 '07 #14

 P: n/a hijkl wrote: > does it matters if its c or c++?? Yes ok consider it as c++..:) No. c.l.c++ is ---thataway. -- "A man who is right every time is not likely to do very much." -- Francis Crick, co-discover of DNA "There is nothing more amazing than stupidity in action." -- Thomas Matthews Mar 7 '07 #15

 P: n/a On 6 Mar 2007 23:35:17 -0800, "santosh" hijkl wrote: >heheu guys making issue :)anyways i will post in c++ grp..u guys better have invested time in solving problem :)enjoyy Please quote context and refrain from meaningless abbreviations like'u', 'grp' etcetera. If not, you're unlikely to get helpful responsesin comp.lang.c++. Since we are discussing English, it is 'et cetera'. Two words. http://en.wikipedia.org/wiki/Et_cetera OK, That is not an authoritative source. Finding the authoritative source is left as an exercise to the other pedantic residents of clc. Have a nice day, Pradeep -- All opinions are mine and do not represent the views or policies of my employer. R Pradeep Chandran rpc AT pobox DOT com Mar 7 '07 #16

 P: n/a R Pradeep Chandran wrote: On 6 Mar 2007 23:35:17 -0800, "santosh"

 P: n/a santosh said: > R Pradeep Chandran wrote: >On 6 Mar 2007 23:35:17 -0800, "santosh" >Please quote context and refrain from meaningless abbreviations like'u', 'grp' etcetera. If not, you're unlikely to get helpfulresponses in comp.lang.c++. Since we are discussing English, it is 'et cetera'. Two words. You got me there! Thanks. No, he didn't. We were discussing English, not Latin. (He is, however, correct that 'et cetera' comprises two words rather than one.) > [ ... ] >OK, That is not an authoritative source. Finding the authoritativesource is left as an exercise to the other pedantic residents of clc. The intention in warning the poster was a genuine interest in help him reach the maximum possible audience for his posts, since many otherwise potentially helpful posters ignore, or post unhelpful replies, to posts containing such "abbreviations." I any case it wasn't meant as an attempt at demonstrating pedanticism. It's 'pedantry', not 'pedanticism'. :-) -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at the above domain, - www. Mar 7 '07 #18

 P: n/a On 7 Mar 2007 11:44:53 -0800, "santosh" R Pradeep Chandran wrote: >OK, That is not an authoritative source. Finding the authoritativesource is left as an exercise to the other pedantic residents of clc. The intention in warning the poster was a genuine interest in help himreach the maximum possible audience for his posts, since manyotherwise potentially helpful posters ignore, or post unhelpfulreplies, to posts containing such "abbreviations." I any case itwasn't meant as an attempt at demonstrating pedanticism. I realized that. I consider being pedantic a good quality in a place like this. It is one of the reasons why I lurk here. That and the occasional dry humor. I guess I should've added a smiley to my post. :-) There is a regular poster who advised a certain researcher at Bell labs about what is topical in clc. There were a lot of people who were humor impaired and the resulting flames were very colorful. Have a nice day, Pradeep -- All opinions are mine and do not represent the views or policies of my employer. R Pradeep Chandran rpc AT pobox DOT com Mar 7 '07 #19

 P: n/a "Richard Heathfield" It's 'pedantry', not 'pedanticism'. :-) .... said the pedant. :-) -Mike Mar 8 '07 #20

 P: n/a CBFalconer >heyis anything wrong with this program??? int *array(int n){ return new int(n); } int main(){ int *p = array(10); for( int i = 0; i < 10; i++ ) { p[i] = 0; } printf( "%d\n", p[0] ); p = array(10); printf( "%d\n", p[0] ); return 0; } Yes. Do you never tire of being such a pedantic, picky, arrogant arse? Mar 10 '07 #21

 P: n/a Richard wrote: CBFalconer hijkl wrote: >>>is anything wrong with this program??? int *array(int n){ return new int(n); } int main(){ int *p = array(10); for( int i = 0; i < 10; i++ ) { p[i] = 0; } printf( "%d\n", p[0] ); p = array(10); printf( "%d\n", p[0] ); return 0; } Yes. Do you never tire of being such a pedantic, picky, arrogant arse? No. Both answers are accurate and responsive. -- Chuck F (cbfalconer at maineline dot net) Available for consulting/temporary embedded and systems. -- Posted via a free Usenet account from http://www.teranews.com Mar 10 '07 #22

### This discussion thread is closed

Replies have been disabled for this discussion.