malloc

  • Thread starter Bill Cunningham
  • Start date
C

crisgoogle

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

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

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
 
B

Bill Cunningham

Tim Rentsch said:
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
 
B

Bill Cunningham

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

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
 
O

osmium

Bill Cunningham said:
Why?

The file's name is specified by a command line

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 ?

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

Bill Cunningham

Ian said:
"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
 
A

Angel

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

Bill Cunningham

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

Bill Cunningham

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
 
O

osmium

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

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

Ben Bacarisse

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

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

Angel

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.

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;
}
 
B

Bill Cunningham

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

Default User

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
 
K

Keith Thompson

Default User said:
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.
 
B

Bill Cunningham

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
 

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
474,085
Messages
2,570,597
Members
47,220
Latest member
AugustinaJ

Latest Threads

Top