473,387 Members | 1,791 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,387 software developers and data experts.

Urgent C Questions


1. in the prg bellow what vars are stored on stack, heap, data segment?

int i;

void main()
{
int j;
int *k = (void *)malloc(1);
}

(I think j and k are on stack, but where is i?)
2. how to find sizeof variable w/o using sizeof command?

(no clue)
3. what is the o/p of this prg?

void main()
{
int x = 5;
x = x++;
printf("x=%i", x);
}

(I think it shoulld be printing 5 but it prints 6!)

ps: I am using QuickC 2.0

pps: Is very urgent!
Jan 15 '08 #1
77 3782
In article <18****************@aioe.org>,
Hans Schneider <ha**@localhost.localdomainwrote:
>1. in the prg bellow what vars are stored on stack, heap, data segment?

int i;

void main()
{
int j;
int *k = (void *)malloc(1);
}

(I think j and k are on stack, but where is i?)
Very much the same question was asked less than a week ago. The
answer is that as far as C is concerned, 'stack', 'heap',
and 'data segment' are implementation details and conforming
implementations exist which do not use any of those.

>2. how to find sizeof variable w/o using sizeof command?
>(no clue)
This is in the C FAQ.

>3. what is the o/p of this prg?

void main()
{
int x = 5;
x = x++;
printf("x=%i", x);
}

(I think it shoulld be printing 5 but it prints 6!)
The output can be anything at all, as the program violates a
constraint, has an incorrect declaration for main, and fails to #include
a necessary interface file.
--
"All is vanity." -- Ecclesiastes
Jan 15 '08 #2
On Tue, 15 Jan 2008 03:41:56 +0100 (CET), Hans Schneider
<ha**@localhost.localdomainwrote in comp.lang.c:

For it to be a C question, urgent or not, it would have to be about
valid C code.
1. in the prg bellow what vars are stored on stack, heap, data segment?

int i;

void main()
The line above generates undefined behavior, not valid C code.
{
int j;
int *k = (void *)malloc(1);
Since there is no prototype for malloc() in scope, undefined behavior,
not valid C code.
}

(I think j and k are on stack, but where is i?)
2. how to find sizeof variable w/o using sizeof command?

(no clue)
There is no sizeof command, not a valid C question.
3. what is the o/p of this prg?

void main()
The line above generates undefined behavior, not valid C code.
{
int x = 5;
x = x++;
The line above generates undefined behavior, not valid C code.
printf("x=%i", x);
Since there is no prototype for printf() in scope, this causes
undefined behavior, not valid C code.
}

(I think it shoulld be printing 5 but it prints 6!)
I think it should print, "This code was written by an idiot!".
ps: I am using QuickC 2.0

pps: Is very urgent!
Sorry, non of these is a question about the C language. If you are in
an actual C programming course, and your instructor asked these
questions for a homework assignment, do the following:

1. Immediately drop the class.

2. Complain to the school administration about the unqualified
instructor and erroneous material.

--
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.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Jan 15 '08 #3
Hans Schneider <ha**@localhost.localdomainwrites:
1. in the prg bellow what vars are stored on stack, heap, data segment?

int i;

void main()
{
int j;
int *k = (void *)malloc(1);
}

(I think j and k are on stack, but where is i?)
2. how to find sizeof variable w/o using sizeof command?

(no clue)
3. what is the o/p of this prg?

void main()
{
int x = 5;
x = x++;
printf("x=%i", x);
}

(I think it shoulld be printing 5 but it prints 6!)

ps: I am using QuickC 2.0

pps: Is very urgent!
You urgently need to read <http://www.c-faq.com>.

If you had deliberately set out to ask questions that have been asked
here repeated by clueless newbies (no offense), you couldn't have done
a much better job. I'm not accusing you, but the coincidence is a bit
troubling.

Incidentally, question #2 isn't hard to answer. Use the sizeof
operator. (C has no sizeof *command*.)

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jan 15 '08 #4
On Tue, 15 Jan 2008 03:41:56 +0100 (CET), Hans Schneider
<ha**@localhost.localdomainwrote:
>pps: Is very urgent!
So, you put off your homework until it's nearly too late, then want
someone else to do it for you?
--
Al Balmer
Sun City, AZ
Jan 15 '08 #5

The post from Jack is a classic example of why c.l.c is getting such a
bad name for itself.

However that said, this is nto a place to get last minute homework done.
In article <ag********************************@4ax.com>, Jack Klein
<ja*******@spamcop.netwrites
>On Tue, 15 Jan 2008 03:41:56 +0100 (CET), Hans Schneider
<ha**@localhost.localdomainwrote in comp.lang.c:

For it to be a C question, urgent or not, it would have to be about
valid C code.
>1. in the prg bellow what vars are stored on stack, heap, data segment?

int i;

void main()

The line above generates undefined behavior, not valid C code.
It is valid C... just not strictly conforming .

How does it's undefined behaviour impinge on the question asked?
> {
int j;
int *k = (void *)malloc(1);

Since there is no prototype for malloc() in scope, undefined behavior,
not valid C code.
It is valid C... for reasons of brevity the headers were not included.

However your pedantry is irrelevant to answering the question at hand.
>
> }

(I think j and k are on stack, but where is i?)
2. how to find sizeof variable w/o using sizeof command?

(no clue)

There is no sizeof command, not a valid C question.
Yes it is. Unless you are an internationally obtuse pedant. Try
communicating with humans once in a while
>3. what is the o/p of this prg?

void main()

The line above generates undefined behavior, not valid C code.
It is valid C... just not strictly conforming .

How does it's undefined behaviour impinge on the question asked?

>
> {
int x = 5;
x = x++;

The line above generates undefined behavior,
Why?
>not valid C code.
> printf("x=%i", x);

Since there is no prototype for printf() in scope, this causes
undefined behavior, not valid C code.
The headers were omitted in this code fragment for brevity.
Assuming the headers were there what is the problem?
> }

(I think it shoulld be printing 5 but it prints 6!)

I think it should print, "This code was written by an idiot!".
No the OP is ignorant. You are just insulting and not much of a person.
Try communicating with people occasionally.
>ps: I am using QuickC 2.0

pps: Is very urgent!

Sorry, non of these is a question about the C language.
They are but you don't have the breath of intelligence or common sense
to understand. You must be a liability if you have to work with humans,
If you are in
an actual C programming course, and your instructor asked these
questions for a homework assignment, do the following:
Ignore Jack.
>1. Immediately drop the class.
Keep going but do your own homework and don't do it at the last
minute.
>2. Complain to the school administration about the unqualified
instructor and erroneous material.
But do remember as he can actually communicate with his human class he
is probably going to be better than Jack.

--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ ch***@phaedsys.org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Jan 15 '08 #6
Chris Hills wrote:
In article <ag********************************@4ax.com>, Jack Klein
<ja*******@spamcop.netwrites
>On Tue, 15 Jan 2008 03:41:56 +0100 (CET), Hans Schneider
<ha**@localhost.localdomainwrote in comp.lang.c:
>> {
int x = 5;
x = x++;

The line above generates undefined behavior,

Why?
The FAQ is at http://c-faq.com
this is question 3.3
Jan 15 '08 #7
Chris Hills wrote:
The post from Jack is a classic example of why c.l.c is getting such a
bad name for itself.
I agree with you that Jack was being excessively pedantic, rude, and
not as helpful as he could have been. In his defense, it does appear
that the OP is asking us to do his homework.

....
3. what is the o/p of this prg?
....
How does it's undefined behaviour impinge on the question asked?
The question asked was (after translation into English) "what is the
output of this program". If the behavior of the program is undefined,
the answer to that question must be "it could be anything".
{
int x = 5;
x = x++;
The line above generates undefined behavior,

Why?
6.5p2: "Between the previous and next sequence point an object shall
have its stored value
modified at most once by the evaluation of an expression."

Violation of a "shall" occurring outside of a "Constraints" section
renders the behavior of the program ins undefined.
Jan 15 '08 #8
Chris Hills <ch***@phaedsys.orgwrote:
In article <ag********************************@4ax.com>, Jack Klein
On Tue, 15 Jan 2008 03:41:56 +0100 (CET), Hans Schneider
{
int j;
int *k = (void *)malloc(1);
Since there is no prototype for malloc() in scope, undefined behavior,
not valid C code.

It is valid C... for reasons of brevity the headers were not included.
You do not know that. From the quality of the rest of the code I suspect
that they were not included for reasons of an incompetent teacher not
knowing that they're needed.

Richard
Jan 15 '08 #9
Richard Bos wrote:
Chris Hills <ch***@phaedsys.orgwrote:
>In article <ag********************************@4ax.com>, Jack Klein
>>On Tue, 15 Jan 2008 03:41:56 +0100 (CET), Hans Schneider

{
int j;
int *k = (void *)malloc(1);

Since there is no prototype for malloc() in scope, undefined
behavior, not valid C code.

It is valid C... for reasons of brevity the headers were not
included.

You do not know that. From the quality of the rest of the code I
suspect that they were not included for reasons of an incompetent
teacher not knowing that they're needed.
the (pointless) cast to a void * seconds that...

Bye, Jojo
Jan 15 '08 #10
On Jan 14, 8:41*pm, Hans Schneider <h...@localhost.localdomainwrote:
1. in the prg bellow what vars are stored on stack, heap, data segment?
The concepts of "heap", "stack", and "data segment" are implementation
details independent of the C language (i.e., the language does not
mandate that some things be stored on a stack or a heap). All the
language cares about is the extent (the lifetime of the variable) and
the visibility (where the variable can be used). For example, since
it's declared at file scope, i has *static* extent (storage is
allocated at the beginning of program execution and held until the
program exits), whereas j and k have *local* extent (storage is
allocated when each variable is defined, and deallocated when main
exits). The memory allocated with malloc() (which is not the variable
k, but what k points to) has *dynamic* extent, meaning it's held until
explicitly released.

Again, whether this storage is on a stack or a heap is not defined by
the language, but up to the individual implementation.
* * int i;

* * void main()
main() should return int, not void. The following are standard
definitions for main():

int main(void)
int main(int argc, char **argv) /* or char *argv[] */

Typing main() as void will invoke undefined behavior.
* * {
* * * * int j;
* * * * int *k = (void *)malloc(1);
* * }

(I think j and k are on stack, but where is i?)

2. how to find sizeof variable w/o using sizeof command?

(no clue)
There's a sizeof operator; it's not a command. I can think of one
trick, but I don't know how reliable it would be, so I'm not going to
relay it here.
>
3. what is the o/p of this prg?

* * void main()
* * {
* * * * int x = 5;
* * * * x = x++;
* * * * printf("x=%i", x);
* * }

(I think it shoulld be printing 5 but it prints 6!)
The statement "x = x++;" invokes undefined behavior; technically
speaking, *any* answer is acceptable. It could print 5, it could
print 6, it could print 42, it could launch Rogue.

Replace that line with one of the following:

x++;
x += 1;
x = x + 1;

ps: I am using QuickC 2.0

pps: Is very urgent!
Jan 15 '08 #11
In article
<7b**********************************@l32g2000hse. googlegroups.com>,
ja*********@verizon.net writes
>Chris Hills wrote:
>The post from Jack is a classic example of why c.l.c is getting such a
bad name for itself.

I agree with you that Jack was being excessively pedantic, rude, and
not as helpful as he could have been.
Absolutely.
>In his defense,
There is no defence for that sort of behaviour from an adult.
>it does appear
that the OP is asking us to do his homework.
I agree and that should have been rebuked. Not descending lower than the
level of the OP in manners.
>
...
>3. what is the o/p of this prg?
...
>How does it's undefined behaviour impinge on the question asked?

The question asked was (after translation into English) "what is the
output of this program". If the behavior of the program is undefined,
the answer to that question must be "it could be anything".
But starting with

void main ()

which is not strictly conforming for a hosted environment is probably
going to be accepted by most compilers.

I would expect an adult to read it as a main with no parameters or
return. Not produce the sort of childish reaction it got.

All we learn from Jacks response is he is probably very bright but very
poor at communicating with humans,.
--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ ch***@phaedsys.org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Jan 15 '08 #12
In article <47*****************@news.xs4all.nl>, Richard Bos
<rl*@hoekstra-uitgeverij.nlwrites
>Chris Hills <ch***@phaedsys.orgwrote:
>In article <ag********************************@4ax.com>, Jack Klein
>On Tue, 15 Jan 2008 03:41:56 +0100 (CET), Hans Schneider

{
int j;
int *k = (void *)malloc(1);

Since there is no prototype for malloc() in scope, undefined behavior,
not valid C code.

It is valid C... for reasons of brevity the headers were not included.

You do not know that. From the quality of the rest of the code I suspect
that they were not included for reasons of an incompetent teacher not
knowing that they're needed.
Possibly but I think it is far more likely they were not there to save
typing. In most programming books for example K&R 2nd ed main is

main ()

and K&R 2nd ed is a book many here advocate as a good book to learn C
from.....

So K&R is not C either... EVERY example in the book is "undefined" and
a C like language but not C...

How pedantic do you want to get?

Constructive criticism is one thing but proving you are a pedant with a
photographic memory and no social skills of little help to anyone.

--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ ch***@phaedsys.org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Jan 15 '08 #13
Chris Hills <ch***@phaedsys.orgwrites:
So K&R is not C either... EVERY example in the book is "undefined"
and a C like language but not C...
The examples in K&R2 are written in C++, according to the
preface.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Jan 15 '08 #14
Ben Pfaff wrote:
Chris Hills <ch***@phaedsys.orgwrites:
So K&R is not C either... EVERY example in the book is "undefined"
and a C like language but not C...

The examples in K&R2 are written in C++, according to the
preface.

Please don't feed the troll.

Brian
Jan 15 '08 #15
In article <5v*************@mid.individual.net>,
Default User <de***********@yahoo.comwrote:
>Ben Pfaff wrote:
>Chris Hills <ch***@phaedsys.orgwrites:
So K&R is not C either... EVERY example in the book is "undefined"
and a C like language but not C...

The examples in K&R2 are written in C++, according to the
preface.


Please don't feed the troll.
So now Chris Hills is a troll?

Cool. Welcome aboard, Chris!

Jan 15 '08 #16
In article <5v*************@mid.individual.net>, Default User
<de***********@yahoo.comwrites
>Ben Pfaff wrote:
>Chris Hills <ch***@phaedsys.orgwrites:
So K&R is not C either... EVERY example in the book is "undefined"
and a C like language but not C...

The examples in K&R2 are written in C++, according to the
preface.


Please don't feed the troll.

Brian
I assume you are you referring to Jack who started this?
--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ ch***@phaedsys.org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Jan 15 '08 #17
In article <87************@blp.benpfaff.org>, Ben Pfaff
<bl*@cs.stanford.eduwrites
>Chris Hills <ch***@phaedsys.orgwrites:
>So K&R is not C either... EVERY example in the book is "undefined"
and a C like language but not C...

The examples in K&R2 are written in C++, according to the
preface.
That's not what it said. Initial testing in a C++ compiler and final
testing in an ANSI C compiler.

At the time C++ was a superset of C. Both have moved since then.

--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ ch***@phaedsys.org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Jan 15 '08 #18
John Bode wrote, On 15/01/08 17:19:
On Jan 14, 8:41 pm, Hans Schneider <h...@localhost.localdomainwrote:
<snip>
>2. how to find sizeof variable w/o using sizeof command?

(no clue)

There's a sizeof operator; it's not a command. I can think of one
trick, but I don't know how reliable it would be, so I'm not going to
relay it here.
<snip>

It is easy. All you have to do is take the following two steps...
1) Wait until after the OPs deadline before giving him the answer
2) Remember that any variable can be treated as an array of 1 element
3) Remember the rules of pointer arithmetic.

Note that it is *not* possible to write a replacement for the sizeof
operator, but that was not the question asked.
--
Flash Gordon
Jan 15 '08 #19
Chris Hills said:

<snip>
At the time C++ was a superset of C.
C++ has never been a superset of C. If it were, all C programs would be C++
programs, which is clearly not the case.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jan 15 '08 #20
Chris Hills wrote:
But starting with

void main ()

which is not strictly conforming for a hosted environment is probably
going to be accepted by most compilers.
On what do you base this claim? How many compilers have you tried?

And even if it's accepted by most compilers, it's still not accepted by
all, and may not be accepted by future versions of compilers which
accept it today.
I would expect an adult to read it as a main with no parameters or
return. Not produce the sort of childish reaction it got.
It doesn't matter what you expect an adult to read this as; the fact is
that a C implementation is not required to deal with it. Several do not
like it at all.

Phil
Jan 15 '08 #21
Chris Hills wrote:
>
The post from Jack is a classic example of
why c.l.c is getting such a bad name for itself.

However that said,
this is nto a place to get last minute homework done.

In article <ag********************************@4ax.com>, Jack Klein
<ja*******@spamcop.netwrites
On Tue, 15 Jan 2008 03:41:56 +0100 (CET), Hans Schneider
<ha**@localhost.localdomainwrote in comp.lang.c:
x = x++;
The line above generates undefined behavior,

Why?
http://c-faq.com/expr/ieqiplusplus.html

--
pete
Jan 15 '08 #22
On 15 Jan 2008 at 20:37, Chris Hills wrote:
In article <5v*************@mid.individual.net>, Default User
<de***********@yahoo.comwrites
>>Ben Pfaff wrote:
>>Chris Hills <ch***@phaedsys.orgwrites:

So K&R is not C either... EVERY example in the book is "undefined"
and a C like language but not C...

The examples in K&R2 are written in C++, according to the
preface.


Please don't feed the troll.

Brian

I assume you are you referring to Jack who started this?
I'm afraid not.

You've expressed disagreement with the CLC mafia, ergo you are a troll.

Jan 15 '08 #23
Chris Hills wrote:
>
...

In article <ag********************************@4ax.com>, Jack Klein
<ja*******@spamcop.netwrites
>On Tue, 15 Jan 2008 03:41:56 +0100 (CET), Hans Schneider
<ha**@localhost.localdomainwrote in comp.lang.c:
>> {
int j;
int *k = (void *)malloc(1);

Since there is no prototype for malloc() in scope, undefined behavior,
not valid C code.

It is valid C... for reasons of brevity the headers were not included.
I doubt it. Casting the result of malloc() to a <void *before
assigning it to an <int *gives off the classic smell of someone
throwing in casts to suppress warnings. Including the header would
admittedly have been a little less brief, but would at least be correct
and slightly reduce the impression that the instructor is clueless.
Jan 16 '08 #24
On Jan 14, 9:41 pm, Hans Schneider <h...@localhost.localdomainwrote:
1. in the prg bellow what vars are stored on stack, heap, data segment?
Heap: malloc()ed memory
Stack: local variables
Data Segment: No clue (haven't done 16-bit x86 asm in a *long* time)
>
int i;
On the outermost stack frame.
void main()
A return type of void works for main(); however, a non-int return type
for main is *not* recommended as int can potentially cause
segmentation faults and other problems.
{
int j;
main()'s stack frame.
int *k = (void *)malloc(1);
k is stored in main()'s stack frame. It contains an address to heap
memory.
i.e., k is on the stack; *k is on the heap.
Furthermore, I think you meant to type:
int *k = (int *)malloc(1 * sizeof(int));
Big difference, for the following reasons:
1) malloc()'s return type is void *, making (void *)malloc()
redundant.
I think you meant (int *)malloc() so that malloc() returns int *.
2) malloc()'s argument is the size of memory *in bytes* to allocate.
malloc(1) allocates 1 byte, malloc(1 * sizeof(int)) allocates memory
for 1 int; the size in bytes allocated being sizeof(int). Do *not* do
sizeof(void) *at all*, as this is undefined (for good reasons).
You could also use (element type *)calloc(number of elements,
sizeof(element type)).
Or create a (preprocessor) macro like this:
#define mymalloc(n, type) (type *)malloc(n * sizeof(type))
and replace "int * i = (int *)malloc(n * sizeof(int))"
with "int * i = mymalloc(n, int);"
}
2. how to find sizeof variable w/o using sizeof command?
sizeof is a compiler (not preprocessor) macro. During compilation, the
compiler replaces sizeof(foo) with a number equal to the number of
bytes 1 foo takes up in memory. This is why malloc() and realloc()
*must* have "* sizeof(type)" in the size argument for them to perform
as expected.
Example (assume sizeof(int) == 2):

int * i = (int *)malloc(sizeof(int));

is equal to
int * i = (int *)malloc(2);
>
3. what is the o/p of this prg?

void main()
{
int x = 5;
x = x++;
printf("x=%i", x);
}
x = x++ is undefined, but judging on how ++ works, I think the program
expands it to something like this (asm in comments):
x = x; /* MOV X, X */
x++; /* INC X */
The program in asm:
..data
szPrintfFormat db "x=%i", 0
..text
start:
main:
MOV [SP - 4], 5 /* int x = 5 */
MOV [SP - 4], [SP - 4] /* x = x */
INC [SP - 4] /* x++ */
PUSH [SP - 4] /* push x on calling stack */
PUSH ADDR szPrintfFormat /* push address of null-
terminated string szPrintfFormat onto calling stack */
CALL printf
XOR AX, AX /* RET uses register AX as return value; XORing
it will set it to 0 */
RET /* return */
end main
int 0x23 /* I think this is the return-to-DOS interrupt */
end start
>
ps: I am using QuickC 2.0
You're using DOS. That's a bit of a help - it lets me type out valid,
working asm to show exactly how
pps: Is very urgent!
Sounds like a homework question. I explained it to the best (read most
verbose) of my abilities.

Jan 16 '08 #25
Jack Klein schrieb:
On Tue, 15 Jan 2008 03:41:56 +0100 (CET), Hans Schneider
<ha**@localhost.localdomainwrote in comp.lang.c:

For it to be a C question, urgent or not, it would have to be about
valid C code.
>1. in the prg bellow what vars are stored on stack, heap, data segment?

int i;

void main()

The line above generates undefined behavior, not valid C code.
>3. what is the o/p of this prg?

void main()

The line above generates undefined behavior, not valid C code.
I didn't write this code, but what is wrong about it?
In 'C: the complete reference' all prgs start like that.
> {
int x = 5;
x = x++;

The line above generates undefined behavior, not valid C code.
Is x++ not the same as x=x+1?
> printf("x=%i", x);

Since there is no prototype for printf() in scope, this causes
undefined behavior, not valid C code.
QuickC didn't complain.
lcc-win32 prints lots of warnings.
Warning test.c: 3 old-style function definition for 'main'
Warning test.c: 3 missing prototype for 'main'
Warning test.c: 3 'void main()' is a non-ANSI definition
Warning test.c: 6 missing prototype for printf
Warning test.c: 6 Missing prototype for 'printf'
0 errors, 5 warnings

What do they mean?
Why are there 2 warnings on line 6?
Some of them go away with #include <stdio.h>.
But where is the prototype for main()?
> }

(I think it shoulld be printing 5 but it prints 6!)

I think it should print, "This code was written by an idiot!".
With MS QuickC and GCC it prints 6.
With lcc-win32 it prints 5.
But gcc says 'operation on x may be undefined'.
I'll have to investigate why.
>ps: I am using QuickC 2.0

pps: Is very urgent!

Sorry, non of these is a question about the C language. If you are in
an actual C programming course, and your instructor asked these
questions for a homework assignment, do the following:

1. Immediately drop the class.
I can't!
It is required for an advanced class.
2. Complain to the school administration about the unqualified
instructor and erroneous material.
I don't think that's gonna work.
He is a cousin of the principal.

Jan 16 '08 #26
On Tue, 15 Jan 2008 16:00:32 +0000, Chris Hills <ch***@phaedsys.org>
wrote in comp.lang.c:
>
The post from Jack is a classic example of why c.l.c is getting such a
bad name for itself.

However that said, this is nto a place to get last minute homework done.
In article <ag********************************@4ax.com>, Jack Klein
<ja*******@spamcop.netwrites
On Tue, 15 Jan 2008 03:41:56 +0100 (CET), Hans Schneider
<ha**@localhost.localdomainwrote in comp.lang.c:

For it to be a C question, urgent or not, it would have to be about
valid C code.
1. in the prg bellow what vars are stored on stack, heap, data segment?

int i;

void main()
The line above generates undefined behavior, not valid C code.

It is valid C... just not strictly conforming .
No, it is not valid C. Undefined behavior renders it invalid.
How does it's undefined behaviour impinge on the question asked?
{
int j;
int *k = (void *)malloc(1);
Since there is no prototype for malloc() in scope, undefined behavior,
not valid C code.

It is valid C... for reasons of brevity the headers were not included.
Where in the OP's post did it state that? What evidence do you have
that this is true?

There are implementations, even those where int and pointer happen to
be the same size, that this code will fail miserably, because pointers
and ints are returned in different manners.
However your pedantry is irrelevant to answering the question at hand.
No, it is not. Once there is "void main()" there are no requirements
at all on the program, because it is not C. And if malloc() is called
without a prototype in scope, and I see no evidence that there is a
prototype in scope, that is just more undefined behavior.

Not to mention the fact that "the question at hand" has no answer in
the C language, which does not define a "stack". Nor does it specify
where an external object like 'i' resides.
}

(I think j and k are on stack, but where is i?)
2. how to find sizeof variable w/o using sizeof command?

(no clue)
There is no sizeof command, not a valid C question.

Yes it is. Unless you are an internationally obtuse pedant. Try
communicating with humans once in a while
I have in no way attempted to deny that the OP is human, despite the
egregious homework post. In fact, I am rather certain that the OP is,
indeed, human. While bots can generate incredibly varied text these
days, I don't think there is one quite up to generating a "do my
homework" post like this.
3. what is the o/p of this prg?

void main()
The line above generates undefined behavior, not valid C code.

It is valid C... just not strictly conforming .

How does it's undefined behaviour impinge on the question asked?
Once a program generates undefined behavior, the C language no longer
imposes any requirements on it. Period.
{
int x = 5;
x = x++;
The line above generates undefined behavior,

Why?
Are you actually the Chris Hills of PhaedruS SystemS, Hitex, MISRA,
and the BSI/ISO C panel, or a troll using his email address on your
posts?

Aren't you aware of MISRA-C 2004 12.2 (required)?

Aren't you aware of 6.3 in ISO 9899:1990 and the identical wording in
ISO 9899:1999:

"Between the previous and next sequence point an object shall have its
stored value modified at most once by the evaluation of an
expression."

Since this code modifies the value of the object 'x' twice without an
intervening sequence point, it violate a "shall" clause outside of a
constraints section, which, of course, produces undefined behavior.
not valid C code.
printf("x=%i", x);
Since there is no prototype for printf() in scope, this causes
undefined behavior, not valid C code.
Again, apparently, you have access to information that we mere
mortals, who only read what the OP posted, are denied. Given the
caliber of the post, I choose to assume that the headers are not
included.
The headers were omitted in this code fragment for brevity.
Assuming the headers were there what is the problem?
Since the previous statement modified an object twice without an
intervening sequence point, whether this printf() call executes is
immaterial from a C language point of view, let alone what output it
might happen to produce, if any, if it does execute.

What do you think the output will be? And why?
}

(I think it shoulld be printing 5 but it prints 6!)
I think it should print, "This code was written by an idiot!".

No the OP is ignorant. You are just insulting and not much of a person.
Try communicating with people occasionally.
I do not assume that the OP wrote the code. In fact I think it is
more likely that it was given to him as a homework assignment. And
that's the sad part of it, that he is dealing with an instructor, or
perhaps a tutorial web site, that considers these legitimate
questions, and they are not.
ps: I am using QuickC 2.0

pps: Is very urgent!
Sorry, non of these is a question about the C language.

They are but you don't have the breath of intelligence or common sense
to understand. You must be a liability if you have to work with humans,
Oh, I'm sad, for now you have turned to personal insult. And
incorrect, because most of my coworkers are human, and the rest almost
so. ;)
If you are in
an actual C programming course, and your instructor asked these
questions for a homework assignment, do the following:

Ignore Jack.
1. Immediately drop the class.

Keep going but do your own homework and don't do it at the last
minute.
As a member of several standards bodies and a certified engineer, I am
appalled at the fact that you are condoning the teaching of blatantly
incorrect material.
2. Complain to the school administration about the unqualified
instructor and erroneous material.

But do remember as he can actually communicate with his human class he
is probably going to be better than Jack.
Are you the real Chris Hills?

--
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.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Jan 16 '08 #27
"Hans Schneider" <ha**@localhost.localdomainwrote in message
news:18****************@aioe.org...
>
1. in the prg bellow what vars are stored on stack, heap, data segment?

int i;

void main()
{
int j;
int *k = (void *)malloc(1);
}

(I think j and k are on stack, but where is i?)
the answers are specific to your environment. i take this risk of
displeasing the Std C gods to
give "inaccurate" answers:

i and k are on the "stack" (assuming the enviroment uses a stack like data
structure to maintain activation records)
what is pointed to by k (ie *k) is on the "heap" (if we assume that
environment allows us to call the place when malloc
reserves storage to be calledas "heap")

and i is on the "bss" segment (assume that the ".data" is always used by
compiler for initialized gobal and static variable and ".bss" is for
unitialized global and static) variable.

notice the number of assumptions. but nevertheless is true for some
environments (which is probably the case with QuickC).

>
2. how to find sizeof variable w/o using sizeof command?

(no clue)
I had seen a post which used pointer arithmetic. Its subject to correction
by the experts:
sizeof(i) == ((&i) + 1) - (&i))
Perhaps useful for your assignment
>

3. what is the o/p of this prg?

void main()
{
int x = 5;
x = x++;
printf("x=%i", x);
}

(I think it shoulld be printing 5 but it prints 6!)

ps: I am using QuickC 2.0

pps: Is very urgent!
Undefined behaviour.

Final Note: Why is the compiler called QuickC ? I think perhaps because it
allows you to quickly type any C program
without proper headers and declaration and yet produce some answer.
Jan 16 '08 #28
On Jan 15, 11:55 pm, "Ravishankar S" <ravishanka...@in.bosch.com>
wrote:
Final Note: Why is the compiler called QuickC ?
So that the programmer can *QUickl*ly*C* that Microsoft products are
standards-incompliant in the worst case. :P
Jan 16 '08 #29
In article <fm**********@news4.fe.internet.bosch.com>,
Ravishankar S <ra***********@in.bosch.comwrote:
>2. how to find sizeof variable w/o using sizeof command?
>I had seen a post which used pointer arithmetic. Its subject to correction
by the experts:
sizeof(i) == ((&i) + 1) - (&i))
Perhaps useful for your assignment
The result of that would always be 1, for any variable of any type.
You missed a couple of casts.
--
So you found your solution
What will be your last contribution?
-- Supertramp (Fool's Overture)
Jan 16 '08 #30
"Walter Roberson" <ro******@ibd.nrc-cnrc.gc.cawrote in message
news:fm**********@canopus.cc.umanitoba.ca...
In article <fm**********@news4.fe.internet.bosch.com>,
Ravishankar S <ra***********@in.bosch.comwrote:
2. how to find sizeof variable w/o using sizeof command?
I had seen a post which used pointer arithmetic. Its subject to
correction
by the experts:
sizeof(i) == ((&i) + 1) - (&i))
Perhaps useful for your assignment

The result of that would always be 1, for any variable of any type.
You missed a couple of casts.
oh yes. ((void*)((&i) + 1) - (void*)(&i)).but arithmetic on void pointers is
not defined i guess.

Jan 16 '08 #31
andreyvul said:
On Jan 15, 11:55 pm, "Ravishankar S" <ravishanka...@in.bosch.com>
wrote:
>Final Note: Why is the compiler called QuickC ?
So that the programmer can *QUickl*ly*C* that Microsoft products are
standards-incompliant in the worst case. :P
Discounting C99, conformance to which very few implementors have seriously
bothered to attempt, what conformance issues do you have with Microsoft's
series of C compilers? That is, in what way do you think they fail to
conform to C90?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jan 16 '08 #32
Hans Schneider wrote:
>
1. in the prg bellow what vars are stored on stack, heap, data segment?

int i;
void main() {
int j;
int *k = (void *)malloc(1);
}

(I think j and k are on stack, but where is i?)
Nowhere. I count at least 4 (possibly 3 on C99) errors.
2. how to find sizeof variable w/o using sizeof command?

(no clue)
Not possible for general variables. char is size 1.
3. what is the o/p of this prg?

void main() {
int x = 5;
x = x++;
printf("x=%i", x);
}

(I think it shoulld be printing 5 but it prints 6!)
ps: I am using QuickC 2.0
pps: Is very urgent!
Undefined behaviour, besides the errors. ANY result is possible.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

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

Jan 16 '08 #33
Chris Hills wrote:
Richard Bos <rl*@hoekstra-uitgeverij.nlwrites
.... snip ...
>
>You do not know that. From the quality of the rest of the code I
suspect that they were not included for reasons of an incompetent
teacher not knowing that they're needed.

Possibly but I think it is far more likely they were not there to
save typing. In most programming books for example K&R 2nd ed
main is

main ()
Not if you have the intelligence to apply the published errata.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

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

Jan 16 '08 #34
Hans Schneider wrote:
Jack Klein schrieb:
>Hans Schneider <ha**@localhost.localdomainwrote:
.... snip ...
>>
>> void main()

The line above generates undefined behavior, not valid C code.

I didn't write this code, but what is wrong about it?
In 'C: the complete reference' all prgs start like that.
If that is the H Schildt (sp?) book, you are lucky. You have now
been advised that it is useless and stuffed full of errors, because
the author doesn't understand C. See if you can get some practical
use of it as a replacement for a log in the fireplace.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

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

Jan 16 '08 #35
Hans Schneider wrote:
Jack Klein schrieb:
>On Tue, 15 Jan 2008 03:41:56 +0100 (CET), Hans Schneider
<ha**@localhost.localdomainwrote in comp.lang.c:
>>3. what is the o/p of this prg?

void main()

The line above generates undefined behavior, not valid C code.

I didn't write this code, but what is wrong about it?
In 'C: the complete reference' all prgs start like that.
One billions flies can't be wrong: dung tasts great

Bye, Jojo.
Jan 16 '08 #36
In article <4d******************************@bt.com>, Richard Heathfield
<rj*@see.sig.invalidwrites
>Chris Hills said:

<snip>
>At the time C++ was a superset of C.

C++ has never been a superset of C.
According to its author it was when he started.
If it were, all C programs would be C++
programs, which is clearly not the case.
Things move C++ was based on C89 since then noth C and C++ have moved
in different directions.

In the beginning C++ was a superset of C otherwise they could not have
used the C++ compiler for initial testing and then a C compiler for
final testing,
--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ ch***@phaedsys.org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Jan 16 '08 #37
In article <47***************@yahoo.com>, CBFalconer
<cb********@yahoo.comwrites
>Hans Schneider wrote:
>Jack Klein schrieb:
>>Hans Schneider <ha**@localhost.localdomainwrote:
... snip ...
>>>
void main()

The line above generates undefined behavior, not valid C code.

I didn't write this code, but what is wrong about it?
In 'C: the complete reference' all prgs start like that.

If that is the H Schildt (sp?) book, you are lucky. You have now
been advised that it is useless and stuffed full of errors, because
the author doesn't understand C. See if you can get some practical
use of it as a replacement for a log in the fireplace.

--
[mail]: Chuck F (cbfalconer at maineline dot net)

For once we agree completely!
BTW Ditto for his C++ books
--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ ch***@phaedsys.org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Jan 16 '08 #38
Chris Hills said:
In article <4d******************************@bt.com>, Richard Heathfield
<rj*@see.sig.invalidwrites
>>Chris Hills said:

<snip>
>>At the time C++ was a superset of C.

C++ has never been a superset of C.

According to its author it was when he started.
It would be interesting to hear him attempting to defend that view.

>If it were, all C programs would be C++
programs, which is clearly not the case.

Things move C++ was based on C89 since then noth C and C++ have moved
in different directions.
It is trivial to write a legal C89 program that is not a legal program in
the C++ that was based on C89.

In the beginning C++ was a superset of C otherwise they could not have
used the C++ compiler for initial testing and then a C compiler for
final testing,
Of course they could - and they did. For some C programs to be legal C++
programs does not require C++ to be a superset of C.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jan 16 '08 #39
In article <k9********************************@4ax.com>, Jack Klein
<ja*******@spamcop.netwrites
>On Tue, 15 Jan 2008 16:00:32 +0000, Chris Hills <ch***@phaedsys.org>
wrote in comp.lang.c:
>However your pedantry is irrelevant to answering the question at hand.

No, it is not. Once there is "void main()" there are no requirements
at all on the program, because it is not C. And if malloc() is called
It is this sort of pedantry that is very unhelpful to everyone.
>3. what is the o/p of this prg?
void main()

The line above generates undefined behavior, not valid C code.
It is valid C... just not strictly conforming .

How does it's undefined behaviour impinge on the question asked?
Once a program generates undefined behavior, the C language no longer
imposes any requirements on it. Period.
This sort of pedantry reflects quite badly on you. You have not helped
anyone

> {
int x = 5;
x = x++;

The line above generates undefined behavior,
Why?

Are you actually the Chris Hills of PhaedruS SystemS, Hitex, MISRA,
and the BSI/ISO C panel,
Yes.
>Aren't you aware of MISRA-C 2004 12.2 (required)?
Aren't you aware of 6.3 in ISO 9899:1990 and the identical wording in
ISO 9899:1999:
"Between the previous and next sequence point an object shall have its
stored value modified at most once by the evaluation of an
expression."
Since this code modifies the value of the object 'x' twice without an
intervening sequence point, it violate a "shall" clause outside of a
constraints section, which, of course, produces undefined behavior.
That is the answer you could have given instead of the reply you gave.
Giving answers like
>The line above generates undefined behavior,
With no explanation is not clever. It might make you feel good/ superior
but it is not a good way to communicate with people or help the OP
>(I think it shoulld be printing 5 but it prints 6!)
I think it should print, "This code was written by an idiot!".
No the OP is ignorant. You are just insulting and not much of a person.
Try communicating with people occasionally.
I do not assume that the OP wrote the code. In fact I think it is
more likely that it was given to him as a homework assignment. And
that's the sad part of it, that he is dealing with an instructor, or
perhaps a tutorial web site, that considers these legitimate
questions, and they are not.
But instead of trying to help you were as obtuse as possible. That is
worse than the original teacher who is trying to help the students.

>ps: I am using QuickC 2.0
pps: Is very urgent!
Sorry, non of these is a question about the C language.

They are but you don't have the breath of intelligence or common sense
to understand. You must be a liability if you have to work with humans,

Oh, I'm sad, for now you have turned to personal insult.
No more than you did with your original replies. (And this one) If you
can't see that you may need to talk to someone about dealing with
people.
And
incorrect, because most of my coworkers are human, and the rest almost
so. ;)
:-)
If you are in
an actual C programming course, and your instructor asked these
questions for a homework assignment, do the following:
Ignore Jack.
>1. Immediately drop the class.
Keep going but do your own homework and don't do it at the last
minute.
As a member of several standards bodies and a certified engineer, I am
appalled at the fact that you are condoning the teaching of blatantly
incorrect material.
I am not condoning it. Several people gave helpful and instructive
replies. Your reply to the OP was, whilst being accurate was neither
helpful nor instructive on what the problems were or how to correct
them. I was not suggesting that you do the homework. For that is clearly
what it was but be helpful to the OP in a constructive manner. .

>2. Complain to the school administration about the unqualified
instructor and erroneous material.
But do remember as he can actually communicate with his human class he
is probably going to be better than Jack.
Are you the real Chris Hills?
One of them....

A couple of weeks ago I found another one 30 miles from where I live who
is in IT and has a blog... Search on your name and you find there are
often many people with *your* name.

--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ ch***@phaedsys.org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Jan 16 '08 #40
Hans Schneider wrote:
Jack Klein schrieb:
>On Tue, 15 Jan 2008 03:41:56 +0100 (CET), Hans Schneider
<ha**@localhost.localdomainwrote in comp.lang.c:

For it to be a C question, urgent or not, it would have to be about
valid C code.
>>1. in the prg bellow what vars are stored on stack, heap, data segment?

int i;

void main()
The line above generates undefined behavior, not valid C code.
>>3. what is the o/p of this prg?

void main()
The line above generates undefined behavior, not valid C code.

I didn't write this code, but what is wrong about it?
In 'C: the complete reference' all prgs start like that.
C: the complete reference by Herbert Schildt is a book everyone knows
for all the wrong reasons. While there are many poor C textbooks, this
one is notorious. For an incomplete list of its errors, see:
http://www.seebs.net/c/c_tcr.html
>> {
int x = 5;
x = x++;
The line above generates undefined behavior, not valid C code.

Is x++ not the same as x=x+1?
As a single statement (not an expression), yes. Which is to say the
statements
x++;
and
x=x+1;
are identical in behaviour.

This is irrelevent, though, because what was written was
x = x++;
which isn't either of the above two statements. Please see the C FAQ,
question 3.3.
>Sorry, non of these is a question about the C language. If you are in
an actual C programming course, and your instructor asked these
questions for a homework assignment, do the following:

1. Immediately drop the class.

I can't!
It is required for an advanced class.
If this is the basic class, I shudder to think what the advanced class
contains.
>2. Complain to the school administration about the unqualified
instructor and erroneous material.

I don't think that's gonna work.
He is a cousin of the principal.
Nevertheless, if his entire class issues complaints, it will look bad.
If the complaints are backed up with citations from the C Standard or
one of its drafts, it will also look bad. There is more support
available to you at a university than you realise. Seek advice from your
student's union, or your tutor.

You have to ask yourself: Why am I doing this class? Is it just to get
letters after my name, and a shiny certificate? Or am I doing it to
actually learn something? If I want to learn something when my
instructor is teaching me falsehoods, how do I go about doing that?

Phil
Jan 16 '08 #41
In article <fm**********@aioe.org>, Philip Potter <pg*@doc.ic.ac.uk>
writes
>
>>2. Complain to the school administration about the unqualified
instructor and erroneous material.
I don't think that's gonna work. He is a cousin of the principal.

Nevertheless, if his entire class issues complaints, it will look bad.
If the complaints are backed up with citations from the C Standard or
one of its drafts, it will also look bad
It depends where this university is. It may get the OP chucked out.
Lecturers don't like being name to look silly. You have to tread
carefully especially as he is related to the principal.

When training a junior rugby team they got press-ups for any
infringement in training. (As I said, "You may not be able to play rugby
but by god you will be fit!" :-) 5 was the usual. However arguing
with the ref (in a training match) got you 10. Arguing with the ref, and
you were right got you 15!
This was to stop them behaving like football payers who always argue
with the ref.

The point being that in a real rugby match if you argue with the referee
as a player you tend to get sent off. The Ref is God.

I suspect that if you argue with a lecturer who is only there because
his cousin is the principal you ain't going to win (or get your
qualification as you will probably fail a crucial exam) and that isn't
in the C standard either.
>. There is more support available to you at a university than you
realise. Seek advice from your student's union, or your tutor.
What Student Union? It depends where this university is. Besides as in
all gambling the House Wins
>You have to ask yourself: Why am I doing this class? Is it just to get
letters after my name, and a shiny certificate?
In some cultures that is what matters. This is why they turn up at
interviews with the certificates. I have seen some students complain
that the degree certificate did not look prestigious enough to put
before an employer. This was important in the country they were going
back to.
Or am I doing it to actually learn something? If I want to learn
something when my instructor is teaching me falsehoods, how do I go
about doing that?
Without failing the course..... you have to pass the course to get the
qualification to get a job to eat to live.

You can be right later. Being right and broke means you starve.

It is a difficult problem for students in some "universities" You HAVE
to pass the course to get a job. This usually means not fighting with
the lecturers and the Establishment.

However you can learn by yourself in places where they give good helpful
advice not just one line "not standard C" as Jack did which helps no
one other than making him feel superior and probably frightening off the
OP from asking any more questions

If you want to help do as John Bode and andreyvul did. Even Keith
Thompson was helpful without doing the homework, a difficult balance.

You are not sure if the question is asked because the OP wanted the
assignment done for him because he is lazy or because he wanted real
guidance and may or may not have known it was full of holes.
--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ ch***@phaedsys.org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Jan 16 '08 #42
On 16 jan, 02:35, andreyvul <andrey....@gmail.comwrote:
On Jan 14, 9:41 pm, Hans Schneider <h...@localhost.localdomainwrote:1. in the prg bellow what vars are stored on stack, heap, data segment?

Heap: malloc()ed memory
Stack: local variables
This is true for most PC-based C implementations, but it is not
actually required by the language.
Data Segment: No clue (haven't done 16-bit x86 asm in a *long* time)
In a typical PC implementation, the data segment is used to hold
variables with static storage duration (i.e. variables declared as
static and global variables).
This is also not required by the language definition.
>
int i;

On the outermost stack frame.
Could be, but for most PC implementations it will live in the data
segment.

<snip>
int *k = (void *)malloc(1);
<snip>
Furthermore, I think you meant to type:
int *k = (int *)malloc(1 * sizeof(int));
Big difference, for the following reasons:
1) malloc()'s return type is void *, making (void *)malloc()
redundant.
I think you meant (int *)malloc() so that malloc() returns int *.
Actually, the cast is completely redundant and can mask the error of
not having a prototype visible.
The correct call would be:
int *k = malloc(1 * sizeof(int));
or (preferrably)
int *k = malloc(1 * sizeof(*k));

<snip>
2. how to find sizeof variable w/o using sizeof command?

sizeof is a compiler (not preprocessor) macro.
sizeof is not a macro at all, but an operator, like +, -, *, etc.
The main differences with the other operators are:
- the operator sizeof is identified by a keyword instead of a symbol
- you can apply the sizeof operator to a type.

<snip>
3. what is the o/p of this prg?
void main()
{
int x = 5;
x = x++;
printf("x=%i", x);
}

x = x++ is undefined, but judging on how ++ works, I think the program
expands it to something like this (asm in comments):
x = x; /* MOV X, X */
x++; /* INC X */
This is one possible implemenation.
It is equally possible to do the operation the other way around, or
even in parallel.
The final verdict is: Whatever the result is, it is correct, because
the behaviour is not defined.

<snip>
Sounds like a homework question. I explained it to the best (read most
verbose) of my abilities.
And I corrected the misconceptions to the best of my abilities.

Bart v Ingen Schenau
Jan 16 '08 #43
Hans Schneider <ha**@localhost.localdomainwrote:
Jack Klein schrieb:
1. Immediately drop the class.

I can't!
It is required for an advanced class.
2. Complain to the school administration about the unqualified
instructor and erroneous material.

I don't think that's gonna work.
He is a cousin of the principal.
Get the hell out of there. Now. This is a _very_ bad sign, and the code
is even worse. Your diploma will be worthless.

Richard
Jan 16 '08 #44
Chris Hills wrote:
In article <fm**********@aioe.org>, Philip Potter <pg*@doc.ic.ac.uk>
writes
>>>2. Complain to the school administration about the unqualified
instructor and erroneous material.
I don't think that's gonna work. He is a cousin of the principal.
Nevertheless, if his entire class issues complaints, it will look bad.
If the complaints are backed up with citations from the C Standard or
one of its drafts, it will also look bad

It depends where this university is. It may get the OP chucked out.
Lecturers don't like being name to look silly. You have to tread
carefully especially as he is related to the principal.

When training a junior rugby team they got press-ups for any
infringement in training. (As I said, "You may not be able to play rugby
but by god you will be fit!" :-) 5 was the usual. However arguing
with the ref (in a training match) got you 10. Arguing with the ref, and
you were right got you 15!
This was to stop them behaving like football payers who always argue
with the ref.

The point being that in a real rugby match if you argue with the referee
as a player you tend to get sent off. The Ref is God.
Yes, but rugby doesn't exist outside of the stadium. C does exist
outside of the OP's university, and his lecturer sure isn't God as far
as C is concerned, even if he is as far as the university is concerned.
I suspect that if you argue with a lecturer who is only there because
his cousin is the principal you ain't going to win (or get your
qualification as you will probably fail a crucial exam) and that isn't
in the C standard either.
I wasn't suggesting arguing with the lecturer; I was suggesting seeking
advice and raising matters with the University administration. If the
principal is abusing his position to keep his incompetent relation in a
job, then perhaps the student press could get involved too. If this
really is the case, it's scandalous.
>. There is more support available to you at a university than you
realise. Seek advice from your student's union, or your tutor.

What Student Union? It depends where this university is. Besides as in
all gambling the House Wins
So you think universities are similar to casinos?
>You have to ask yourself: Why am I doing this class? Is it just to get
letters after my name, and a shiny certificate?

In some cultures that is what matters. This is why they turn up at
interviews with the certificates. I have seen some students complain
that the degree certificate did not look prestigious enough to put
before an employer. This was important in the country they were going
back to.
>Or am I doing it to actually learn something? If I want to learn
something when my instructor is teaching me falsehoods, how do I go
about doing that?

Without failing the course..... you have to pass the course to get the
qualification to get a job to eat to live.
It depends on whether the qualification is worth the paper it's written
on. Getting a degree doesn't guarantee a job, and not getting a degree
doesn't guarantee unemployment.
It is a difficult problem for students in some "universities" You HAVE
to pass the course to get a job. This usually means not fighting with
the lecturers and the Establishment.
See above.

Phil
Jan 16 '08 #45
On 16 Jan, 01:35, andreyvul <andrey....@gmail.comwrote:
On Jan 14, 9:41 pm, Hans Schneider <h...@localhost.localdomainwrote:
I suggest you read the other posts in this thread.
Then read the clc FAQ.
Then read more clc posts.

1. in the prg bellow what vars are stored on stack, heap, data segment?
"program below"
Heap: malloc()ed memory
Stack: local variables
no. these are implementation details and vary from compiler to
compiler. This applies to all your answers to questions like this.

Data Segment: No clue (haven't done 16-bit x86 asm in a *long* time)
<snip>
* * void main()

A return type of void works for main();
works? In what sense does it "work"?
however, a non-int return type
for main is *not* recommended as [it] can potentially cause
segmentation faults and other problems.
you call segmentaion faults "working"? I hope
you aren't programming anything I rely on...

just curious, on which compiler? It is not recomended
because *it is non-standard*.

<snip>
* * * * int *k = (void *)malloc(1);
<snip>
Furthermore, I think you meant to type:
* * * * * int *k = (int *)malloc(1 * sizeof(int));
I hope not. He *meant* to type

int *k = malloc (sizeof (int));

or
int *k = malloc (sizeof *k);
Big difference, for the following reasons:
1) malloc()'s return type is void *, making (void *)malloc()
redundant.
* *I think you meant (int *)malloc() so that malloc() returns int *.
nope. (void*) is automatically converted (int*)

2) malloc()'s argument is the size of memory *in bytes* to allocate.
malloc(1) allocates 1 byte, malloc(1 * sizeof(int)) allocates memory
for 1 int; the size in bytes allocated being sizeof(int).
I don't see what the "1" buys you.
Do *not* do
sizeof(void) *at all*, as this is undefined (for good reasons).
ie. it makes no sense.

* *You could also use (element type *)calloc(number of elements,
sizeof(element type)).
note: calloc() zeros the memory. This may not be what you want.
calloc() cannot be relied on zero pointers or floating point types
correctly. Generally, I use malloc().

* *Or create a (preprocessor) macro like this:
* * * * #define mymalloc(n, type) (type *)malloc(n * sizeof(type))

* *and replace "int * i = (int *)malloc(n * sizeof(int))"
* *with "int * i = mymalloc(n, int);"
puke.

1. by convention macros are in upper case
2. heavy use of the preprocessor makes for obscure code.
3. the cast is unnecessary
4. you need more brackets in your macro

int *i = mymalloc (NORMAL_COUNT + SPECIAL_COUNT, int);

does what?

and isn't "type" a C++ reserved word?
:-)

>
* * }
2. how to find sizeof variable w/o using sizeof command?

sizeof is a compiler (not preprocessor) macro.
no, it isn't a macro.
During compilation, the
compiler replaces sizeof(foo) with a number equal to the number of
bytes 1 foo takes up in memory. This is why malloc() and realloc()
*must* have "* sizeof(type)" in the size argument for them to perform
as expected.

Example (assume sizeof(int) == 2):

* * int * i = (int *)malloc(sizeof(int));

is equal to
* * int * i = (int *)malloc(2);
3. what is the o/p of this prg?
* * void main()
* * {
* * * * int x = 5;
* * * * x = x++;
* * * * printf("x=%i", x);
* * }

x = x++ is undefined, but judging on how ++ works, I think the program
expands it to something like this (asm in comments):
the point is the behaviour is not defined by the standard.
So the compiler writer can do what he damn well pleases.

<snip>
--
Nick Keighley

The Dinosaurs have come and gone, we Theriodonts remain
Jan 16 '08 #46
On 16 Jan, 06:05, "Ravishankar S" <ravishanka...@in.bosch.comwrote:
"Walter Roberson" <rober...@ibd.nrc-cnrc.gc.cawrote in message
news:fm**********@canopus.cc.umanitoba.ca...
In article <fmk2or$u5...@news4.fe.internet.bosch.com>,
Ravishankar S <ravishanka...@in.bosch.comwrote:
>2. how to find sizeof variable w/o using sizeof command?
>I had seen a post which used pointer arithmetic. Its subject to
>correction by the experts:
>sizeof(i) *== *((&i) + 1) - (&i))
>Perhaps useful for your assignment
The result of that would always be 1, for any variable of any type.
You missed a couple of casts.

oh yes. ((void*)((&i) + 1) - (void*)(&i)).but arithmetic on void pointers is
not defined i guess.
correct. Try casting to (unsigned char*)
I havn't checked your expression

--
Nick Keighley

Jan 16 '08 #47
In article <47*****************@news.xs4all.nl>, Richard Bos
<rl*@hoekstra-uitgeverij.nlwrites
>Hans Schneider <ha**@localhost.localdomainwrote:
>Jack Klein schrieb:
1. Immediately drop the class.

I can't!
It is required for an advanced class.
2. Complain to the school administration about the unqualified
instructor and erroneous material.

I don't think that's gonna work.
He is a cousin of the principal.

Get the hell out of there. Now. This is a _very_ bad sign, and the code
is even worse. Your diploma will be worthless.
No.. It may get him a job where he can learn the right way. On the other
hand there may not be anywhere else that is practical to go to.

That said if there is an opportunity to move and the other place is
better (not run by a brother of the cousin :-) I would say move as well.
..
--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ ch***@phaedsys.org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Jan 16 '08 #48
In article <fm**********@aioe.org>, Philip Potter <pg*@doc.ic.ac.uk>
writes
>Chris Hills wrote:
>The point being that in a real rugby match if you argue with the
referee as a player you tend to get sent off. The Ref is God.

Yes, but rugby doesn't exist outside of the stadium. C does exist
outside of the OP's university, and his lecturer sure isn't God as far
as C is concerned, even if he is as far as the university is concerned.
Getting caned by a university is the end in some places. Some parts
of the world fighting authority will not get you any job. (With or
without the degree) Burma, China etc
>I suspect that if you argue with a lecturer who is only there because
his cousin is the principal you ain't going to win (or get your
qualification as you will probably fail a crucial exam) and that
isn't in the C standard either.

I wasn't suggesting arguing with the lecturer; I was suggesting seeking
advice and raising matters with the University administration.
If he is related to the principal it may be one and the same. If the
lecturer is as bad as the source code indicates I don't think he got the
job on technical merit.... Its not quite the same as a UK university.
If the principal is abusing his position to keep his incompetent
relation in a job, then perhaps the student press could get involved
too. If this really is the case, it's scandalous.
Not everywhere works the way you might like it too. In some cultures you
don't argue with the administration and win. See Burma the whole
religious and legal professions got involved and lost.

I have argued with the lecturer in the UK but at the time I was a
mature student with a lot of relevant experience. Even so it was not as
simple as you might think. There was a lot of "compromise" to save face
for the collage.
>>. There is more support available to you at a university than you
realise. Seek advice from your student's union, or your tutor.
What Student Union? It depends where this university is. Besides as
in all gambling the House Wins
So you think universities are similar to casinos?
Universities and life in general. Even in court it depends on the mix
of your lawyer, their lawyer and the judge. Being right does not always
have any bearing on the result.

Some people just can't hack university life, the way the course is
run/taught etc whereas they would have done very well at a different
university, course, syllabus or lecturer. Life is full of variables.

As you point out you can pass a degree without actually learning
anything if you just give the answer the lecturer wants even if it is
technically bot the best answer or actually as per the source code we
have seen not "conforming" even if it does work on the compiler used.
>>You have to ask yourself: Why am I doing this class? Is it just to
get letters after my name, and a shiny certificate?
In some cultures that is what matters. This is why they turn up at
interviews with the certificates. I have seen some students complain
that the degree certificate did not look prestigious enough to put
before an employer. This was important in the country they were
going back to.
>>Or am I doing it to actually learn something? If I want to learn
something when my instructor is teaching me falsehoods, how do I go
about doing that?
Without failing the course..... you have to pass the course to get
the qualification to get a job to eat to live.

It depends on whether the qualification is worth the paper it's written
on.
However in some parts of the world no degree no interview. Therefore no
job. It is not as bad in the UK but you still see adverts for 2.1 and
1sts only not just "a degree" and many do ask for a degree. It is just
that a lot of places don't
>Getting a degree doesn't guarantee a job,
Absolutely but it can get you to the start line. No degree you don't
even get there in some cultures and parts of the world.
>and not getting a degree doesn't guarantee unemployment.
Not always true in some places, at least not in the filed in question
(anyone can flip burgers) . Some parts of the world you fail or not
complete you don't even get the interview.

The point is the students may not be in a position to argue with the
lecture that the course is hopelessly wrong and win. They may not have
the luxury of changing universities or finding work without a degree.

Besides if all the software people there are taught the same and it
works in the local developments environments no one is going to employ
a disruptive student who failed the degree... no job no money no food.
(Not everywhere has social services.)


--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ ch***@phaedsys.org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Jan 16 '08 #49
andreyvul <an********@gmail.comwrites:
On Jan 15, 9:57 pm, Hans Schneider <h...@localhost.localdomainwrote:
<snip>
>Is x++ not the same as x=x+1?
Only when x++ is defined, such as
((int *)a)[x++] = y;
a[x++] = b[y++];
x++;
I don't know how these relate to the question ("is x++ not the same as
x=x+1?") but it seems worth noting that the first two of these are not
always well-defined. It might make an interesting (but advanced)
exercise to show a code fragment in which these first two have
undefined behaviour.

For extra credit, do the same for the third one! (This is unrelated to
the current issue).

PS. This is not really an exercise. I am just pointing out how
careful one must be about sequence points.

--
Ben.
Jan 16 '08 #50

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

Similar topics

0
by: adsheehan | last post by:
Hi, I am embedding Python into a multi-threaded C++ application runnig on Solaris and need urgent clarification on the embedding architecture and its correct usage (as I am experience weird...
12
by: Vibhajha | last post by:
Hi friends, My sister is in great problem , she has this exam of C++ and she is stuck up with some questions, if friends like this group provides some help to her, she will be very grateful....
3
by: Rob | last post by:
I have a form - when you click the submit button, it appends a variable to the URL (e.g. xyz.cgi?inputID=some_dynamic_variable) It also opens a new page. Now, that some_dynamic_variable is...
8
by: Mike | last post by:
Hello, I have a few rather urgent questions that I hope someone can help with (I need to figure this out prior to a meeting tomorrow.) First, a bit of background: The company I work for is...
2
by: Max Power | last post by:
Hi All I am coding a small app in that swaps specific files between a client and server. All files and locations are set at both sides. I want my app to show a file list, based on the file...
2
by: Gurmeet | last post by:
Hi All, Does any one ever appeared for Brainbench ceritification in C++? I need to know what kind of question are there in C++. Now brainbench is paid. I cant give or get sample or practice C++...
3
by: N. Spiker | last post by:
I am attempting to receive a single TCP packet with some text ending with carriage return and line feed characters. When the text is send and the packet has the urgent flag set, the text read from...
1
by: TexCube | last post by:
Hello everyone, I have an urgent question. Can data be pulled from an access database table to create a directory that can be printed out and bound together? I need to have each row of a...
1
by: rajesh.us.it.recruiter | last post by:
Hi Guys/Partners, We have a URGENT Requirement for Perl Programmer with H1 visa in India/US for one of our prestigious Client. Location : New Jersey Requirements : Should be very strong in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.