Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > malloc questions

Reply
Thread Tools

malloc questions

 
 
Jack
Guest
Posts: n/a
 
      06-02-2006
The code is below:

char *p;
p = (char*)malloc(10 * sizeof(char)); //LINE1

After LINE1, the 10 char-length memory that p points to is empty or
some random characters?

Thanks.

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      06-02-2006
Jack said:

> The code is below:
>
> char *p;
> p = (char*)malloc(10 * sizeof(char)); //LINE1


Better: p = malloc(10 * sizeof *p);

> After LINE1, the 10 char-length memory that p points to is empty or
> some random characters?


The values of the ten bytes, starting with the byte pointed to by p, are
indeterminate. That means you can't rely on their having meaningful values.

I am reluctant to accede to the use of the word "random", in case it gives
you the idea that you can get entropy from those bytes. You certainly can't
do that reliably.

The safe, portable assumption to make is that you must not /read/ from those
bytes unless you have first /written/ values to them.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
 
 
 
Tomás
Guest
Posts: n/a
 
      06-02-2006
Jack posted:

> The code is below:
>
> char *p;
> p = (char*)malloc(10 * sizeof(char)); //LINE1
>
> After LINE1, the 10 char-length memory that p points to is empty or
> some random characters?



Random characters -- and you've no guarantee that there's a terminating
null character, so don't do the following:

char * const p = malloc(10);

printf("%s", p);


"calloc" on the other hand will set all bits to zero.

If you want to dynamically allocate an array of doubles, or perhaps an
array of pointers, you'll have to manually set each element to zero:

#include <...

int main(void)
{
enum { len = 45 };

double *p = malloc( sizeof( double[len] ) );


const double * const p_over = p + len;

do
{
*p++ = 0;
} while ( p != p_over );

}


I suppose you could make a function for creating an array of objects with
"default" values:


typedef char T;

T * const allocdef( unsigned const amount_elem )
{
T * const p = malloc( sizeof( T[amount_elem] ) );

const T * const p_over = p + amount_elem;

do
{
*p++ = 0;
} while ( p != p_over );
}


-Tomás
 
Reply With Quote
 
Gordon Burditt
Guest
Posts: n/a
 
      06-02-2006
>char *p;
>p = (char*)malloc(10 * sizeof(char)); //LINE1
>
>After LINE1, the 10 char-length memory that p points to is empty or
>some random characters?


memory is never "empty".

There is no guarantee what is in the memory you malloc() and you
shouldn't read it until you've put something in it. It is certainly
not guaranteed to contain cryptographically-secure random numbers,
nor is it guaranteed to be initialized to all-bits 0xdeadbeef.
"garbage" is probably a better description. If you want it initialized
to all-bits-zero, use calloc().

Gordon L. Burditt
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      06-02-2006
"Jack" <> wrote:

> char *p;
> p = (char*)malloc(10 * sizeof(char)); //LINE1


This is not the best way to call malloc().

First, if you have a proper prototype in scope, the cast does nothing.
OTOH, if you do not have a proper prototype in scope, the cast is likely
to hide any error messages your compiler might have given you to warn
you of this mistake, and the result is likely to be garbage. Lose the
cast.
Second, sizeof(char) is always 1. This is guaranteed by the Standard. If
you know that p is always going to be a char * (i.e., it holds a
string), you can omit the sizeof(char).
OTOH, if you do _not_ know that p is always going to be a char * (e.g.,
it holds flags, and it may some day become a struct flags * instead),
then it should be treated as a normal malloc() call. Any normal malloc()
call is easier to maintain properly if you do not ask for N*sizeof(type)
but for N*sizeof *pointer. When the type of the pointer changes,
N*sizeof(type) may be incorrect and need changing; but N*sizeof *pointer
is always right no matter what type pointer has.
Also, double spacing is an abomination concocted by a conspiracy of
typewriter makers and stationers. Also also, magic numbers are to be
abhorred.

All in all, your code is better this way:

#define MAX_LENGTH 10

char *p;
p = malloc(MAX_LENGTH * sizeof *p);


> After LINE1, the 10 char-length memory that p points to is empty or
> some random characters?


The latter.

Richard
 
Reply With Quote
 
Simon Biber
Guest
Posts: n/a
 
      06-02-2006
Tomás wrote:
> I suppose you could make a function for creating an array of objects with
> "default" values:
>
>
> typedef char T;
>
> T * const allocdef( unsigned const amount_elem )
> {
> T * const p = malloc( sizeof( T[amount_elem] ) );


The line above defines p is a const variable pointing at non-const data.
You cannot modify p, but you may modify *p. The operand of sizeof is a
variable-length array type, which is valid in C99 but not in C89. You
should check whether malloc returned NULL.

> const T * const p_over = p + amount_elem;


p_over is a const variable pointing at const data. You can neither
modify p_over or *p_over.

> do
> {
> *p++ = 0;


The line above attempts to modify both p and *p. That is not allowed, as
p is const. The compiler should diagnose this error.

> } while ( p != p_over );


You failed to return a value from this function. It would be unusual to
return p at this point, as it points one element past the end of the
allocated buffer.

> }
>
>
> -Tom嫳


That's an interesting Chinese character in your name. It's there because
you didn't include a Content-type header in your post. Your news client
should be configured to put:

Content-Type: text/plain; charset="iso-8859-1"

Otherwise, news readers have no way to tell what encoding you really
intended. My one erroneously detected Big5, a legacy encoding for
traditional Chinese characters.

In my opinion it's bad form to post non-ASCII characters to newsgroups
without a valid Content-type header.

--
Simon.
 
Reply With Quote
 
Haider
Guest
Posts: n/a
 
      06-02-2006

Richard Bos wrote:
> "Jack" <> wrote:
>
> > char *p;
> > p = (char*)malloc(10 * sizeof(char)); //LINE1

>
> This is not the best way to call malloc().
>
> First, if you have a proper prototype in scope, the cast does nothing.
> OTOH, if you do not have a proper prototype in scope, the cast is likely
> to hide any error messages your compiler might have given you to warn
> you of this mistake, and the result is likely to be garbage. Lose the
> cast.
> Second, sizeof(char) is always 1. This is guaranteed by the Standard. If
> you know that p is always going to be a char * (i.e., it holds a
> string), you can omit the sizeof(char).
> OTOH, if you do _not_ know that p is always going to be a char * (e.g.,
> it holds flags, and it may some day become a struct flags * instead),
> then it should be treated as a normal malloc() call. Any normal malloc()
> call is easier to maintain properly if you do not ask for N*sizeof(type)
> but for N*sizeof *pointer. When the type of the pointer changes,
> N*sizeof(type) may be incorrect and need changing; but N*sizeof *pointer
> is always right no matter what type pointer has.
> Also, double spacing is an abomination concocted by a conspiracy of
> typewriter makers and stationers. Also also, magic numbers are to be
> abhorred.
>
> All in all, your code is better this way:
>
> #define MAX_LENGTH 10
>
> char *p;
> p = malloc(MAX_LENGTH * sizeof *p);

above lines of code gives compilation error on MS VC++ 6.0
so i think cast has to be there

> > After LINE1, the 10 char-length memory that p points to is empty or
> > some random characters?

>
> The latter.
>
> Richard


 
Reply With Quote
 
Vladimir Oka
Guest
Posts: n/a
 
      06-02-2006

Haider wrote:
> Richard Bos wrote:
> >
> > char *p;
> > p = malloc(MAX_LENGTH * sizeof *p);

> above lines of code gives compilation error on MS VC++ 6.0
> so i think cast has to be there


Try using it in C mode.

Cast is only required in C++ (even then, you'd probably really want to
use `new`).

 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      06-02-2006
"Haider" <> wrote:

[ Snip! ]

> Richard Bos wrote:
> > "Jack" <> wrote:
> >
> > > char *p;
> > > p = (char*)malloc(10 * sizeof(char)); //LINE1

> >
> > This is not the best way to call malloc().
> >
> > First, if you have a proper prototype in scope, the cast does nothing.
> > OTOH, if you do not have a proper prototype in scope, the cast is likely
> > to hide any error messages your compiler might have given you to warn
> > you of this mistake, and the result is likely to be garbage. Lose the
> > cast.


> > char *p;
> > p = malloc(MAX_LENGTH * sizeof *p);

> above lines of code gives compilation error on MS VC++ 6.0


Use a real C compiler, then, not a pink-elephant compiler forced to work
on C code.

> so i think cast has to be there


You think wrongly.

Richard
 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      06-02-2006
Haider wrote:

> Richard Bos wrote:
>> char *p;
>> p = malloc(MAX_LENGTH * sizeof *p);

> above lines of code gives compilation error on MS VC++ 6.0
> so i think cast has to be there


Why are you compiling /C/ code with a /C++/ compiler? Use a C
compiler.

(Or write idiomatic malloc-free C++.)

--
Chris "seeker" Dollin
"Never ask that question!" Ambassador Kosh, /Babylon 5/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
to malloc or not to malloc?? Johs32 C Programming 4 03-30-2006 10:03 AM
porting non-malloc code to malloc micromysore@gmail.com C Programming 3 02-19-2005 05:39 AM
Malloc/Free - freeing memory allocated by malloc Peter C Programming 34 10-22-2004 10:23 AM
free'ing malloc'd structure with malloc'd members John C Programming 13 08-02-2004 11:45 AM
Re: free'ing malloc'd structure with malloc'd members ravi C Programming 0 07-30-2004 12:42 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57