473,796 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

memfrob and strfry ? OT

My string.h headers declares two functions I have been using called
memfrob and strfry. They are encryption types functions. My man pages say
they are standard to linux c and gnu c. They sure aren't in my C books.
Interesting functions by they're OT here but this raises to me a question.
If a function returns a pointer to a void and and as it's first parameter a
pointer to a void. Then should that function's first parameter accept a
string or pointer to char or any other data type since the parameter is
declared a void * ?

Bill
Jun 27 '08
35 5688

"santosh" <sa*********@gm ail.comwrote in message
news:g1******** **@registered.m otzarella.org.. .
#include <stdio.h>
#include <stdlib.h>

int main(void) {
void *t, *p, **pp;
size_t sz;

p = malloc(1);
for (sz = 2; sz < 100; sz++) {
pp = p;
p = realloc(p, sz);
printf("p was %p, is %p at sz = %lu\n",
pp, p, (unsigned long)sz);
}
return 0;
}
That is much better. I guess the only thing I don't see is the pupose
for the pp=p; statement.

Bill
Jun 27 '08 #31
On May 23, 2:15*pm, santosh <santosh....@gm ail.comwrote:
>
#include <stdio.h>
#include <stdlib.h>

int main(void) {
* void *t, *p, **pp;
* size_t sz;

* p = malloc(1);
* for (sz = 2; sz < 100; sz++) {
* * pp = p;
* * p = realloc(p, sz);
* * printf("p was %p, is %p at sz = %lu\n",
* * * * * * * *pp, * * * *p, * (unsigned long)sz);
While I understand the intent here in replying to Bill C., it should
be noted that if realloc causes the allocated area to move (p != pp),
then this code invokes undefined behavior. The standard states that
the value of a pointer (pp in this case) becomes indeterminate when
the object it points to (in this case the allocated memory) ceases to
exist. Any attempt to evaluate an indeterminate value is prohibited.
* }
* return 0;
Jun 27 '08 #32

"Keith Thompson" <ks***@mib.orgw rote in message
news:ln******** ****@nuthaus.mi b.org...
malloc() allocates a chunk of memory. realloc() resizes a chunk of
memory that's already been allocated, possibly copying it to a new
location. free() deallocations a chunk of memory that was allocated
by malloc() and/or realloc().
So that's what they do ok.
What exactly are you trying to accomplish?
Insead of reading from ram read into ram a file of data.

Bill

Jun 27 '08 #33
On May 24, 11:16*am, "Bill Cunningham" <nos...@nspam.c omwrote:
>
What exactly are you trying to accomplish?

* * Insead of reading from ram read into ram a file of data.
To read from a file into memory, you first must define or allocate a
buffer. It can be as small as a single char or as large as your
system will allow. A typical example would be an array of char.

Before you can read, you must open the file usign fopen. After
checking that fopen succeeded, you have several otions to actually
transfer the data. fgetc will transfer a single character. fgets
will real a line if the fileis text. fread will read a block of
data. There are others. You chose the one that suits your needs.
When you are done processing the file, you close it with fclose.
Jun 27 '08 #34

"santosh" <sa*********@gm ail.comwrote in message
news:g1******** **@registered.m otzarella.org.. .
#include <stdio.h>
#include <stdlib.h>

int main(void) {
void *t, *p, **pp;
size_t sz;

p = malloc(1);
for (sz = 2; sz < 100; sz++) {
pp = p;
p = realloc(p, sz);
printf("p was %p, is %p at sz = %lu\n",
pp, p, (unsigned long)sz);
}
return 0;
}
I'm beginning to see what pointers are for and it's new to me at this
stage. Pointers in the past to me have just been another way to use arrays.
Can I have an example of malloc by itself? Or is it not used by itself? For
what reasons would you use malloc?

Bill
Jun 27 '08 #35
Bill Cunningham wrote:
>
"santosh" <sa*********@gm ail.comwrote in message
news:g1******** **@registered.m otzarella.org.. .
>#include <stdio.h>
#include <stdlib.h>

int main(void) {
void *t, *p, **pp;
size_t sz;

p = malloc(1);
for (sz = 2; sz < 100; sz++) {
pp = p;
p = realloc(p, sz);
printf("p was %p, is %p at sz = %lu\n",
pp, p, (unsigned long)sz);
}
return 0;
}

I'm beginning to see what pointers are for and it's new to me at
this
stage. Pointers in the past to me have just been another way to use
arrays. Can I have an example of malloc by itself?
Sure:

int *blk;

blk = malloc(ELEMENTS * sizeof *blk);
/* In serious program you must check the return value
of malloc before proceeding. I omitted it here because
you have previously expressed difficulty understanding code
with error checking included.
*/

Here we have called malloc to attempt an allocation of ELEMENTS*sizeof
*blk bytes of memory. ELEMENTS must be defined and set to an
appropriate value previously. After the function has succeeded you
have 'blk' pointing to a contiguous sequence of the amount of bytes of
memory you passed to malloc. By accessing them through 'blk' they are
treated as an array of ELEMENTS ints. You could've also assigned the
return value to another type of pointer (say double or unsigned char)
in which case the memory is treated as a sequence of objects of the
appropriate type. This is done automatically by your compiler.
Or is it not used
by itself?
It's often used by itself.
For what reasons would you use malloc?
Malloc is used when you need to allocate storage during runtime, i.e.,
you do not know how much memory you will need until after the program
is running (for example it might depend on user or other device input).
Statically allocated memory is out-of-question while VLA have other
drawbacks (they have a smaller scope and lifetime and they is no
portable way to detect or recover from a VLA allocation failure).

Also dynamically allocated memory persists for exactly as long as you
want, no more no less. Global objects persist throughout the program's
lifetime whether or not you want that while local objects are destroyed
when their scope is exited, and again there is nothing you can do about
that. Memory derived through malloc (or realloc or calloc) is available
until you deallocate it with free or until the program terminates.

Jun 27 '08 #36

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.