dynamic mem alloc q

Discussion in 'C Programming' started by Joe Smith, May 9, 2006.

  1. Joe Smith

    Joe Smith Guest

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>


    #define BUF_MAX 80

    int main (void) {
    char buf[BUF_MAX];
    unsigned long l;
    size_t s;
    printf("give me a number less than a third billion: \n");
    while(fgets(buf, BUF_MAX, stdin) != NULL) {
    if ((s = strspn(buf, "0123456789 \t\n")) == strlen(buf)) {
    errno = 0;
    l = strtoul(buf, NULL, 10);
    if (errno == ERANGE) {
    printf("number out of range!\n");
    } else {

    /* add code here */

    }
    } else {
    while(s--)
    putchar(' ');
    putchar('^');
    printf(" invalid character\n");
    }
    }

    return 0;
    }

    /* int *dynarray;
    dynarray = malloc(l * sizeof(long)); */
    /* end source */

    I seek to dynamically allocate memory of an array of unsigned longs with
    array size determined from an unsigned long from the keyboard. With program
    control as it exists now, it would seem that I would get the business done
    where I have marked
    /* add code here */
    Do I need to redesign program control in order not to have a leak the size
    of Texas? Joe
     
    Joe Smith, May 9, 2006
    #1
    1. Advertising

  2. Joe Smith

    Michael Mair Guest

    Joe Smith schrieb:
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <string.h>
    > #include <errno.h>
    >
    >
    > #define BUF_MAX 80
    >
    > int main (void) {
    > char buf[BUF_MAX];
    > unsigned long l;
    > size_t s;
    > printf("give me a number less than a third billion: \n");
    > while(fgets(buf, BUF_MAX, stdin) != NULL) {
    > if ((s = strspn(buf, "0123456789 \t\n")) == strlen(buf)) {
    > errno = 0;
    > l = strtoul(buf, NULL, 10);
    > if (errno == ERANGE) {
    > printf("number out of range!\n");
    > } else {


    Note: The values of l indicating potential errors are 0
    and ULONG_MAX; as you want to allocate storage neither for 0
    nor for ULONG_MAX array elements (at least if by "billion" you
    mean 1e9), you can just test for >0 and <= 333333333UL (assuming
    you mean "a third of a" by "third") in the first place.
    Only if you have a failure there, I'd check using strspn() or
    repeat the strtoul() call preceded by errno = 0.
    This is more straightforward and entirely sufficient.

    >
    > /* add code here */
    >
    > }
    > } else {
    > while(s--)
    > putchar(' ');
    > putchar('^');
    > printf(" invalid character\n");
    > }
    > }
    >
    > return 0;
    > }
    >
    > /* int *dynarray;
    > dynarray = malloc(l * sizeof(long)); */
    > /* end source */
    >
    > I seek to dynamically allocate memory of an array of unsigned longs with
    > array size determined from an unsigned long from the keyboard. With program
    > control as it exists now, it would seem that I would get the business done
    > where I have marked
    > /* add code here */
    > Do I need to redesign program control in order not to have a leak the size
    > of Texas?


    Well, don't forget to free() allocated storage :)
    Apart from that, only forgetting to check against l != 0 can be a
    problem.

    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, May 9, 2006
    #2
    1. Advertising

  3. Joe Smith

    Joe Smith Guest

    "Michael Mair" penned:
    > Joe Smith schrieb:
    >> #include <stdio.h>
    >> #include <stdlib.h>
    >> #include <string.h>
    >> #include <errno.h>
    >>
    >>
    >> #define BUF_MAX 80
    >>
    >> int main (void) {
    >> char buf[BUF_MAX];
    >> unsigned long l;
    >> size_t s;
    >> printf("give me a number less than a third billion: \n");
    >> while(fgets(buf, BUF_MAX, stdin) != NULL) {
    >> if ((s = strspn(buf, "0123456789 \t\n")) == strlen(buf)) {
    >> errno = 0;
    >> l = strtoul(buf, NULL, 10);
    >> if (errno == ERANGE) {
    >> printf("number out of range!\n");
    >> } else {

    >
    > Note: The values of l indicating potential errors are 0
    > and ULONG_MAX; as you want to allocate storage neither for 0
    > nor for ULONG_MAX array elements (at least if by "billion" you
    > mean 1e9), you can just test for >0 and <= 333333333UL (assuming
    > you mean "a third of a" by "third") in the first place.
    > Only if you have a failure there, I'd check using strspn() or
    > repeat the strtoul() call preceded by errno = 0.
    > This is more straightforward and entirely sufficient.


    'm' and 'b' are both labials in english. 'onnen' and 'arden' in german
    don't penetrate the stupor of thought that a lot of persons who have who use
    these words, i.e. regarding largish numbers. I would have thought that a
    zero input would have succumbed to the ERANGE flag. It seeems to me,
    however, that you're hinting my sniped code needs a tune-up here.

    >>
    >> /* add code here */
    >>
    >> }
    >> } else {
    >> while(s--)
    >> putchar(' ');
    >> putchar('^');
    >> printf(" invalid character\n");
    >> }
    >> }
    >>
    >> return 0;
    >> }
    >>
    >> /* int *dynarray;
    >> dynarray = malloc(l * sizeof(long)); */
    >> /* end source */
    >>
    >> I seek to dynamically allocate memory of an array of unsigned longs with
    >> array size determined from an unsigned long from the keyboard. With
    >> program control as it exists now, it would seem that I would get the
    >> business done where I have marked
    >> /* add code here */
    >> Do I need to redesign program control in order not to have a leak the
    >> size of Texas?

    >
    > Well, don't forget to free() allocated storage :)
    > Apart from that, only forgetting to check against l != 0 can be a
    > problem.


    I'll give it hell tonight. Joe
     
    Joe Smith, May 10, 2006
    #3
  4. Joe Smith wrote:
    > "Michael Mair" penned:
    > > Joe Smith schrieb:
    > >> #include <stdio.h>
    > >> #include <stdlib.h>
    > >> #include <string.h>
    > >> #include <errno.h>
    > >>
    > >>
    > >> #define BUF_MAX 80
    > >>
    > >> int main (void) {
    > >> char buf[BUF_MAX];
    > >> unsigned long l;
    > >> size_t s;
    > >> printf("give me a number less than a third billion: \n");
    > >> while(fgets(buf, BUF_MAX, stdin) != NULL) {
    > >> if ((s = strspn(buf, "0123456789 \t\n")) == strlen(buf)) {
    > >> errno = 0;
    > >> l = strtoul(buf, NULL, 10);
    > >> if (errno == ERANGE) {
    > >> printf("number out of range!\n");
    > >> } else {

    > >
    > > Note: The values of l indicating potential errors are 0
    > > and ULONG_MAX; as you want to allocate storage neither for 0
    > > nor for ULONG_MAX array elements (at least if by "billion" you
    > > mean 1e9), you can just test for >0 and <= 333333333UL (assuming
    > > you mean "a third of a" by "third") in the first place.
    > > Only if you have a failure there, I'd check using strspn() or
    > > repeat the strtoul() call preceded by errno = 0.
    > > This is more straightforward and entirely sufficient.

    >
    > 'm' and 'b' are both labials in english. 'onnen' and 'arden' in german
    > don't penetrate the stupor of thought that a lot of persons who have who use
    > these words, i.e. regarding largish numbers. I would have thought that a
    > zero input would have succumbed to the ERANGE flag. It seeems to me,
    > however, that you're hinting my sniped code needs a tune-up here.


    Below is the appropriate caveat I listed with the code when I
    originally posted it:

    "If the input contains only of whitespace or the minus sign the
    converted value will be 0. "

    The minus sign part does not, of course, apply to your version. The
    word "contains" was supposed to be "consists".

    I have tried to impress upon you the fact that the code was written to
    meet a very specific need, what exactly are the needs *you* are trying
    to accomidate?

    Robert Gamble
     
    Robert Gamble, May 10, 2006
    #4
  5. Joe Smith

    Michael Mair Guest

    Joe Smith schrieb:
    > "Michael Mair" penned:
    >>Joe Smith schrieb:
    >>
    >>>#include <stdio.h>
    >>>#include <stdlib.h>
    >>>#include <string.h>
    >>>#include <errno.h>
    >>>
    >>>#define BUF_MAX 80
    >>>
    >>>int main (void) {
    >>> char buf[BUF_MAX];
    >>> unsigned long l;
    >>> size_t s;
    >>> printf("give me a number less than a third billion: \n");
    >>> while(fgets(buf, BUF_MAX, stdin) != NULL) {
    >>> if ((s = strspn(buf, "0123456789 \t\n")) == strlen(buf)) {
    >>> errno = 0;
    >>> l = strtoul(buf, NULL, 10);
    >>> if (errno == ERANGE) {
    >>> printf("number out of range!\n");
    >>> } else {

    >>
    >>Note: The values of l indicating potential errors are 0
    >>and ULONG_MAX; as you want to allocate storage neither for 0
    >>nor for ULONG_MAX array elements (at least if by "billion" you
    >>mean 1e9), you can just test for >0 and <= 333333333UL (assuming
    >>you mean "a third of a" by "third") in the first place.
    >>Only if you have a failure there, I'd check using strspn() or
    >>repeat the strtoul() call preceded by errno = 0.
    >>This is more straightforward and entirely sufficient.

    >
    > 'm' and 'b' are both labials in english. 'onnen' and 'arden' in german

    ITYM 'onen' and 'arden'.
    > don't penetrate the stupor of thought that a lot of persons who have who use
    > these words, i.e. regarding largish numbers.


    I am not sure what you are trying to achieve with these
    "witticisms" of yours: There are two possible meanings of "billion"
    in English, so the question was just to clarify whether you mean
    1e9 or 1e12.
    Especially in a newsgroup frequented by native speakers and
    non-native speakers alike, everyone should take care to express his
    or her intent clearly and concisely.


    > I would have thought that a
    > zero input would have succumbed to the ERANGE flag. It seeems to me,
    > however, that you're hinting my sniped code needs a tune-up here.


    Why don't you just read the documentation of strtoul()?
    It will tell you that 0 and ULONG_MAX are the only return values
    that _can_ indicate errors. As you need neither, no further
    preliminaries are necessary.


    >>>/* add code here */
    >>>
    >>> }
    >>> } else {
    >>> while(s--)
    >>> putchar(' ');
    >>> putchar('^');
    >>> printf(" invalid character\n");
    >>> }
    >>> }
    >>>
    >>> return 0;
    >>>}
    >>>
    >>>/* int *dynarray;
    >>> dynarray = malloc(l * sizeof(long)); */
    >>>/* end source */
    >>>
    >>>I seek to dynamically allocate memory of an array of unsigned longs with
    >>>array size determined from an unsigned long from the keyboard. With
    >>>program control as it exists now, it would seem that I would get the
    >>>business done where I have marked
    >>>/* add code here */
    >>>Do I need to redesign program control in order not to have a leak the
    >>>size of Texas?

    >>
    >>Well, don't forget to free() allocated storage :)
    >>Apart from that, only forgetting to check against l != 0 can be a
    >>problem.

    >
    > I'll give it hell tonight. Joe


    Good luck :)


    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, May 10, 2006
    #5
  6. Joe Smith

    Joe Smith Guest

    "Michael Mair"
    > Joe Smith schrieb:
    >> "Michael Mair" penned:
    >>>Joe Smith schrieb:
    >>>
    >>>
    >>>Note: The values of l indicating potential errors are 0
    >>>and ULONG_MAX; as you want to allocate storage neither for 0
    >>>nor for ULONG_MAX array elements (at least if by "billion" you
    >>>mean 1e9), you can just test for >0 and <= 333333333UL (assuming
    >>>you mean "a third of a" by "third") in the first place.
    >>>Only if you have a failure there, I'd check using strspn() or
    >>>repeat the strtoul() call preceded by errno = 0.
    >>>This is more straightforward and entirely sufficient.

    >>
    >> 'm' and 'b' are both labials in english. 'onnen' and 'arden' in german

    > ITYM 'onen' and 'arden'.
    >> don't penetrate the stupor of thought that a lot of persons who have who
    >> use these words, i.e. regarding largish numbers.

    >
    > I am not sure what you are trying to achieve with these
    > "witticisms" of yours: There are two possible meanings of "billion"
    > in English, so the question was just to clarify whether you mean
    > 1e9 or 1e12.
    > Especially in a newsgroup frequented by native speakers and
    > non-native speakers alike, everyone should take care to express his
    > or her intent clearly and concisely.


    I would object to my witticism needing to be regarded as a string literal
    had the grammar been correct. Maybe we should say something, as I have
    serially witnessed German-English discussions fly off the rails right here,
    usually by the English speaker thinking 'Milliard' was a million. I will
    use billion to mean 10^9 and trillion to mean 10^12 .

    >
    >> I would have thought that a zero input would have succumbed to the ERANGE
    >> flag. It seeems to me, however, that you're hinting my sniped code needs
    >> a tune-up here.

    >
    > Why don't you just read the documentation of strtoul()?
    > It will tell you that 0 and ULONG_MAX are the only return values
    > that _can_ indicate errors. As you need neither, no further
    > preliminaries are necessary.


    I'll get to it over cereal today.

    >>>>I seek to dynamically allocate memory of an array of unsigned longs with
    >>>>array size determined from an unsigned long from the keyboard. With
    >>>>program control as it exists now, it would seem that I would get the
    >>>>business done where I have marked
    >>>>/* add code here */
    >>>>Do I need to redesign program control in order not to have a leak the
    >>>>size of Texas?
    >>>
    >>>Well, don't forget to free() allocated storage :)
    >>>Apart from that, only forgetting to check against l != 0 can be a
    >>>problem.


    [snip]
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>


    #define BUF_MAX 80

    int main (void) {
    char buf[BUF_MAX];
    unsigned long l, i;
    size_t s;
    int *dynarray;
    printf("give me a number less than a third billion: \n");
    while(fgets(buf, BUF_MAX, stdin) != NULL) {
    if ((s = strspn(buf, "0123456789 \t\n")) == strlen(buf)) {
    errno = 0;
    l = strtoul(buf, NULL, 10);
    if (errno == ERANGE) {
    printf("number out of range!\n");
    } else {

    /* dyn mem alloc */
    dynarray = malloc((l+1) * sizeof(long));
    if (dynarray != NULL)
    {
    /* populate array with the dummy's index */
    for(i = 0;i <= l; ++ i) dynarray = i;
    /* output: print em all */
    for(i = 0;i <= l; ++ i) printf("%lu %lu\n", i, l);
    free(dynarray);
    }
    /* else code for unsuccessful malloc here */

    }
    } else {
    while(s--)
    putchar(' ');
    putchar('^');
    printf(" invalid character\n");
    }
    }

    return 0;
    }
    /* end source */
    This compiles and seems to behave. At this point I wouldn't know what to do
    with an unsuccessful malloc. This malloc was particularly effective when
    gorgeous, blond Stephanie asked me whether what I was writing on the napkin
    was difficult mathematics. I told her it wasn't easy. I tested for values
    up to four million. It would seem quite possible that a computer might have
    trouble storing one third of a billion longs. Joe
     
    Joe Smith, May 10, 2006
    #6
  7. Joe Smith

    Michael Mair Guest

    Joe Smith schrieb:
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <string.h>
    > #include <errno.h>
    >
    >
    > #define BUF_MAX 80
    >
    > int main (void) {
    > char buf[BUF_MAX];
    > unsigned long l, i;
    > size_t s;
    > int *dynarray;
    > printf("give me a number less than a third billion: \n");
    > while(fgets(buf, BUF_MAX, stdin) != NULL) {
    > if ((s = strspn(buf, "0123456789 \t\n")) == strlen(buf)) {
    > errno = 0;
    > l = strtoul(buf, NULL, 10);
    > if (errno == ERANGE) {
    > printf("number out of range!\n");
    > } else {
    >
    > /* dyn mem alloc */
    > dynarray = malloc((l+1) * sizeof(long));


    Are you kidding?
    In your OP, you wrote
    ,- <> -
    I seek to dynamically allocate memory of an array of unsigned
    longs with array size determined from an unsigned long from the
    keyboard.
    `----
    Now, you are allocating memory and assign it to an int *
    variable. The "sizeof(long)" does not help either and seems like
    something having been forgotten when switching from long to int.
    Note that around here, the following way of using malloc() is
    recommended as least error prone way:
    unsigned long *dynarray;
    ....
    dynarray = malloc((l+1) * sizeof *dynarray);

    > if (dynarray != NULL)
    > {
    > /* populate array with the dummy's index */
    > for(i = 0;i <= l; ++ i) dynarray = i;


    This assignment could overflow.
    If you use your compiler at the highest warning level, it
    probably will warn you about that.

    > /* output: print em all */
    > for(i = 0;i <= l; ++ i) printf("%lu %lu\n", i, l);
    > free(dynarray);
    > }
    > /* else code for unsuccessful malloc here */
    >
    > }
    > } else {
    > while(s--)
    > putchar(' ');
    > putchar('^');
    > printf(" invalid character\n");
    > }
    > }
    >
    > return 0;
    > }
    > /* end source */


    As mentioned in a previous post, I'd rather check for strtoul()s
    possible error returns. This is probably more efficient and less
    obscure than your method. It has the added benefit that you can
    check whether the user input is in the required range and not in
    the type range.


    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, May 10, 2006
    #7
  8. Joe Smith

    Joe Smith Guest

    "Michael Mair"
    > Joe Smith schrieb:


    >> dynarray = malloc((l+1) * sizeof(long));

    > Are you kidding?

    Unfortunately, no.

    > In your OP, you wrote
    > ,- <> -
    > I seek to dynamically allocate memory of an array of unsigned
    > longs with array size determined from an unsigned long from the
    > keyboard.
    > `----
    > Now, you are allocating memory and assign it to an int *
    > variable. The "sizeof(long)" does not help either and seems like
    > something having been forgotten when switching from long to int.
    > Note that around here, the following way of using malloc() is
    > recommended as least error prone way:
    > unsigned long *dynarray;
    > ....
    > dynarray = malloc((l+1) * sizeof *dynarray);
    >
    >> if (dynarray != NULL)
    >> {
    >> /* populate array with the dummy's index */
    >> for(i = 0;i <= l; ++ i) dynarray = i;

    >
    > This assignment could overflow.
    > If you use your compiler at the highest warning level, it
    > probably will warn you about that.


    The argument for mallocing an array would seem to consist of something that
    indicates the the number of boxes you're going to have multplied by how much
    memory each of those boxes is going to take. (An array is a bunch of boxes
    in a line with numbers on the side. One thinks of it this way and takes his
    pills. Mine are blue.) The (l+1) part indicates the number of boxes, and C
    being what it is, I ordered an extra box for zero.

    > As mentioned in a previous post, I'd rather check for strtoul()s
    > possible error returns. This is probably more efficient and less
    > obscure than your method. It has the added benefit that you can
    > check whether the user input is in the required range and not in
    > the type range.


    But just as soon as a figure out longs, I'm going to want longers.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>


    #define BUF_MAX 80

    int main (void) {
    char buf[BUF_MAX];
    unsigned long l, i;
    size_t s;
    unsigned long *dynarray;
    printf("give me a number less than a third billion: \n");
    while(fgets(buf, BUF_MAX, stdin) != NULL) {
    if ((s = strspn(buf, "0123456789 \t\n")) == strlen(buf)) {
    errno = 0;
    l = strtoul(buf, NULL, 10);
    if (errno == ERANGE) {
    printf("number out of range!\n");
    } else {

    /* dyn mem alloc */
    dynarray = malloc((l+1) * sizeof(long));
    if (dynarray != NULL)
    {
    /* populate array with the dummy's index */
    for(i = 0;i <= l; ++ i) dynarray = i;
    /* output: print em all */
    for(i = 0;i <= l; ++ i) printf("%lu %lu\n", i, l);
    free(dynarray);
    }
    /* else code for unsuccessful malloc here */

    }
    } else {
    while(s--)
    putchar(' ');
    putchar('^');
    printf(" invalid character\n");
    }
    }

    return 0;
    }
    /* end source */
    I believe that this addresses what Mr. Mair was getting at, but that a
    pointer would need to be an unsigned long makes no sense to me. There
    doesn't seem to be much you could do in that /*else code */ part other than
    to
    printf("give me a smaller number or get more RAM\n");
    Joe
     
    Joe Smith, May 10, 2006
    #8
  9. Joe Smith

    Joe Smith Guest

    "Robert Gamble"
    > Joe Smith wrote:


    [code snipped]
    > Below is the appropriate caveat I listed with the code when I
    > originally posted it:
    >
    > "If the input contains only of whitespace or the minus sign the
    > converted value will be 0. "
    >
    > The minus sign part does not, of course, apply to your version. The
    > word "contains" was supposed to be "consists".
    >
    > I have tried to impress upon you the fact that the code was written to
    > meet a very specific need, what exactly are the needs *you* are trying
    > to accomidate?


    I've always cheated with io and C. Indeed, I think the sheer punishment
    that is the io syntax in C led many on a path that would leave them hanging
    out with the polymorphism wizard. My first endeavor is to improve my
    syntax. When I've got enough of that remembered, renewed, modified, revised
    and added, I would like to play with combinatorial-sized numbers. Joe
     
    Joe Smith, May 10, 2006
    #9
  10. Joe Smith

    CBFalconer Guest

    Joe Smith wrote:
    >

    .... snip ...
    >
    > I would object to my witticism needing to be regarded as a string
    > literal had the grammar been correct. Maybe we should say
    > something, as I have serially witnessed German-English discussions
    > fly off the rails right here, usually by the English speaker
    > thinking 'Milliard' was a million. I will use billion to mean 10^9
    > and trillion to mean 10^12 .


    The simple cure is to simply write "1e9" or "1e12" as needed, and
    retire the confusing verbiage. Similarly, the use of ISO standard
    date formats can avoid much confusion.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
    Also see <http://www.safalra.com/special/googlegroupsreply/>
     
    CBFalconer, May 10, 2006
    #10
  11. Joe Smith

    Michael Mair Guest

    Joe Smith schrieb:
    > "Michael Mair"
    >>Joe Smith schrieb:

    >
    >>>dynarray = malloc((l+1) * sizeof(long));

    >>
    >>Are you kidding?

    >
    > Unfortunately, no.
    >
    >>In your OP, you wrote
    >>,- <> -
    >>I seek to dynamically allocate memory of an array of unsigned
    >>longs with array size determined from an unsigned long from the
    >>keyboard.
    >>`----
    >>Now, you are allocating memory and assign it to an int *
    >>variable. The "sizeof(long)" does not help either and seems like
    >>something having been forgotten when switching from long to int.
    >>Note that around here, the following way of using malloc() is
    >>recommended as least error prone way:
    >> unsigned long *dynarray;
    >> ....
    >> dynarray = malloc((l+1) * sizeof *dynarray);
    >>
    >>
    >>>if (dynarray != NULL)
    >>>{
    >>>/* populate array with the dummy's index */
    >>>for(i = 0;i <= l; ++ i) dynarray = i;

    >>
    >>This assignment could overflow.
    >>If you use your compiler at the highest warning level, it
    >>probably will warn you about that.

    >
    > The argument for mallocing an array would seem to consist of something that
    > indicates the the number of boxes you're going to have multplied by how much
    > memory each of those boxes is going to take. (An array is a bunch of boxes
    > in a line with numbers on the side. One thinks of it this way and takes his
    > pills. Mine are blue.) The (l+1) part indicates the number of boxes, and C
    > being what it is, I ordered an extra box for zero.


    If dynarray is a pointer to int, dynarray will access a
    signed int value. Assigning i, an unsigned long value, to
    dynarray may lead to signed integer overflow.

    <snip>
    > unsigned long l, i;

    <snip>
    > unsigned long *dynarray;

    <snip>
    > dynarray = malloc((l+1) * sizeof(long));

    <snip>
    > for(i = 0;i <= l; ++ i) dynarray = i;

    <snip>
    > /* end source */
    > I believe that this addresses what Mr. Mair was getting at, but that a
    > pointer would need to be an unsigned long makes no sense to me.


    No. Not the pointer is an unsigned long -- what it points to is of
    type unsigned long. As you stated that you want an array l of unsigned
    long (now rather an array l+1 of unsigned long), it is an error
    to use a pointer to int: It is the wrong pointer type, dynarray
    consequently will be of type int instead of type unsigned long.

    > There
    > doesn't seem to be much you could do in that /*else code */ part other than
    > to
    > printf("give me a smaller number or get more RAM\n");


    The way the program is structured, that is about it.


    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, May 11, 2006
    #11
  12. Joe Smith

    Richard Bos Guest

    "Joe Smith" <> wrote:

    > I would object to my witticism needing to be regarded as a string literal
    > had the grammar been correct. Maybe we should say something, as I have
    > serially witnessed German-English discussions fly off the rails right here,
    > usually by the English speaker thinking 'Milliard' was a million. I will
    > use billion to mean 10^9 and trillion to mean 10^12 .


    Ah, but _that_, OTOH, has the disadvantage that you're only compatible
    with USAnians and USAnianised[1] Britons[2]. The rest of the world knows
    that a billion is 10e12. What's more, this incompatibility is harder to
    detect than using milliard.

    Richard

    [1] Or should that be *USAnianized?
    [2] More and more each day, and President^WPM Blair isn't helping.
     
    Richard Bos, May 11, 2006
    #12
  13. Richard Bos said:

    > "Joe Smith" <> wrote:
    >
    >> I would object to my witticism needing to be regarded as a string literal
    >> had the grammar been correct. Maybe we should say something, as I have
    >> serially witnessed German-English discussions fly off the rails right
    >> here,
    >> usually by the English speaker thinking 'Milliard' was a million. I will
    >> use billion to mean 10^9 and trillion to mean 10^12 .

    >
    > Ah, but _that_, OTOH, has the disadvantage that you're only compatible
    > with USAnians and USAnianised[1] Britons[2].


    And, apparently, the French.

    "Milliard" is, of course, the proper way to describe 1,000,000,000.

    > The rest of the world knows that a billion is 10e12.


    We also know the proper end at which to open our eggs.

    > What's more, this incompatibility is harder to
    > detect than using milliard.


    I will continue to use "billion" for 1e12 and "milliard" for 1e9, and if
    people wish to misunderstand me, that is their problem, not mine.


    > [...] President^WPM Blair isn't helping.


    Don't blame me. I didn't vote for him. (Ever.)


    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, May 11, 2006
    #13
  14. Joe Smith

    Joe Smith Guest

    "Richard Heathfield"
    > Richard Bos said:
    >> "Joe Smith"


    >>> I would object to my witticism needing to be regarded as a string
    >>> literal
    >>> had the grammar been correct. Maybe we should say something, as I have
    >>> serially witnessed German-English discussions fly off the rails right
    >>> here,
    >>> usually by the English speaker thinking 'Milliard' was a million. I
    >>> will
    >>> use billion to mean 10^9 and trillion to mean 10^12 .

    >>
    >> Ah, but _that_, OTOH, has the disadvantage that you're only compatible
    >> with USAnians and USAnianised[1] Britons[2].

    >
    > And, apparently, the French.
    >
    > "Milliard" is, of course, the proper way to describe 1,000,000,000.
    >
    >> The rest of the world knows that a billion is 10e12.

    >
    > We also know the proper end at which to open our eggs.
    >
    >> What's more, this incompatibility is harder to
    >> detect than using milliard.

    >
    > I will continue to use "billion" for 1e12 and "milliard" for 1e9, and if
    > people wish to misunderstand me, that is their problem, not mine.
    >
    >
    >> [...] President^WPM Blair isn't helping.

    >
    > Don't blame me. I didn't vote for him. (Ever.)



    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>


    #define BUF_MAX 80

    int main (void) {
    char buf[BUF_MAX];
    unsigned long l, i, *dynarray;
    size_t s;

    printf("give me a number less than a third billion: \n");
    while(fgets(buf, BUF_MAX, stdin) != NULL) {
    if ((s = strspn(buf, "0123456789 \t\n")) == strlen(buf)) {
    errno = 0;
    l = strtoul(buf, NULL, 10);
    if (errno == ERANGE) {
    printf("number out of range!\n");
    } else {

    /* dyn mem alloc */
    dynarray = malloc((l+1) * sizeof *dynarray);

    if (dynarray != NULL)
    {
    /* populate array with the dummy's index */
    for(i = 0;i <= l; ++ i) dynarray = i;
    /* output: print em all */
    for(i = 0;i <= l; ++ i)
    {
    /* printf("%lu %lu\n", i, l); */
    }
    free(dynarray);
    }
    /* else code for unsuccessful malloc here */
    else
    {
    printf("malloc failed\n");
    free(dynarray);
    }

    }
    } else {
    while(s--)
    putchar(' ');
    putchar('^');
    printf(" invalid character\n");
    }
    }

    return 0;
    }
    /* end source */
    This is probably most directed at Mr. Mair, yet I wanted to comment on a
    couple things said along the way and thought the better way to do that might
    be to do it in one morningly post. Between an ell at 50 million and 60
    million, the malloc fails on my 'puter. On the else branch, I still free
    it, yet my unnamed OS doesn't seem to be the same after the malloc fails. I
    have warnings set as high as I know how to put them and get none.

    As to the language stuff, it's always been said that England and the US were
    two countries separated by a language. After listening to Southern Neocon
    Repugs for a seeming eternity, the split is much deeper than that.
    Cbfalconer is exactly correct. If there is the chance of miscommunication,
    show the exponent. I'm unaware of what a Standard date would look like, but
    I always am wary of order in date and month when corresponding with
    Continentals. Precise communication in clc, unlike the constant stream of
    Kremlinized crap that flows out of our largest bully pulpit is, as they say,
    impordint. Joe
     
    Joe Smith, May 11, 2006
    #14
  15. "Joe Smith" <> writes:
    [...]
    > As to the language stuff, it's always been said that England and the US were
    > two countries separated by a language. After listening to Southern Neocon
    > Repugs for a seeming eternity, the split is much deeper than that.
    > Cbfalconer is exactly correct. If there is the chance of miscommunication,
    > show the exponent. I'm unaware of what a Standard date would look like, but
    > I always am wary of order in date and month when corresponding with
    > Continentals. Precise communication in clc, unlike the constant stream of
    > Kremlinized crap that flows out of our largest bully pulpit is, as they say,
    > impordint. Joe


    I suggest that national politics, or even international politics, is
    not a good subject for comp.lang.c. I have my own very strong
    opinions in those areas, but I don't express them, or even allude to
    them, here in comp.lang.c. The very last thing we need here is a
    flame war about, say, US party politics.

    But the real reason I'm posting this followup is to answer your
    question about standard date formats. There is an ISO standard for
    time and date formats, ISO 8601. The date format is YYYY-MM-DD, where
    YYYY is the year, MM is the month (01..12), and DD is the day of the
    month (01..31). It's unambiguous, it sorts correctly, and it's not
    subject to variations in natural-language month names. For example,
    today is 2006-05-11.

    For more details, see <http://www.cl.cam.ac.uk/~mgk25/iso-time.html>.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, May 12, 2006
    #15
  16. Joe Smith

    Nelu Guest

    Richard Heathfield wrote:
    > Richard Bos said:
    >
    > > "Joe Smith" <> wrote:
    > >
    > >> I would object to my witticism needing to be regarded as a string literal
    > >> had the grammar been correct. Maybe we should say something, as I have
    > >> serially witnessed German-English discussions fly off the rails right
    > >> here,
    > >> usually by the English speaker thinking 'Milliard' was a million. I will
    > >> use billion to mean 10^9 and trillion to mean 10^12 .

    > >
    > > Ah, but _that_, OTOH, has the disadvantage that you're only compatible
    > > with USAnians and USAnianised[1] Britons[2].

    >
    > And, apparently, the French.
    >
    > "Milliard" is, of course, the proper way to describe 1,000,000,000.

    The english got the 'proper' use of billion from the French after the
    17th century, apparently. The french mathematicians changed the group
    of six digits to represent multiples of millions to groups of three
    digits, a little later. US adopted the French system, but the French
    switched back, again, to the old system, later. The English switched to
    the US system afterwards. Supposedly the american use of billion was
    more practical (at least in that period). There was also some story
    related to the City of Ilion (Troy) but I forgot it :).

    I've been away from the group for some time. It's good to be back! :)

    --
    Ioan - Ciprian Tandau
    tandau _at_ freeshell _dot_ org (hope it's not too late)
    (... and that it still works...)
     
    Nelu, May 12, 2006
    #16
  17. Joe Smith

    Joe Smith Guest

    "Keith Thompson"
    > "Joe Smith" <> writes:
    > [...]
    >> [arguably inflammatory rhetoric intermixed with something topical]


    > I suggest that national politics, or even international politics, is
    > not a good subject for comp.lang.c. I have my own very strong
    > opinions in those areas, but I don't express them, or even allude to
    > them, here in comp.lang.c. The very last thing we need here is a
    > flame war about, say, US party politics.
    >
    > But the real reason I'm posting this followup is to answer your
    > question about standard date formats. There is an ISO standard for
    > time and date formats, ISO 8601. The date format is YYYY-MM-DD, where
    > YYYY is the year, MM is the month (01..12), and DD is the day of the
    > month (01..31). It's unambiguous, it sorts correctly, and it's not
    > subject to variations in natural-language month names. For example,
    > today is 2006-05-11.


    Politics qua politics is OT. I wanted to state on 2006-05-12 that this
    isn't a good day for US programmers from a perspective of resource
    allocation. Think, Keith, of all the stuff you have to do to get your
    projects staffed and done. The US taxpayer now owns the largest piece of
    digital duke ever instantiated. Data mining? Is that for hobbits? The army
    of persons who have been sniffing packets needed to get
    UP_CLOSE_AND_PERSONAL, for which they appear to lack either courage, or more
    likely, conviction, and certainly, court approval. Did we really need to
    invest these type of resources in other snafu? I'll try to focus again on
    memory management techniques in the C programming language. Joe
     
    Joe Smith, May 12, 2006
    #17
  18. "Joe Smith" <> writes:
    > "Keith Thompson"
    >> "Joe Smith" <> writes:
    >> [...]
    >>> [arguably inflammatory rhetoric intermixed with something topical]

    >
    >> I suggest that national politics, or even international politics, is
    >> not a good subject for comp.lang.c. I have my own very strong
    >> opinions in those areas, but I don't express them, or even allude to
    >> them, here in comp.lang.c. The very last thing we need here is a
    >> flame war about, say, US party politics.

    [snip]
    >
    > Politics qua politics is OT. I wanted to state on 2006-05-12 that this
    > isn't a good day for US programmers from a perspective of resource
    > allocation. Think, Keith, of all the stuff you have to do to get your
    > projects staffed and done. The US taxpayer now owns the largest piece of
    > digital duke ever instantiated. Data mining? Is that for hobbits? The army
    > of persons who have been sniffing packets needed to get
    > UP_CLOSE_AND_PERSONAL, for which they appear to lack either courage, or more
    > likely, conviction, and certainly, court approval. Did we really need to
    > invest these type of resources in other snafu? I'll try to focus again on
    > memory management techniques in the C programming language. Joe


    None of this is relevant to the C programming language.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, May 12, 2006
    #18
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. beetle
    Replies:
    25
    Views:
    828
    Mabden
    Jan 31, 2005
  2. srini

    Dynamic mem allocation for pointers

    srini, Dec 14, 2005, in forum: C Programming
    Replies:
    4
    Views:
    371
    Barry Schwarz
    Dec 18, 2005
  3. Replies:
    8
    Views:
    340
    jaysome
    Mar 1, 2007
  4. cerr

    Queue question wih dyn mem alloc

    cerr, Dec 28, 2009, in forum: C Programming
    Replies:
    14
    Views:
    568
    Barry Schwarz
    Dec 31, 2009
  5. news.aon.at
    Replies:
    11
    Views:
    678
    Ian Collins
    Jan 29, 2011
Loading...

Share This Page