[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

malloc

Bill Cunningham

5/1/2011 5:02:00 AM

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


41 Answers

Bill Cunningham

5/1/2011 5:04:00 AM

0

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


luserXtrog

5/1/2011 5:32:00 AM

0

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

5/1/2011 5:35:00 AM

0

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

5/1/2011 6:14:00 AM

0


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

5/1/2011 6:15:00 AM

0

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.


phrogg

5/1/2011 7:19:00 AM

0

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

Bill Cunningham

5/1/2011 8:08:00 AM

0

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

5/1/2011 12:52:00 PM

0



"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

osmium

5/1/2011 2:57:00 PM

0

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



Barry Schwarz

5/1/2011 5:31:00 PM

0

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