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


Groups > comp.lang.c > #3105

Re: malloc

From "osmium" <r124c4u102@comcast.net>
Newsgroups comp.lang.c
Subject Re: malloc
Date 2011-05-01 09:56 -0500
Message-ID <925almFnc4U1@mid.individual.net> (permalink)
References <4dbce936$0$8355$bbae4d71@news.suddenlink.net>

Show all headers | View raw


"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.
>
> #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?

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.


Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web