malloc

  • Thread starter Bill Cunningham
  • Start date
B

Bill Cunningham

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
 
B

Bill Cunningham

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
 
L

luser- -droog

    I have this unfinished untested code that I would like to enquire
^
You need a comma between |
two adjectives that modify |
the same noun. |
,___________________________|
 
I

Ian Collins

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

Bill Cunningham

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
 
M

Mickey Mouse

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
 
B

Bill Cunningham

Mickey said:

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
 
O

osmium

Bill Cunningham said:
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.
 
B

Barry Schwarz

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

Tim Rentsch

Bill Cunningham said:
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.
 
B

Bill Cunningham

osmium said:
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
 
L

luser- -droog

BartC said:
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.
 
J

John Gordon

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

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?

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

Bill Cunningham

Barry said:
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
 
M

Mickey Mouse

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

Ian Collins

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

Seebs

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Linux: using "clone3" and "waitid" 0
malloc of large string works, but same sized array fails 9
URGENT 1
malloc and maximum size 56
write error 13
Working with files 1
wcstombs() problem 16
C pipe 1

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top