will this work??

H

hijkl

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 = 0;
}
printf( "%d\n", p[0] );
p = array(10);
printf( "%d\n", p[0] );
return 0;
}
 
G

Gordon Burditt

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 = 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;
}
 
K

Keith Thompson

hijkl said:
does it matters if its c or c++??

Does *what* matter?

You need to provide context when posting a followup. Don't assume
that your readers have seen the parent article. See
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.
 
B

Barry Schwarz

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
 
M

Martin Ambuhl

hijkl said:
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 = 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;
}
 
M

Martin Ambuhl

hijkl said:
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.
 
R

Richard Heathfield

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.
 
H

hijkl

hehe
u guys making issue :)
anyways i will post in c++ grp..
u guys better have invested time in solving problem :)
enjoyy
 
S

santosh

hijkl said:
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++.
 
C

CBFalconer

hijkl said:
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 = 0;
}
printf( "%d\n", p[0] );
p = array(10);
printf( "%d\n", p[0] );
return 0;
}


Yes.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"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
 
R

R Pradeep Chandran

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++.

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
 
S

santosh

R said:
Since we are discussing English, it is 'et cetera'. Two words.

You got me there! Thanks.

[ ... ]
OK, That is not an authoritative source. Finding the authoritative
source 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.
 
R

Richard Heathfield

santosh said:
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 authoritative
source 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'. :)
 
R

R Pradeep Chandran

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.

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.
:)

<OT about humor>
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.
</OT>

Have a nice day,
Pradeep
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top