473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

will this work??

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 1498

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
>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
does it matters if its c or c++??
ok consider it as c++..:)

Mar 7 '07 #4
"hijkl" <s_******@yahoo.comwrites:
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
<http://cfaj.freeshell.org/google/>. (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 <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 7 '07 #5
On 6 Mar 2007 21:38:52 -0800, "hijkl" <s_******@yahoo.comwrote in
comp.lang.c:
does it matters if its c or c++??
It does in comp.lang.c.
ok consider it as c++..:)
We don't discuss C++ here. news:comp.lang.c++ is three doors down the
hall on the left, past the water cooler.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Mar 7 '07 #6
On 6 Mar 2007 21:38:52 -0800, "hijkl" <s_******@yahoo.comwrote:
>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
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
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
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
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
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
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
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.

--
<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
Mar 7 '07 #14
hijkl wrote:
>
does it matters if its c or c++??
Yes
ok consider it as c++..:)
No. c.l.c++ is ---thataway.

--
<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
Mar 7 '07 #15
On 6 Mar 2007 23:35:17 -0800, "santosh" <sa*********@gmail.comwrote:
>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++.
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

R Pradeep Chandran wrote:
On 6 Mar 2007 23:35:17 -0800, "santosh" <sa*********@gmail.comwrote:
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++.

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.

Mar 7 '07 #17
santosh said:
>
R Pradeep Chandran wrote:
>On 6 Mar 2007 23:35:17 -0800, "santosh" <sa*********@gmail.com>
<snip>
>
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.

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

--
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
On 7 Mar 2007 11:44:53 -0800, "santosh" <sa*********@gmail.comwrote:
>R Pradeep Chandran wrote:
>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.
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
--
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

"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:ob*********************@bt.com...
>
It's 'pedantry', not 'pedanticism'. :-)
.... said the pedant. :-)

-Mike
Mar 8 '07 #20
CBFalconer <cb********@yahoo.comwrites:
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.
Do you never tire of being such a pedantic, picky, arrogant arse?
Mar 10 '07 #21
Richard wrote:
CBFalconer <cb********@yahoo.comwrites:
>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.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Mar 10 '07 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Antoon Pardon | last post by:
I have had a look at the signal module and the example and came to the conclusion that the example wont work if you try to do this in a thread. So is there a chance similar code will work in a...
7
by: jason | last post by:
I am getting twisted by the possibility that my virtual includes which currently work great on non-domain remote IP will crash if I purchase a domain and point it to one of my designated...
8
by: Eric Lilja | last post by:
Hello, I had what I thought was normal text-file and I needed to locate a string matching a certain pattern in that file and, if found, replace that string. I thought this would be simple but I had...
3
by: Zheng Da | last post by:
Will the following class work well? If it can not work correctly in some situation, could you help me to fix it? Thank you. //the class will work like the reference in java //when you create a...
5
by: Ralph2 | last post by:
Hello The person responsible for installing software at work believes in installing the bare minimum., which does not include the option to split a data base into a front and back end. So, rather...
7
by: Peter Steele | last post by:
I have code to add a domain user to a local group but I'm not sure if it will work with NT domains or whether it will only work with Active Directory based systems. Here's the code: public void...
0
by: palomine1234 | last post by:
PAYPAL MAGIC!!! TURN $5 INTO $15,000 IN ONLY 30 DAYS...HERES HOW! This is a Money Scheme and Not, I repeat... This is Not a Scam!!!
48
by: meyer | last post by:
Hi everyone, which compiler will Python 2.5 on Windows (Intel) be built with? I notice that Python 2.4 apparently has been built with the VS2003 toolkit compiler, and I read a post from Scott...
1
by: Ryan Liu | last post by:
So for a UtpClient to JoinMulticastGroup, there must be a router in the network and with multicast enabled? That is, for a small LAN with a few computers connected by a hub or siwtch, it won't...
1
by: Brendan Miller | last post by:
I need a portable way to tell what subprocess.Popen will call. For instance on unix systems, Popen will work for files flagged with the executable bit, whereas on windows Popen will work on files...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.