Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   dynamic mem alloc q (http://www.velocityreviews.com/forums/t442672-dynamic-mem-alloc-q.html)

Joe Smith 05-09-2006 06:10 PM

dynamic mem alloc q
 
#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



Michael Mair 05-09-2006 07:43 PM

Re: dynamic mem alloc q
 
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.

Joe Smith 05-10-2006 12:06 AM

Re: dynamic mem alloc q
 

"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



Robert Gamble 05-10-2006 12:30 AM

Re: dynamic mem alloc q
 
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


Michael Mair 05-10-2006 05:13 AM

Re: dynamic mem alloc q
 
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.

Joe Smith 05-10-2006 05:54 PM

Re: dynamic mem alloc q
 

"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] = 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



Michael Mair 05-10-2006 08:01 PM

Re: dynamic mem alloc q
 
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
,- <ec3e2dad@news.usenetmonster.com> -
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] = 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.

Joe Smith 05-10-2006 08:59 PM

Re: dynamic mem alloc q
 

"Michael Mair"
> Joe Smith schrieb:


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

> Are you kidding?

Unfortunately, no.

> In your OP, you wrote
> ,- <ec3e2dad@news.usenetmonster.com> -
> 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] = 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] = 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 05-10-2006 09:26 PM

Re: dynamic mem alloc q
 

"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



CBFalconer 05-10-2006 10:54 PM

Re: dynamic mem alloc q
 
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/>




All times are GMT. The time now is 03:08 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.