* Re: Division in loop
2009-09-08 14:40 Division in loop Randi Botse
@ 2009-09-08 14:47 ` Saurabh Sehgal
2009-09-08 14:57 ` Bert Wesarg
` (2 subsequent siblings)
3 siblings, 0 replies; 16+ messages in thread
From: Saurabh Sehgal @ 2009-09-08 14:47 UTC (permalink / raw)
To: Randi Botse; +Cc: linux-c-programming
Where do you initialize the variable size and to what value ?
On Tue, Sep 8, 2009 at 10:40 AM, Randi Botse <nightdecoder@gmail.com> wrote:
>
> hi all,
>
> ....
> int fd, fdp;
> unsigned int size;
> int progress = 0;
> size_t bytes = 0;
> void *buffer;
> ....
>
> /* open fd from a device then create fdp, allocate buffer,, etc. */
>
> while (bytes < size) {
> ret = read(fd, buffer + bytes, size - bytes);
> if (ret == -1) {
> perror("read");
> return -1;
> }
> ret = write(fdp, buffer + bytes, ret);
> if (ret == -1) {
> perror("write");
> return -1;
> }
> bytes += ret;
>
> progress = (int) ((bytes / size) * 100);
> printf("\rcompleted: %i%%", progress);
> fflush(stdout);
> }
>
> ....
>
> I run this loop to receive some data, the printf() seem not updated,
> the progress output is
> always in it's initial value until the loop was finished. but when i do...
>
> progress = (int) ((bytes / 2) * 100); /* change size to 2 (or random number) */
>
> then it's works... what's wrong here???
> --
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
--
Saurabh Sehgal
E-mail: saurabh.r.s@gmail.com
Phone: 647-831-5621
LinkedIn: http://www.linkedin.com/pub/1/7a3/436
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 16+ messages in thread* Re: Division in loop
2009-09-08 14:40 Division in loop Randi Botse
2009-09-08 14:47 ` Saurabh Sehgal
@ 2009-09-08 14:57 ` Bert Wesarg
2009-09-08 14:58 ` Tim Walberg
2009-09-09 0:45 ` Zhenwen Xu
3 siblings, 0 replies; 16+ messages in thread
From: Bert Wesarg @ 2009-09-08 14:57 UTC (permalink / raw)
To: Randi Botse; +Cc: linux-c-programming
On Tue, Sep 8, 2009 at 16:40, Randi Botse<nightdecoder@gmail.com> wrote:
> hi all,
Hi,
>
> while (bytes < size) {
:
>
> progress = (int) ((bytes / size) * 100);
This is an integer division, and because bytes is always smaller than
size will result in 0. You should cast bytes to double, or multiply by
100 first and than divide by size.
Bert
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 16+ messages in thread* Re: Division in loop
2009-09-08 14:40 Division in loop Randi Botse
2009-09-08 14:47 ` Saurabh Sehgal
2009-09-08 14:57 ` Bert Wesarg
@ 2009-09-08 14:58 ` Tim Walberg
2009-09-08 15:09 ` Randi Botse
2009-09-09 0:49 ` Glynn Clements
2009-09-09 0:45 ` Zhenwen Xu
3 siblings, 2 replies; 16+ messages in thread
From: Tim Walberg @ 2009-09-08 14:58 UTC (permalink / raw)
To: Randi Botse; +Cc: linux-c-programming
What are the typical values of bytes and size? Suspect
"(bytes / size)", being integer division, probably truncates
to 0 if size is relatively large...
Maybe you want "(bytes * 100) / size"? Or as another
alternative (arithmetically approximately equivalent,
but not quite as clear, however, it saves on multiply
operations):
size = size / 100;
...
...
...
printf(... bytes / size);
On 09/08/2009 21:40 +0700, Randi Botse wrote:
>> hi all,
>>
>> ....
>> int fd, fdp;
>> unsigned int size;
>> int progress = 0;
>> size_t bytes = 0;
>> void *buffer;
>> ....
>>
>> /* open fd from a device then create fdp, allocate buffer,, etc. */
>>
>> while (bytes < size) {
>> ret = read(fd, buffer + bytes, size - bytes);
>> if (ret == -1) {
>> perror("read");
>> return -1;
>> }
>> ret = write(fdp, buffer + bytes, ret);
>> if (ret == -1) {
>> perror("write");
>> return -1;
>> }
>> bytes += ret;
>>
>> progress = (int) ((bytes / size) * 100);
>> printf("\rcompleted: %i%%", progress);
>> fflush(stdout);
>> }
>>
>> ....
>>
>> I run this loop to receive some data, the printf() seem not updated,
>> the progress output is
>> always in it's initial value until the loop was finished. but when i do...
>>
>> progress = (int) ((bytes / 2) * 100); /* change size to 2 (or random number) */
>>
>> then it's works... what's wrong here???
>> --
>> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
>> the body of a message to majordomo@vger.kernel.org
>> More majordomo info at http://vger.kernel.org/majordomo-info.html
End of included message
--
+----------------------+
| Tim Walberg |
| 830 Carriage Dr. |
| Algonquin, IL 60102 |
| twalberg@comcast.net |
+----------------------+
^ permalink raw reply [flat|nested] 16+ messages in thread* Re: Division in loop
2009-09-08 14:58 ` Tim Walberg
@ 2009-09-08 15:09 ` Randi Botse
2009-09-09 0:49 ` Glynn Clements
1 sibling, 0 replies; 16+ messages in thread
From: Randi Botse @ 2009-09-08 15:09 UTC (permalink / raw)
To: Tim Walberg; +Cc: linux-c-programming
Hi all,,,
i was too frustrate too ask this problem, actually this is percentage
output of received data, yes integer division ALWAYS have (bytes /
size) equal to 0, until bytes >= size, sorry all... :),.. my fool...
THANKS SO MUCH!!!
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: Division in loop
2009-09-08 14:58 ` Tim Walberg
2009-09-08 15:09 ` Randi Botse
@ 2009-09-09 0:49 ` Glynn Clements
2009-09-09 2:37 ` Randi Botse
1 sibling, 1 reply; 16+ messages in thread
From: Glynn Clements @ 2009-09-09 0:49 UTC (permalink / raw)
To: Tim Walberg; +Cc: Randi Botse, linux-c-programming
Tim Walberg wrote:
> Maybe you want "(bytes * 100) / size"?
However, if int and size_t are 32-bit, that will overflow at around
20 or 40 MB (20 for signed, 40 for unsigned), which isn't really all
that much. Converting to either double or long long might be wise,
i.e.
percent = (int)(bytes * 100.0) / size;
or:
percent = (int)(bytes * 100LL) / size;
--
Glynn Clements <glynn@gclements.plus.com>
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: Division in loop
2009-09-09 0:49 ` Glynn Clements
@ 2009-09-09 2:37 ` Randi Botse
2009-09-09 2:39 ` Randi Botse
0 siblings, 1 reply; 16+ messages in thread
From: Randi Botse @ 2009-09-09 2:37 UTC (permalink / raw)
To: Glynn Clements; +Cc: linux-c-programming
Hi again...
int fd; /* RS232 serial link */
int fdp; /* file descriptor for a regular file */
struct stat stat;
size_t bytes = 0;
void *buffer; /* buffer, will be allocated */
...
while (bytes < stat.st_size) {
ret = read(fdp, buffer + bytes, stat.st_size - bytes);
if (ret == -1) {
perror("read");
return -1;
}
ret = write(fd, buffer + bytes, ret);
if (ret == -1) {
perror("write");
return -1;
}
bytes += ret;
progress = (int) (bytes * 100) / stat.st_size;
printf("\rcompleted: %i%%", progress);
fflush(stdout);
}
...
Then i see printf() never output the message until the loop has ended...
'fdp' is a file descriptor to a local file, i send it through a serial
connection (RS232),
i use a serial connection so i can practice Linux's File I/O operation
in real world ;p
the receiver can output the progress's message, but the sender (the
above code) could not...
this is the receiver codes:
...
while (bytes < size) {
ret = read(fd, buffer + bytes, size - bytes);
if (ret == -1) {
perror("read");
return -1;
}
ret = write(fdp, buffer + bytes, ret);
if (ret == -1) {
perror("write");
return -1;
}
bytes += ret;
progress = (int) (bytes * 100) / size;
printf("\rcompleted: %i%%", progress); /* THIS NEVER OUTPUT,
UNTIL LOOP END! */
fflush(stdout);
}
...
On Tue, Sep 8, 2009 at 8:49 PM, Glynn Clements<glynn@gclements.plus.com> wrote:
>
> Tim Walberg wrote:
>
>> Maybe you want "(bytes * 100) / size"?
>
> However, if int and size_t are 32-bit, that will overflow at around
> 20 or 40 MB (20 for signed, 40 for unsigned), which isn't really all
> that much. Converting to either double or long long might be wise,
> i.e.
>
> percent = (int)(bytes * 100.0) / size;
> or:
> percent = (int)(bytes * 100LL) / size;
>
> --
> Glynn Clements <glynn@gclements.plus.com>
>
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 16+ messages in thread* Re: Division in loop
2009-09-09 2:37 ` Randi Botse
@ 2009-09-09 2:39 ` Randi Botse
2009-09-09 2:52 ` Eric Bambach
` (2 more replies)
0 siblings, 3 replies; 16+ messages in thread
From: Randi Botse @ 2009-09-09 2:39 UTC (permalink / raw)
To: Glynn Clements; +Cc: linux-c-programming
Hi again...
int fd; /* RS232 serial link */
int fdp; /* file descriptor for a regular file */
struct stat stat;
size_t bytes = 0;
void *buffer; /* buffer, will be allocated */
...
while (bytes < stat.st_size) {
ret = read(fdp, buffer + bytes, stat.st_size - bytes);
if (ret == -1) {
perror("read");
return -1;
}
ret = write(fd, buffer + bytes, ret);
if (ret == -1) {
perror("write");
return -1;
}
bytes += ret;
progress = (int) (bytes * 100) / stat.st_size;
printf("\rcompleted: %i%%", progress); /* NO OUTPUT, UNTIL LOOP ENDED */
fflush(stdout);
}
...
Then i see printf() never output the message until the loop has ended...
'fdp' is a file descriptor to a local file, i send it through a serial
connection (RS232),
i use a serial connection so i can practice Linux's File I/O operation
in real world ;p
the receiver can output the progress's message, but the sender (the
above code) could not...
this is the receiver codes:
...
while (bytes < size) {
ret = read(fd, buffer + bytes, size - bytes);
if (ret == -1) {
perror("read");
return -1;
}
ret = write(fdp, buffer + bytes, ret);
if (ret == -1) {
perror("write");
return -1;
}
bytes += ret;
progress = (int) (bytes * 100) / size;
printf("\rcompleted: %i%%", progress); /* HAS OUTPUT */
fflush(stdout);
}
...
On Tue, Sep 8, 2009 at 8:49 PM, Glynn Clements<glynn@gclements.plus.com> wrote:
>
> Tim Walberg wrote:
>
>> Maybe you want "(bytes * 100) / size"?
>
> However, if int and size_t are 32-bit, that will overflow at around
> 20 or 40 MB (20 for signed, 40 for unsigned), which isn't really all
> that much. Converting to either double or long long might be wise,
> i.e.
>
> percent = (int)(bytes * 100.0) / size;
> or:
> percent = (int)(bytes * 100LL) / size;
>
> --
> Glynn Clements <glynn@gclements.plus.com>
>
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 16+ messages in thread* Re: Division in loop
2009-09-09 2:39 ` Randi Botse
@ 2009-09-09 2:52 ` Eric Bambach
2009-09-09 2:57 ` Eric Bambach
2009-09-10 0:46 ` Glynn Clements
2 siblings, 0 replies; 16+ messages in thread
From: Eric Bambach @ 2009-09-09 2:52 UTC (permalink / raw)
To: Randi Botse; +Cc: linux-c-programming
On Tuesday 08 September 2009 09:39:40 pm you wrote:
> Hi again...
>
> int fd; /* RS232 serial link */
> int fdp; /* file descriptor for a regular file */
> struct stat stat;
> size_t bytes = 0;
> void *buffer; /* buffer, will be allocated */
> ...
> while (bytes < stat.st_size) {
> ret = read(fdp, buffer + bytes, stat.st_size - bytes);
> if (ret == -1) {
> perror("read");
> return -1;
> }
> ret = write(fd, buffer + bytes, ret);
> if (ret == -1) {
> perror("write");
> return -1;
> }
> bytes += ret;
> progress = (int) (bytes * 100) / stat.st_size;
> printf("\rcompleted: %i%%", progress); /* NO OUTPUT, UNTIL LOOP
> ENDED */ fflush(stdout);
> }
> ...
>
> Then i see printf() never output the message until the loop has ended...
> 'fdp' is a file descriptor to a local file, i send it through a serial
> connection (RS232),
> i use a serial connection so i can practice Linux's File I/O operation
> in real world ;p
>
> the receiver can output the progress's message, but the sender (the
> above code) could not...
> this is the receiver codes:
>
> ...
> while (bytes < size) {
> ret = read(fd, buffer + bytes, size - bytes);
> if (ret == -1) {
> perror("read");
> return -1;
> }
> ret = write(fdp, buffer + bytes, ret);
> if (ret == -1) {
> perror("write");
> return -1;
> }
> bytes += ret;
>
> progress = (int) (bytes * 100) / size;
> printf("\rcompleted: %i%%", progress); /* HAS OUTPUT */
> fflush(stdout);
> }
> ...
>
> On Tue, Sep 8, 2009 at 8:49 PM, Glynn Clements<glynn@gclements.plus.com>
wrote:
> > Tim Walberg wrote:
> >> Maybe you want "(bytes * 100) / size"?
> >
> > However, if int and size_t are 32-bit, that will overflow at around
> > 20 or 40 MB (20 for signed, 40 for unsigned), which isn't really all
> > that much. Converting to either double or long long might be wise,
> > i.e.
> >
> > percent = (int)(bytes * 100.0) / size;
> > or:
> > percent = (int)(bytes * 100LL) / size;
> >
> > --
> > Glynn Clements <glynn@gclements.plus.com>
>
> --
> To unsubscribe from this list: send the line "unsubscribe
> linux-c-programming" in the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
Your printf statements are being buffered by the system. If enough of them
accumulate you will see them being printed "inside the loop". You need to
disable buffering or print to STDERR.
^ permalink raw reply [flat|nested] 16+ messages in thread* Re: Division in loop
2009-09-09 2:39 ` Randi Botse
2009-09-09 2:52 ` Eric Bambach
@ 2009-09-09 2:57 ` Eric Bambach
2009-09-09 10:12 ` Randi Botse
2009-09-10 0:46 ` Glynn Clements
2 siblings, 1 reply; 16+ messages in thread
From: Eric Bambach @ 2009-09-09 2:57 UTC (permalink / raw)
To: Randi Botse; +Cc: linux-c-programming
On Tuesday 08 September 2009 09:39:40 pm you wrote:
> Hi again...
>
> int fd; /* RS232 serial link */
> int fdp; /* file descriptor for a regular file */
> struct stat stat;
> size_t bytes = 0;
> void *buffer; /* buffer, will be allocated */
> ...
> while (bytes < stat.st_size) {
> ret = read(fdp, buffer + bytes, stat.st_size - bytes);
> if (ret == -1) {
> perror("read");
> return -1;
> }
> ret = write(fd, buffer + bytes, ret);
> if (ret == -1) {
> perror("write");
> return -1;
> }
> bytes += ret;
> progress = (int) (bytes * 100) / stat.st_size;
> printf("\rcompleted: %i%%", progress); /* NO OUTPUT, UNTIL LOOP
> ENDED */ fflush(stdout);
> }
> ...
>
> Then i see printf() never output the message until the loop has ended...
> 'fdp' is a file descriptor to a local file, i send it through a serial
> connection (RS232),
> i use a serial connection so i can practice Linux's File I/O operation
> in real world ;p
>
> the receiver can output the progress's message, but the sender (the
> above code) could not...
> this is the receiver codes:
>
> ...
> while (bytes < size) {
> ret = read(fd, buffer + bytes, size - bytes);
> if (ret == -1) {
> perror("read");
> return -1;
> }
> ret = write(fdp, buffer + bytes, ret);
> if (ret == -1) {
> perror("write");
> return -1;
> }
> bytes += ret;
>
> progress = (int) (bytes * 100) / size;
> printf("\rcompleted: %i%%", progress); /* HAS OUTPUT */
> fflush(stdout);
> }
> ...
>
> On Tue, Sep 8, 2009 at 8:49 PM, Glynn Clements<glynn@gclements.plus.com>
wrote:
> > Tim Walberg wrote:
> >> Maybe you want "(bytes * 100) / size"?
> >
> > However, if int and size_t are 32-bit, that will overflow at around
> > 20 or 40 MB (20 for signed, 40 for unsigned), which isn't really all
> > that much. Converting to either double or long long might be wise,
> > i.e.
> >
> > percent = (int)(bytes * 100.0) / size;
> > or:
> > percent = (int)(bytes * 100LL) / size;
> >
> > --
> > Glynn Clements <glynn@gclements.plus.com>
>
> --
> To unsubscribe from this list: send the line "unsubscribe
> linux-c-programming" in the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
Wow. I feel silly. Ignore my previous message. It's late here and I missed the
fflush().
^ permalink raw reply [flat|nested] 16+ messages in thread* Re: Division in loop
2009-09-09 2:39 ` Randi Botse
2009-09-09 2:52 ` Eric Bambach
2009-09-09 2:57 ` Eric Bambach
@ 2009-09-10 0:46 ` Glynn Clements
2009-09-10 7:01 ` Randi Botse
2 siblings, 1 reply; 16+ messages in thread
From: Glynn Clements @ 2009-09-10 0:46 UTC (permalink / raw)
To: Randi Botse; +Cc: linux-c-programming
Randi Botse wrote:
> void *buffer; /* buffer, will be allocated */
> ...
> ret = read(fdp, buffer + bytes, stat.st_size - bytes);
Pointer arithmetic on a void* is undefined. gcc allows this as an
extension, treating void* like char*, i.e. p+i is treated as
(void*)((char*)p+i).
For portability, define buffer as a char*.
> printf("\rcompleted: %i%%", progress); /* NO OUTPUT, UNTIL LOOP ENDED */
> fflush(stdout);
> Then i see printf() never output the message until the loop has ended...
> 'fdp' is a file descriptor to a local file, i send it through a serial
> connection (RS232),
Try checking the return value of printf() and fflush(), and errno upon
error.
Also, what is stdout connected to? A VT? An xterm? sshd?
--
Glynn Clements <glynn@gclements.plus.com>
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 16+ messages in thread* Re: Division in loop
2009-09-10 0:46 ` Glynn Clements
@ 2009-09-10 7:01 ` Randi Botse
2009-09-10 7:03 ` Manish Katiyar
2009-09-11 7:26 ` Glynn Clements
0 siblings, 2 replies; 16+ messages in thread
From: Randi Botse @ 2009-09-10 7:01 UTC (permalink / raw)
To: Glynn Clements; +Cc: linux-c-programming
Thanks Glynn for let me know that the void* pointer arithmetic is
undefined, and gcc automatically cast it to char* pointer... is this
because void* doesn't have a size? im curious when i do sizeof(void*),
my machine tell me sizeof(void*) is 4kb... i thnk when i do (void* +
1) that's means "4 kb after void*"?....
i run the code in GNOME terminal, that's means stdout connected to xterm?
On Thu, Sep 10, 2009 at 7:46 AM, Glynn Clements
<glynn@gclements.plus.com> wrote:
>
> Randi Botse wrote:
>
>> void *buffer; /* buffer, will be allocated */
>> ...
>
>> ret = read(fdp, buffer + bytes, stat.st_size - bytes);
>
> Pointer arithmetic on a void* is undefined. gcc allows this as an
> extension, treating void* like char*, i.e. p+i is treated as
> (void*)((char*)p+i).
>
> For portability, define buffer as a char*.
>
>> printf("\rcompleted: %i%%", progress); /* NO OUTPUT, UNTIL LOOP ENDED */
>> fflush(stdout);
>
>> Then i see printf() never output the message until the loop has ended...
>> 'fdp' is a file descriptor to a local file, i send it through a serial
>> connection (RS232),
>
> Try checking the return value of printf() and fflush(), and errno upon
> error.
>
> Also, what is stdout connected to? A VT? An xterm? sshd?
>
> --
> Glynn Clements <glynn@gclements.plus.com>
>
^ permalink raw reply [flat|nested] 16+ messages in thread* Re: Division in loop
2009-09-10 7:01 ` Randi Botse
@ 2009-09-10 7:03 ` Manish Katiyar
2009-09-11 7:26 ` Glynn Clements
1 sibling, 0 replies; 16+ messages in thread
From: Manish Katiyar @ 2009-09-10 7:03 UTC (permalink / raw)
To: Randi Botse; +Cc: Glynn Clements, linux-c-programming
On Thu, Sep 10, 2009 at 12:31 PM, Randi Botse <nightdecoder@gmail.com> wrote:
> Thanks Glynn for let me know that the void* pointer arithmetic is
> undefined, and gcc automatically cast it to char* pointer... is this
> because void* doesn't have a size?
void * or any pointer for that matter will have a size fixed based on
your platform ie... 4 for x86 and 8 for x86_64.
> im curious when i do sizeof(void*),
> my machine tell me sizeof(void*) is 4kb
That is 4 bytes and not 4KB.
> ... i thnk when i do (void* +
> 1) that's means "4 kb after void*"?....
>
> i run the code in GNOME terminal, that's means stdout connected to xterm?
>
>
> On Thu, Sep 10, 2009 at 7:46 AM, Glynn Clements
> <glynn@gclements.plus.com> wrote:
>>
>> Randi Botse wrote:
>>
>>> void *buffer; /* buffer, will be allocated */
>>> ...
>>
>>> ret = read(fdp, buffer + bytes, stat.st_size - bytes);
>>
>> Pointer arithmetic on a void* is undefined. gcc allows this as an
>> extension, treating void* like char*, i.e. p+i is treated as
>> (void*)((char*)p+i).
>>
>> For portability, define buffer as a char*.
>>
>>> printf("\rcompleted: %i%%", progress); /* NO OUTPUT, UNTIL LOOP ENDED */
>>> fflush(stdout);
>>
>>> Then i see printf() never output the message until the loop has ended...
>>> 'fdp' is a file descriptor to a local file, i send it through a serial
>>> connection (RS232),
>>
>> Try checking the return value of printf() and fflush(), and errno upon
>> error.
>>
>> Also, what is stdout connected to? A VT? An xterm? sshd?
>>
>> --
>> Glynn Clements <glynn@gclements.plus.com>
>>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>
--
Thanks -
Manish
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 16+ messages in thread* Re: Division in loop
2009-09-10 7:01 ` Randi Botse
2009-09-10 7:03 ` Manish Katiyar
@ 2009-09-11 7:26 ` Glynn Clements
1 sibling, 0 replies; 16+ messages in thread
From: Glynn Clements @ 2009-09-11 7:26 UTC (permalink / raw)
To: Randi Botse; +Cc: linux-c-programming
Randi Botse wrote:
> Thanks Glynn for let me know that the void* pointer arithmetic is
> undefined, and gcc automatically cast it to char* pointer... is this
> because void* doesn't have a size?
Technically, it's because the standard says that it's undefined.
Pointer arithmetic is defined in terms of pointers to array elements,
e.g. if you have:
int foo[N];
int *p = &foo[A];
int *q = p+B;
int *r = &foo[A+B];
then r==q. But you can't have an array of "void", so pointer
arithmetic on void* isn't meaningful.
> im curious when i do sizeof(void*),
> my machine tell me sizeof(void*) is 4kb... i thnk when i do (void* +
> 1) that's means "4 kb after void*"?....
The result of sizeof is in bytes (technically, "char"s; sizeof(char)
is guaranteed to be 1).
The result of "sizeof" applied to any pointer type will typically be 4
bytes on 32-bit systems or 8 bytes on 64-bit systems. Although it's
possible for pointers to different types to have different sizes, it's
rare.
> i run the code in GNOME terminal, that's means stdout connected to xterm?
No, to Gnome terminal; xterm is similar.
It's possible that the behaviour which you experience is a property of
the terminal emulator. You might try running the program on a
different terminal emulator, or on a VT, to see if the same problem
occurs there.
--
Glynn Clements <glynn@gclements.plus.com>
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: Division in loop
2009-09-08 14:40 Division in loop Randi Botse
` (2 preceding siblings ...)
2009-09-08 14:58 ` Tim Walberg
@ 2009-09-09 0:45 ` Zhenwen Xu
3 siblings, 0 replies; 16+ messages in thread
From: Zhenwen Xu @ 2009-09-09 0:45 UTC (permalink / raw)
To: Randi Botse; +Cc: linux-c-programming
On Tue, Sep 08, 2009 at 09:40:30PM +0700, Randi Botse wrote:
> hi all,
>
> ....
> int fd, fdp;
> unsigned int size;
> int progress = 0;
> size_t bytes = 0;
> void *buffer;
> ....
>
> /* open fd from a device then create fdp, allocate buffer,, etc. */
>
> while (bytes < size) {
> ret = read(fd, buffer + bytes, size - bytes);
> if (ret == -1) {
> perror("read");
> return -1;
> }
> ret = write(fdp, buffer + bytes, ret);
> if (ret == -1) {
> perror("write");
> return -1;
> }
> bytes += ret;
>
> progress = (int) ((bytes / size) * 100);
> printf("\rcompleted: %i%%", progress);
printf them all and check them:
printf("\rcompleted: %i%% bytes: %d size: %d \n", progress, bytes, size);
> fflush(stdout);
> }
>
> ....
>
> I run this loop to receive some data, the printf() seem not updated,
> the progress output is
> always in it's initial value until the loop was finished. but when i do...
>
> progress = (int) ((bytes / 2) * 100); /* change size to 2 (or random number) */
>
> then it's works... what's wrong here???
> --
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
--
--------------------------------
http://zhwen.org - Open and Free
^ permalink raw reply [flat|nested] 16+ messages in thread