Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

malloc

29 views
Skip to first unread message

Bill Cunningham

unread,
May 1, 2011, 1:01:49 AM5/1/11
to
I have this unfinished untested code that I would like to enquire to
someone that has used malloc() before if I can use it here.

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

int main(int argc, char **argv)
{
if (argc > 10) {
printf(" too many characters in name\n try 10 or less\n");
exit(1);
}
char p[20];
FILE *fp;
size_t t;
if ((fopen("argv[1]", "r")) == EOF) {
fprintf(stderr, "fopen error\n");
exit(1);
}
t = fread(p, sizeof(int), 20, fp);
if (t = ferror(fp)) {
fprintf(stderr, " file error indicated\n");
exit(1);
} else if (t = feof(fp)) {
fprintf(stderr, " EOF indicated\n");
exit(1);
}

In the code above the 3rd parameter of malloc() is taking 20 character
members. That's not enough. But I don't want buffer overflow either. I know
this is kind of reinventing the wheel so far in what is above but there is a
reason for this. I am trying to write a text editor that takes code that
only ends with "+" instead of C's normal ";". Can malloc be put where the 20
is and just take a malloc? malloc returns void * and this 3rd parameter a
size_t so maybe a struct or union would be easier. Such as

struct values{
size_t t;
void * val;
};

Would that eliminate the need for casting?

Bill


Bill Cunningham

unread,
May 1, 2011, 1:03:31 AM5/1/11
to
Bill Cunningham wrote:

[snip]

> In the code above the 3rd parameter of malloc() is taking 20
> character members. That's not enough. But I don't want buffer
> overflow either.

[...]

Sorry that is not 3rd parameter of malloc but of fread().

Bill


luser- -droog

unread,
May 1, 2011, 1:31:36 AM5/1/11
to
On May 1, 12:01 am, "Bill Cunningham" <nos...@nspam.invalid> wrote:
>     I have this unfinished untested code that I would like to enquire
^
You need a comma between |
two adjectives that modify |
the same noun. |
,___________________________|

Ian Collins

unread,
May 1, 2011, 1:35:25 AM5/1/11
to
On 05/ 1/11 05:01 PM, Bill Cunningham wrote:
> I have this unfinished untested code that I would like to enquire to
> someone that has used malloc() before if I can use it here.

2/10.

You're loosing your touch.

--
Ian Collins

Bill Cunningham

unread,
May 1, 2011, 2:14:03 AM5/1/11
to

Let me see if I can make this easier to understand.

fread(p,sizeof(int),20,fp);

Ok nothing will go above 20 size_t's but if only 15 size_t's are filled I
want to return 5 to memory. Is there a way to do that?

Bill


Bill Cunningham

unread,
May 1, 2011, 2:15:09 AM5/1/11
to

Sorry.


Mickey Mouse

unread,
May 1, 2011, 3:18:31 AM5/1/11
to


realloc

Bill Cunningham

unread,
May 1, 2011, 4:07:43 AM5/1/11
to
Mickey Mouse wrote:

> realloc

Then I'm not going to want to use fread's standard parameters am I? I am
going to have to write a function that uses realloc and fread or fgetc for
char * types anyway.

Bill


BartC

unread,
May 1, 2011, 8:52:23 AM5/1/11
to

"luser- -droog" <mij...@yahoo.com> wrote in message
news:baab1bba-f3f2-40da...@c41g2000yqm.googlegroups.com...

Will a conjunction do?


--
Bartc

osmium

unread,
May 1, 2011, 10:56:56 AM5/1/11
to
"Bill Cunningham" wrote:

For the umpteenth time.

I suggest you try something simpler that you might actually get to work.
Write a program that tries to read 20 char from a file that may or may not
exist. The file's name is specified by a command line argument.

You can create a file to test your program by using a text editor.

Barry Schwarz

unread,
May 1, 2011, 1:31:23 PM5/1/11
to

One of your more pedestrian trolling efforts Bill!

Original post:
Title completely unrelated to the code
Bad grammar
Comparing the number of command line arguments to the length
of a value
Comparing a pointer to a negative int
Non-portable return value from main
Trying to read 20 int into an array of 20 char
Assigning a potentially negative number to a size_t
Using = instead of == in an if
Asking a question about a function call that does not appear
in the post
Discussing the third parameter of a function that has only one
Discussing non-existent casts

Follow up:
Discussing input operations on size-t objects that do not
exist.

Here:
Finally reaching the inevitable conclusion of all your
threads, random guessing on how to solve an unspecified problem.

I think the 2 out of 10 rating given else thread was too generous. I
suggest you change your goal from error density to subtlety.

--
Remove del for email

Tim Rentsch

unread,
May 1, 2011, 3:51:06 PM5/1/11
to
"Bill Cunningham" <nos...@nspam.invalid> writes:

> I have this unfinished untested code that I would like to enquire to
> someone that has used malloc() before if I can use it here.
>

> [snip]

You might want to try posting this again 11 months from now.

Bill Cunningham

unread,
May 1, 2011, 6:20:40 PM5/1/11
to
osmium wrote:

> For the umpteenth time.
>
> I suggest you try something simpler that you might actually get to
> work. Write a program that tries to read 20 char from a file that may
> or may not exist. The file's name is specified by a command line
> argument.
> You can create a file to test your program by using a text editor.

Looking over that code I have noticed several bugs. the argv[1]
shouldn't be in quotes. The tests for t isn't an assignment like written but
should be t== not t=. If I can't write something like this I'm a very sad
story. I've had success with fgetc more anyway.

Bill


luser- -droog

unread,
May 2, 2011, 3:05:15 AM5/2/11
to

I think so. (Linking up phrases and something and something and
something...)

I gave up trying to spot all the errors so I tried to find the first
one.

--
lothario draganeti

John Gordon

unread,
May 2, 2011, 11:31:40 AM5/2/11
to

> fread(p,sizeof(int),20,fp);

Allocate a suitably large buffer with malloc().
Call fread() to put stuff in the buffer.
If the buffer ends up being too large, resize it with realloc().

I noticed a problem with your original code:

> char p[20];


> t = fread(p, sizeof(int), 20, fp);

p is declared as an array of 20 characters, however you're trying to stuff
20 ints into it, which is too much. If you know you will put ints into p,
declare it that way.

--
John Gordon A is for Amy, who fell down the stairs
gor...@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

Bill Cunningham

unread,
May 2, 2011, 4:55:27 PM5/2/11
to
Barry Schwarz wrote:

> One of your more pedestrian trolling efforts Bill!
>
> Original post:
> Title completely unrelated to the code
> Bad grammar
> Comparing the number of command line arguments to the length
> of a value
> Comparing a pointer to a negative int
> Non-portable return value from main
> Trying to read 20 int into an array of 20 char
> Assigning a potentially negative number to a size_t
> Using = instead of == in an if
> Asking a question about a function call that does not appear
> in the post
> Discussing the third parameter of a function that has only one
> Discussing non-existent casts
>
> Follow up:
> Discussing input operations on size-t objects that do not
> exist.
>
> Here:
> Finally reaching the inevitable conclusion of all your
> threads, random guessing on how to solve an unspecified problem.
>
> I think the 2 out of 10 rating given else thread was too generous. I
> suggest you change your goal from error density to subtlety.

I disagree with some of your views of the OP. malloc does match with the
intention of the post though not in the code because I didn't know how to
use it. The whole point of the post. The grammer is not perfect granted. It
seems that "Mickey Mouse" was the only one so far who has understood my
inquiry and post.

Bill


Bill Cunningham

unread,
May 2, 2011, 4:58:00 PM5/2/11
to
Mickey Mouse wrote:
> realloc

Thank you for your response. You have answered my question and seem to
be the only one understanding my inquiry.

Bill


Mickey Mouse

unread,
May 2, 2011, 5:34:39 PM5/2/11
to

You are welcome.

You might be better posting questions in 'comp.lang.c.moderated', it
is a mostly quiet group but one where questions get answered.


Ian Collins

unread,
May 2, 2011, 5:55:21 PM5/2/11
to

"Bill" first asked about malloc in 2003 (as seen from google).

Go figure.

--
Ian Collins

Seebs

unread,
May 2, 2011, 6:18:05 PM5/2/11
to
On 2011-05-02, Mickey Mouse <fa...@email.com> wrote:
> You might be better posting questions in 'comp.lang.c.moderated', it
> is a mostly quiet group but one where questions get answered.

But it's also a group where the moderator might not approve posts which
were blatantly trolling. You never know.

I honestly don't know what I'd do if one of Bill's surrealist pieces showed
up. I have spent a fair amount of my recreational time over the last twenty
years marvelling at the many and diverse ways in which humans can be
mind-numbingly stupid, and I still can't figure out for sure whether his
posts are *possibly* the result of sincere stupidity. I tend to suspect
that he's an extremely talented and persistent troll. If not, I imagine
someone could get a doctorate by mapping the boundaries of his cognitive
dysfunction.

-s
--
Copyright 2011, all wrongs reversed. Peter Seebach / usenet...@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

crisgoogle

unread,
May 2, 2011, 8:43:08 PM5/2/11
to
On May 2, 12:05 am, luser- -droog <mijo...@yahoo.com> wrote:
> BartC wrote:
> > "luser- -droog" <mijo...@yahoo.com> wrote in message

> >news:baab1bba-f3f2-40da...@c41g2000yqm.googlegroups.com...
> > > On May 1, 12:01 am, "Bill Cunningham" <nos...@nspam.invalid> wrote:
> > >>     I have this unfinished untested code that I would like to enquire
> > >                            ^
> > > You need a comma between    |
> > > two adjectives that modify  |
> > > the same noun.              |
> > > ,___________________________|
>
> > Will a conjunction do?
>
> I think so. (Linking up phrases and something and something and
> something...)

Hookin' up clauses and phrases and words ...

(IIRC -- it's been 30 years or so -- but probably YTIYF))

=)

Bill Cunningham

unread,
May 2, 2011, 11:49:00 PM5/2/11
to

"Tim Rentsch" <t...@alumni.caltech.edu> wrote in

> You might want to try posting this again 11 months from now.

I don't believe I've ever posted any question like this before
concerning memory reallocation.

Bill


Bill Cunningham

unread,
May 2, 2011, 11:51:08 PM5/2/11
to

"Barry Schwarz" <schw...@dqel.com> wrote in

> One of your more pedestrian trolling efforts Bill!
>
> Original post:
> Title completely unrelated to the code
> Bad grammar
> Comparing the number of command line arguments to the length
> of a value
> Comparing a pointer to a negative int
> Non-portable return value from main
> Trying to read 20 int into an array of 20 char
> Assigning a potentially negative number to a size_t
> Using = instead of == in an if
> Asking a question about a function call that does not appear
> in the post
> Discussing the third parameter of a function that has only one
> Discussing non-existent casts
>
> Follow up:
> Discussing input operations on size-t objects that do not
> exist.
>
> Here:
> Finally reaching the inevitable conclusion of all your
> threads, random guessing on how to solve an unspecified problem.
>
> I think the 2 out of 10 rating given else thread was too generous. I
> suggest you change your goal from error density to subtlety.

Note: See other posts concerning this thread.
---

Sorry that is not 3rd parameter of malloc but of fread().

Bill
---

Ian Collins

unread,
May 2, 2011, 9:35:30 PM5/2/11
to

Bill Cunningham

unread,
May 2, 2011, 9:38:40 PM5/2/11
to
osmium wrote:
> For the umpteenth time.
>
> I suggest you try something simpler that you might actually get to
> work. Write a program that tries to read 20 char from a file that may
> or may not exist.

Why?

The file's name is specified by a command line
> argument.

So?

>
> You can create a file to test your program by using a text editor.

What's this post all about? What's it's title? I could've picked picked
a better title true. Which do you think would've made a better title?

1) stream question or
2) memory allocation question ?

Bill


osmium

unread,
May 3, 2011, 10:02:30 AM5/3/11
to

"Bill Cunningham" wrote:

Your post made clear that you were trying to skip the fundamentals and go
right to the meat of the thing; for this week your target was "malloc". My
suggestion was for a more ordered approach in which you might actually learn
something and get a program you had written to work. But clearly you don't
like that approach.

Your approach reminds me of someone in a foreign country opening an English
dictionary at random, and asking questions about a few words every week.
What is a pharmacist? What is a dormer? What is rubidium? What is a
carnival?

Since you are unlikely to live for much more than 100 years, I think that
method is doomed to failure.


Bill Cunningham

unread,
May 3, 2011, 10:20:08 AM5/3/11
to
Ian Collins wrote:

> "Bill" first asked about malloc in 2003 (as seen from google).
>
> Go figure.

malloc is the only memory allocation I know about other than arrays.
realloc must come into play somewhere.

Bill


Angel

unread,
May 3, 2011, 10:55:42 AM5/3/11
to

There are malloc(), calloc() and realloc(). In addition, on a Unix
system there are also sbrk() and mmap(), but if you want to write
portable code you should stick to malloc() and friends.

realloc() could, for example, be used to implement a dynamic "array" that
grows in size as elements are added and shrinks as elements are removed.


--
The perfected state of a spam server is a smoking crater.
- The Crater Corollary to Rule #4

Bill Cunningham

unread,
May 3, 2011, 11:27:55 AM5/3/11
to
Ian Collins wrote:

http://groups.google.com/group/comp.lang.c/browse_thread/thread/8505637713d6e1eb/885a55feeb4d25d0 Nope after viewing the link above, I think I can still say I don'tbelieve I've ever posted anything about memory allocation. Encrypting andrandom strings maybe. I've never used malloc before in code but it is verypopular.B

Bill Cunningham

unread,
May 3, 2011, 11:29:39 AM5/3/11
to
Ian Collins wrote:

> "Bill" first asked about malloc in 2003 (as seen from google).
>
> Go figure.

Fill me in.

Bill


Bill Cunningham

unread,
May 3, 2011, 3:21:15 PM5/3/11
to
osmium wrote:

> Your post made clear that you were trying to skip the fundamentals
> and go right to the meat of the thing; for this week your target was
> "malloc". My suggestion was for a more ordered approach in which you
> might actually learn something and get a program you had written to
> work. But clearly you don't like that approach.

[...]

How much more to "the meat of the thing" is malloc? I'm not sure that
I'm understanding you. What do you mean by a "more ordered approach" ? The
program that was written did not included malloc. And I noticed later it was
full of bugs as posted. I must admit I need to start testing code and
examining it more closely before posting. fread as was in the program is
designed to take a set number of data.
Hence my point. I am in no position to write code that expands and
ontracts memory storage on the fly. That was what my inquiry was concerning.
Using the FILE* and streams as an example.

Bill


Bill Cunningham

unread,
May 3, 2011, 3:34:20 PM5/3/11
to
Angel wrote:

[snip]

> realloc() could, for example, be used to implement a dynamic "array"
> that grows in size as elements are added and shrinks as elements are
> removed.

Exactly what I was looking for. Using the example of streams and the
FILE* struct. I don't think fread should even be used for this come to think
of it. fgetc might be a better way. But I have no idea where to even begin
with designing a function to set aside storage and allow it to grow and
shrink dynamically.

Bill


osmium

unread,
May 3, 2011, 3:35:33 PM5/3/11
to
"Bill Cunningham" wrote:

Memory that expands or contracts is more complicated than a fixed target of
known maximum size.Your post tells me you can't even handle that. Thus the
simpler assignment I suggested. Your knowledge of argc, for example is
woefully wrong.. If you understand the basics you should be able to do what
I wrote in five minutes or less.


Ian Collins

unread,
May 3, 2011, 3:54:56 PM5/3/11
to

Then you didn't read the full thread.

--
Ian Collins

Ben Bacarisse

unread,
May 3, 2011, 4:50:52 PM5/3/11
to
"Bill Cunningham" <nos...@nspam.invalid> writes:

Your quoting is broken again.

Another one then: in Message-ID: <AUnuj.5177$kD3.212@trnddc08> (18 Feb
2008) you asked:

| So I'll need a buffer for fread and write.
| char buff(10);
| And a pointer for realloc
| char *b=&buff;
| Something like that right?

You seem to keep asking the same questions. The message I've just
quoted is from a thread about almost exactly the same issues as the
current thread: expanding a buffer to take more input.

BTW, I don't mind. The same questions keep getting asked anyway and it
really doesn't matter if it is many people asking them or just one.

--
Ben.

Angel

unread,
May 3, 2011, 4:56:48 PM5/3/11
to
On 2011-05-03, Bill Cunningham <nos...@nspam.invalid> wrote:

Here is a very quick, very dirty (no error handling or boundary
checking) implementation of a variable-length first-in, first-out
character buffer. It's only as large as it needs to be,
and the maximum size is only limited by available memory.

(Yes, I know there are far better ways to do this. It's just an example
of using realloc(), nothing more.)


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

static char *fifo = NULL;
static size_t fifo_count = 0;

size_t fifo_write(const char *data, const size_t len)
{
fifo = realloc(fifo, fifo_count + len);
memcpy(fifo + fifo_count, data, len);
fifo_count += len;
return len;
}

size_t fifo_read(char *data, const size_t len)
{
memcpy(data, fifo, len);
memmove(fifo, fifo + len, fifo_count - len);
fifo_count -= len;
fifo = realloc(fifo, fifo_count);
return len;

Bill Cunningham

unread,
May 3, 2011, 6:43:36 PM5/3/11
to
osmium wrote:

> Memory that expands or contracts is more complicated than a fixed
> target of known maximum size.Your post tells me you can't even handle
> that. Thus the simpler assignment I suggested. Your knowledge of
> argc, for example is woefully wrong.. If you understand the basics
> you should be able to do what I wrote in five minutes or less.

You're obviously more experienced than I. And have a better
understanding of C and programming in general.

Bill


Default User

unread,
May 4, 2011, 1:20:40 PM5/4/11
to

"Mickey Mouse" <fa...@email.com> wrote in message
news:kh8ur6hs3huci2rqq...@4ax.com...

> You might be better posting questions in 'comp.lang.c.moderated', it
> is a mostly quiet group but one where questions get answered.

Bill has supposedly been learning C for the better part of a decade. Over
the course of numerous threads during that time, his knowledge of the
language and specific parts of the language have waxed and waned. He will
use some function correctly in one program, then a month later use it with
the wrong inputs, or even claim to be unaware of it.

Many of us have come to the conclusion that it's a long-con troll. If it's
not, then his capacity for learning the language simply doesn't exist. He
demonstrates less capability than a first-sememster student at mid-terms.
Either way, a waste of time to respond.

Brian


Keith Thompson

unread,
May 4, 2011, 1:52:25 PM5/4/11
to

He's said in the past that it's due to side effects of some
medication he's on. Either it's true, and he's (sadly) wasting his
time, or it's not, and he's (annoyingly) wasting ours. (I'm aware
that some people here dismiss the former possibility; I don't care
to debate the point.)

If Bill really wants to learn programming, I suggest that C may be one
of the worst possible languages for him to use. Of the languages I'm
familiar with, Python might be a much better choice.

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Joe Wright

unread,
May 4, 2011, 2:57:21 PM5/4/11
to
On 5/4/2011 13:52, Keith Thompson wrote:
> "Default User"<defaul...@yahoo.com> writes:
>> "Mickey Mouse"<fa...@email.com> wrote in message
>> news:kh8ur6hs3huci2rqq...@4ax.com...
>>> You might be better posting questions in 'comp.lang.c.moderated', it
>>> is a mostly quiet group but one where questions get answered.
>>
>> Bill has supposedly been learning C for the better part of a decade. Over
>> the course of numerous threads during that time, his knowledge of the
>> language and specific parts of the language have waxed and waned. He will
>> use some function correctly in one program, then a month later use it with
>> the wrong inputs, or even claim to be unaware of it.
>>
>> Many of us have come to the conclusion that it's a long-con troll. If it's
>> not, then his capacity for learning the language simply doesn't exist. He
>> demonstrates less capability than a first-sememster student at mid-terms.
>> Either way, a waste of time to respond.
>
> He's said in the past that it's due to side effects of some
> medication he's on. Either it's true, and he's (sadly) wasting his
> time, or it's not, and he's (annoyingly) wasting ours. (I'm aware
> that some people here dismiss the former possibility; I don't care
> to debate the point.)
>
> If Bill really wants to learn programming, I suggest that C may be one
> of the worst possible languages for him to use. Of the languages I'm
> familiar with, Python might be a much better choice.
>
I got involved with Bill several years ago here in clc. I am not a doctor.
Bill's condition plus his medication causes severe memory loss. He couldn't
tell you what he had for lunch yesterday. Bill is not a troll. My take on
him is that he is 'trying to get better' and using usenet and comp.lang.c
to do it. I can't fault him for that.

Replying to his quests here is kind if not helpful to him. Your reply to
Bill might be helpful to other myriad readers here. Don't stop.

--
Joe Wright
"If you rob Peter to pay Paul you can depend on the support of Paul."

Bill Cunningham

unread,
May 4, 2011, 6:53:33 PM5/4/11
to
Keith Thompson wrote:

[...]

> If Bill really wants to learn programming, I suggest that C may be one
> of the worst possible languages for him to use. Of the languages I'm
> familiar with, Python might be a much better choice.

I have looked at python and it totally blows my mind. Just looking at
the code mind you and not trying to learn. I have read so many times the C
is easy to learn. I've looked at C++ and it is alittle more understnadable
to me. clc is the only place I know to go if I have any C questions. I know
nothing about algorithms and computer science. I have no college training.
I just want to learn a language. I claim nothing more. I know how to use
functions half wy decent in C but coding for results is beyond me. Right now
anyway. I hope that changes some day.

Bill


Angel

unread,
May 5, 2011, 4:12:56 AM5/5/11
to

While the syntax of C is fairly simple, the language allows way too much
freedom that can result in bad code or subtle bugs, so IMHO it is not
the best language for someone completely new to programming. For
learning purposes, a strongly typed and rigidly structured language
might be better.

I started with Pascal myself back in high school (okay, I did C64 BASIC
before that), but nowadays Java seems to be the educational language of
choice. If you've seen C++, Java will look very familiar.

If you still want to learn C, well the FAQ for this newsgroup holds a
lot of answers to common questions, take a look there. There are also
free online courses available, just Google a bit for them.

Of course, like with so many computer things, the best way to learn it
is to play with it. Start with small things, like for example a program
that works like the Unix command "cat", and work your way up from there.

0 new messages