Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #3087 > unrolled thread

malloc

Started by"Bill Cunningham" <nospam@nspam.invalid>
First post2011-05-01 01:01 -0400
Last post2011-05-03 21:50 +0100
Articles 20 on this page of 42 — 16 participants

Back to article view | Back to comp.lang.c


Contents

  malloc "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-01 01:01 -0400
    Re: malloc "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-01 01:03 -0400
    Re: malloc luser- -droog <mijoryx@yahoo.com> - 2011-04-30 22:31 -0700
      Re: malloc "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-01 02:15 -0400
      Re: malloc "BartC" <bc@freeuk.com> - 2011-05-01 13:52 +0100
        Re: malloc luser- -droog <mijoryx@yahoo.com> - 2011-05-02 00:05 -0700
          Re: malloc crisgoogle <crisd@telus.net> - 2011-05-02 17:43 -0700
    Re: malloc Ian Collins <ian-news@hotmail.com> - 2011-05-01 17:35 +1200
    Re: malloc "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-01 02:14 -0400
      Re: malloc Mickey Mouse <fake@email.com> - 2011-05-01 00:18 -0700
        Re: malloc "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-01 04:07 -0400
          Re: malloc Barry Schwarz <schwarzb@dqel.com> - 2011-05-01 10:31 -0700
            Re: malloc "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-02 16:55 -0400
            Re: malloc "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-02 23:51 -0400
        Re: malloc "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-02 16:58 -0400
          Re: malloc Mickey Mouse <fake@email.com> - 2011-05-02 14:34 -0700
            Re: malloc Ian Collins <ian-news@hotmail.com> - 2011-05-03 09:55 +1200
              Re: malloc "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-03 10:20 -0400
                Re: malloc Angel <angel+news@spamcop.net> - 2011-05-03 14:55 +0000
                  Re: malloc "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-03 15:34 -0400
                    Re: malloc Angel <angel+news@spamcop.net> - 2011-05-03 20:56 +0000
              Re: malloc "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-03 11:29 -0400
            Re: malloc Seebs <usenet-nospam@seebs.net> - 2011-05-02 22:18 +0000
            Re: malloc "Default User" <defaultuserbr@yahoo.com> - 2011-05-04 12:20 -0500
              Re: malloc Keith Thompson <kst-u@mib.org> - 2011-05-04 10:52 -0700
                Re: malloc Joe Wright <joewwright@comcast.net> - 2011-05-04 14:57 -0400
                Re: malloc "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-04 18:53 -0400
                  Re: malloc Angel <angel+news@spamcop.net> - 2011-05-05 08:12 +0000
      Re: malloc John Gordon <gordon@panix.com> - 2011-05-02 15:31 +0000
    Re: malloc "osmium" <r124c4u102@comcast.net> - 2011-05-01 09:56 -0500
      Re: malloc "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-01 18:20 -0400
      Re: malloc "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-02 21:38 -0400
        Re: malloc "osmium" <r124c4u102@comcast.net> - 2011-05-03 09:02 -0500
          Re: malloc "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-03 15:21 -0400
            Re: malloc "osmium" <r124c4u102@comcast.net> - 2011-05-03 14:35 -0500
              Re: malloc "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-03 18:43 -0400
    Re: malloc Tim Rentsch <txr@alumni.caltech.edu> - 2011-05-01 12:51 -0700
      Re: malloc "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-02 23:49 -0400
        Re: malloc Ian Collins <ian-news@hotmail.com> - 2011-05-03 13:35 +1200
          Re: malloc "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-03 11:27 -0400
            Re: malloc Ian Collins <ian-news@hotmail.com> - 2011-05-04 07:54 +1200
            Re: malloc Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-05-03 21:50 +0100

Page 1 of 3  [1] 2 3  Next page →


#3087 — malloc

From"Bill Cunningham" <nospam@nspam.invalid>
Date2011-05-01 01:01 -0400
Subjectmalloc
Message-ID<4dbce936$0$8355$bbae4d71@news.suddenlink.net>
    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

[toc] | [next] | [standalone]


#3088

From"Bill Cunningham" <nospam@nspam.invalid>
Date2011-05-01 01:03 -0400
Message-ID<4dbce99e$0$8342$bbae4d71@news.suddenlink.net>
In reply to#3087
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

[toc] | [prev] | [next] | [standalone]


#3090

Fromluser- -droog <mijoryx@yahoo.com>
Date2011-04-30 22:31 -0700
Message-ID<baab1bba-f3f2-40da-a179-929e02556344@c41g2000yqm.googlegroups.com>
In reply to#3087
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.              |
,___________________________|

[toc] | [prev] | [next] | [standalone]


#3093

From"Bill Cunningham" <nospam@nspam.invalid>
Date2011-05-01 02:15 -0400
Message-ID<4dbcfa66$0$8372$bbae4d71@news.suddenlink.net>
In reply to#3090
luser- -droog wrote:
> 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.              |
> ,___________________________|

    Sorry.

[toc] | [prev] | [next] | [standalone]


#3101

From"BartC" <bc@freeuk.com>
Date2011-05-01 13:52 +0100
Message-ID<ipjl34$m79$1@dont-email.me>
In reply to#3090

"luser- -droog" <mijoryx@yahoo.com> wrote in message 
news:baab1bba-f3f2-40da-a179-929e02556344@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?


-- 
Bartc 

[toc] | [prev] | [next] | [standalone]


#3121

Fromluser- -droog <mijoryx@yahoo.com>
Date2011-05-02 00:05 -0700
Message-ID<85a35186-9db7-4a47-8b28-4f15a448f0aa@r20g2000yqd.googlegroups.com>
In reply to#3101

BartC wrote:
> "luser- -droog" <mijoryx@yahoo.com> wrote in message
> news:baab1bba-f3f2-40da-a179-929e02556344@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...)

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

--
lothario draganeti

[toc] | [prev] | [next] | [standalone]


#3148

Fromcrisgoogle <crisd@telus.net>
Date2011-05-02 17:43 -0700
Message-ID<3edd6a94-a925-4863-ba96-c2e0e03ca0fe@k15g2000pri.googlegroups.com>
In reply to#3121
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-a179-929e02556344@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))

=)

[toc] | [prev] | [next] | [standalone]


#3091

FromIan Collins <ian-news@hotmail.com>
Date2011-05-01 17:35 +1200
Message-ID<9249ouFj03U5@mid.individual.net>
In reply to#3087
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

[toc] | [prev] | [next] | [standalone]


#3092

From"Bill Cunningham" <nospam@nspam.invalid>
Date2011-05-01 02:14 -0400
Message-ID<4dbcfa24$0$8384$bbae4d71@news.suddenlink.net>
In reply to#3087
    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

[toc] | [prev] | [next] | [standalone]


#3094

FromMickey Mouse <fake@email.com>
Date2011-05-01 00:18 -0700
Message-ID<h92qr659kufv4nhsdonmih3ved15avvren@4ax.com>
In reply to#3092
On Sun, 1 May 2011 02:14:03 -0400, "Bill Cunningham"
<nospam@nspam.invalid> wrote:

>
>    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
>


realloc

[toc] | [prev] | [next] | [standalone]


#3095

From"Bill Cunningham" <nospam@nspam.invalid>
Date2011-05-01 04:07 -0400
Message-ID<4dbd14c8$0$8369$bbae4d71@news.suddenlink.net>
In reply to#3094
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

[toc] | [prev] | [next] | [standalone]


#3106

FromBarry Schwarz <schwarzb@dqel.com>
Date2011-05-01 10:31 -0700
Message-ID<s44rr6d0kb9l82mcvva68u06lnspq0dcea@4ax.com>
In reply to#3095
On Sun, 1 May 2011 04:07:43 -0400, "Bill Cunningham"
<nospam@nspam.invalid> wrote:

>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.

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

[toc] | [prev] | [next] | [standalone]


#3136

From"Bill Cunningham" <nospam@nspam.invalid>
Date2011-05-02 16:55 -0400
Message-ID<4dbf1a33$0$15061$bbae4d71@news.suddenlink.net>
In reply to#3106
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

[toc] | [prev] | [next] | [standalone]


#3151

From"Bill Cunningham" <nospam@nspam.invalid>
Date2011-05-02 23:51 -0400
Message-ID<4dbf5170$0$15089$bbae4d71@news.suddenlink.net>
In reply to#3106
"Barry Schwarz" <schwarzb@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
---
    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.

B


[toc] | [prev] | [next] | [standalone]


#3137

From"Bill Cunningham" <nospam@nspam.invalid>
Date2011-05-02 16:58 -0400
Message-ID<4dbf1acc$0$15082$bbae4d71@news.suddenlink.net>
In reply to#3094
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

[toc] | [prev] | [next] | [standalone]


#3139

FromMickey Mouse <fake@email.com>
Date2011-05-02 14:34 -0700
Message-ID<kh8ur6hs3huci2rqq66aunn0f7rgd4kv4h@4ax.com>
In reply to#3137
On Mon, 2 May 2011 16:58:00 -0400, "Bill Cunningham"
<nospam@nspam.invalid> wrote:

>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
>

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.

[toc] | [prev] | [next] | [standalone]


#3140

FromIan Collins <ian-news@hotmail.com>
Date2011-05-03 09:55 +1200
Message-ID<928ni9Fj03U9@mid.individual.net>
In reply to#3139
On 05/ 3/11 09:34 AM, Mickey Mouse wrote:
> On Mon, 2 May 2011 16:58:00 -0400, "Bill Cunningham"
> <nospam@nspam.invalid>  wrote:
>
>> 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
>>
>
> 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.

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

Go figure.

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#3177

From"Bill Cunningham" <nospam@nspam.invalid>
Date2011-05-03 10:20 -0400
Message-ID<4dc00f16$0$5074$bbae4d71@news.suddenlink.net>
In reply to#3140
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

[toc] | [prev] | [next] | [standalone]


#3179

FromAngel <angel+news@spamcop.net>
Date2011-05-03 14:55 +0000
Message-ID<slrnis05re.k9c.angel+news@pearlgates.net>
In reply to#3177
On 2011-05-03, Bill Cunningham <nospam@nspam.invalid> wrote:
> 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.

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

[toc] | [prev] | [next] | [standalone]


#3191

From"Bill Cunningham" <nospam@nspam.invalid>
Date2011-05-03 15:34 -0400
Message-ID<4dc058b8$0$5086$bbae4d71@news.suddenlink.net>
In reply to#3179
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

[toc] | [prev] | [next] | [standalone]


Page 1 of 3  [1] 2 3  Next page →

Back to top | Article view | comp.lang.c


csiph-web