Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > allocating memory for pointee

Reply
Thread Tools

allocating memory for pointee

 
 
Barry Schwarz
Guest
Posts: n/a
 
      01-17-2013
On Thu, 17 Jan 2013 06:49:11 -0500, "Bill Cunningham"
<(E-Mail Removed)> wrote:

>Shao Miller wrote:
>> On 1/16/2013 19:10, Bill Cunningham wrote:
>>> Shao Miller wrote:
>>>
>>>> Or you could add this:
>>>>
>>>> x = malloc(sizeof *x);

><snip>
>
>Does the * in front of the x indicate that x is a pointer? You do not want
>just x then or the compiler wouldn't know it's a pointer?


There is a difference in meaning between the use of the * in a
declaration and in a statement.

In a declaration such as
int *y;
the * indicates that y is indeed a pointer.

In a statement such as you quoted, the * is the unary indirection
operator. This operator requires that its operand be a pointer but it
does not mean that the operand is a pointer. If you mistakenly apply
this operator to a non-pointer operand, it does not magically make the
operand a pointer. Instead you have a syntax error.

And of course you also have to distinguish between the unary
indirection operator and the binary multiplication operator.

Consider
int a = 1;
int b = 2;
int *p = &a;
int c = b * *p;
All three asterisks mean something different.

--
Remove del for email
 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      01-17-2013
On 1/16/2013 4:10 PM, Bill Cunningham wrote:
> Shao Miller wrote:
>
>> Or you could add this:
>>
>> x = malloc(sizeof *x);
>>
>> That is a touch more precise than the former, because it says "Try to
>> allocate enough storage for the pointee of 'x'," rather than "try to
>> allocate enough storage for an 'int'" (which so happens to be the
>> right amount of storage).
>>
>> Using this form, if you decide to change 'int *x;' to 'long *x;' some
>> day, you don't need to change the line with the 'malloc'.

>
> Oh I see yes. That makes sense. Is that proper style here?


It is just one possible style. And a good one at that. In general I
would prefer to make my code as type-independent as possible. I.e. the
guideline I'd follow is that type names belong in declarations. If
something is not a declaration, it is best to avoid any explicit
references to type names as much as possible.

Under this guideline this is "bad"

x = (int *) malloc(n * sizeof(int));

and this is "good"

x = malloc(n * sizeof *x);

But in the end it is matter of your personal preference.

--
Best regards,
Andrey Tarasevich

 
Reply With Quote
 
 
 
 
Bill Cunningham
Guest
Posts: n/a
 
      01-17-2013
Shao Miller wrote:
> On 1/17/2013 08:21, Bill Cunningham wrote:
>> Shao Miller wrote:
>>> On 1/17/2013 06:49, Bill Cunningham wrote:
>>>> Shao Miller wrote:
>>>>> On 1/16/2013 19:10, Bill Cunningham wrote:
>>>>>> Shao Miller wrote:
>>>>>>
>>>>>>> Or you could add this:
>>>>>>>
>>>>>>> x = malloc(sizeof *x);
>>>> <snip>
>>>>
>>>> Does the * in front of the x indicate that x is a pointer? You do
>>>> not want just x then or the compiler wouldn't know it's a pointer?
>>>>
>>>>
>>>
>>> There are two kinds of '*' operator:
>>>
>>> - The unary indirection operator
>>> - The binary multiplication operator
>>>
>>> the one that's being used above is the unary indirection operator.
>>> For any expression 'E' that has a pointer type, the expression '*E'
>>> means: The pointee of 'E'.
>>>
>>> Your declaration of 'x' was:
>>>
>>> int *x;
>>>
>>> So in a later expression, 'x' has a pointer type. Thus, the
>>> expression '*x' means: The pointee of 'x'.
>>>
>>> So 'sizeof *x' means: The size of the pointee of 'x'.
>>>
>>> '*x' doesn't tell the compiler that 'x' is a pointer. Your
>>> declaration of 'x' told the compiler that 'x' is a pointer.

>>
>> So for a function that takes a pointer you don't need to pass
>> that * operator right?
>>

>
> You don't pass an operator, ever.
>
>> void func(void *);
>> would be void func(x); for example. I know you could also use
>>
>> void func(&x);
>> And bypass the pointer altogether and pass the object pointed to
>> (pointee) right?
>>

>
> That second one is not C, so you're mistaken.


In the example of scanf which takes a pointer I am thinking you use the
& operator. What's the difference with the second example I gave?

Bill


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      01-17-2013
Barry Schwarz wrote:
> On Thu, 17 Jan 2013 06:49:11 -0500, "Bill Cunningham"
> <(E-Mail Removed)> wrote:
>
>> Shao Miller wrote:
>>> On 1/16/2013 19:10, Bill Cunningham wrote:
>>>> Shao Miller wrote:
>>>>
>>>>> Or you could add this:
>>>>>
>>>>> x = malloc(sizeof *x);

>> <snip>
>>
>> Does the * in front of the x indicate that x is a pointer? You do
>> not want just x then or the compiler wouldn't know it's a pointer?

>
> There is a difference in meaning between the use of the * in a
> declaration and in a statement.
>
> In a declaration such as
> int *y;
> the * indicates that y is indeed a pointer.
>
> In a statement such as you quoted, the * is the unary indirection
> operator. This operator requires that its operand be a pointer but it
> does not mean that the operand is a pointer. If you mistakenly apply
> this operator to a non-pointer operand, it does not magically make the
> operand a pointer. Instead you have a syntax error.
>
> And of course you also have to distinguish between the unary
> indirection operator and the binary multiplication operator.
>
> Consider
> int a = 1;
> int b = 2;
> int *p = &a;
> int c = b * *p;
> All three asterisks mean something different.


What I seem to be confused about is when the unary operator is needed
and when it isn't.

FILE *fp;
fp=fopen()...
not *fp=fopen()...
And in the example I Shao gave,

x=malloc(sizeof *x);
x=malloc(sizeof x); WRONG

Bill


 
Reply With Quote
 
Shao Miller
Guest
Posts: n/a
 
      01-17-2013
On 1/17/2013 17:46, Bill Cunningham wrote:
> Shao Miller wrote:
>> On 1/17/2013 08:21, Bill Cunningham wrote:
>>> Shao Miller wrote:
>>>> On 1/17/2013 06:49, Bill Cunningham wrote:
>>>>> Shao Miller wrote:
>>>>>> On 1/16/2013 19:10, Bill Cunningham wrote:
>>>>>>> Shao Miller wrote:
>>>>>>>
>>>>>>>> Or you could add this:
>>>>>>>>
>>>>>>>> x = malloc(sizeof *x);
>>>>> <snip>
>>>>>
>>>>> Does the * in front of the x indicate that x is a pointer? You do
>>>>> not want just x then or the compiler wouldn't know it's a pointer?
>>>>>
>>>>>
>>>>
>>>> There are two kinds of '*' operator:
>>>>
>>>> - The unary indirection operator
>>>> - The binary multiplication operator
>>>>
>>>> the one that's being used above is the unary indirection operator.
>>>> For any expression 'E' that has a pointer type, the expression '*E'
>>>> means: The pointee of 'E'.
>>>>
>>>> Your declaration of 'x' was:
>>>>
>>>> int *x;
>>>>
>>>> So in a later expression, 'x' has a pointer type. Thus, the
>>>> expression '*x' means: The pointee of 'x'.
>>>>
>>>> So 'sizeof *x' means: The size of the pointee of 'x'.
>>>>
>>>> '*x' doesn't tell the compiler that 'x' is a pointer. Your
>>>> declaration of 'x' told the compiler that 'x' is a pointer.
>>>
>>> So for a function that takes a pointer you don't need to pass
>>> that * operator right?
>>>

>>
>> You don't pass an operator, ever.
>>
>>> void func(void *);
>>> would be void func(x); for example. I know you could also use
>>>
>>> void func(&x);
>>> And bypass the pointer altogether and pass the object pointed to
>>> (pointee) right?
>>>

>>
>> That second one is not C, so you're mistaken.

>
> In the example of scanf which takes a pointer I am thinking you use the
> & operator. What's the difference with the second example I gave?
>


'void func(&x);' is a syntax violation. It starts out looking like a
function declaration, but isn't one.

--
- Shao Miller
--
"Thank you for the kind words; those are the kind of words I like to hear.

Cheerily," -- Richard Harter
 
Reply With Quote
 
Shao Miller
Guest
Posts: n/a
 
      01-17-2013
On 1/17/2013 17:50, Bill Cunningham wrote:
> Barry Schwarz wrote:
>> On Thu, 17 Jan 2013 06:49:11 -0500, "Bill Cunningham"
>> <(E-Mail Removed)> wrote:
>>
>>> Shao Miller wrote:
>>>> On 1/16/2013 19:10, Bill Cunningham wrote:
>>>>> Shao Miller wrote:
>>>>>
>>>>>> Or you could add this:
>>>>>>
>>>>>> x = malloc(sizeof *x);
>>> <snip>
>>>
>>> Does the * in front of the x indicate that x is a pointer? You do
>>> not want just x then or the compiler wouldn't know it's a pointer?

>>
>> There is a difference in meaning between the use of the * in a
>> declaration and in a statement.
>>
>> In a declaration such as
>> int *y;
>> the * indicates that y is indeed a pointer.
>>
>> In a statement such as you quoted, the * is the unary indirection
>> operator. This operator requires that its operand be a pointer but it
>> does not mean that the operand is a pointer. If you mistakenly apply
>> this operator to a non-pointer operand, it does not magically make the
>> operand a pointer. Instead you have a syntax error.
>>
>> And of course you also have to distinguish between the unary
>> indirection operator and the binary multiplication operator.
>>
>> Consider
>> int a = 1;
>> int b = 2;
>> int *p = &a;
>> int c = b * *p;
>> All three asterisks mean something different.

>
> What I seem to be confused about is when the unary operator is needed
> and when it isn't.
>
> FILE *fp;
> fp=fopen()...
> not *fp=fopen()...
> And in the example I Shao gave,
>
> x=malloc(sizeof *x);
> x=malloc(sizeof x); WRONG
>


The meaning of '*' depends on the context. It can be part of a pointer
declarator, or can be one of two possible operators used in an
expression. The unary indirection operator is needed when you wish to
designate the pointee of a pointer.

--
- Shao Miller
--
"Thank you for the kind words; those are the kind of words I like to hear.

Cheerily," -- Richard Harter
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      01-18-2013
On Thu, 17 Jan 2013 17:50:05 -0500, "Bill Cunningham"
<(E-Mail Removed)> wrote:

>Barry Schwarz wrote:


snip

>> There is a difference in meaning between the use of the * in a
>> declaration and in a statement.
>>
>> In a declaration such as
>> int *y;
>> the * indicates that y is indeed a pointer.
>>
>> In a statement such as you quoted, the * is the unary indirection
>> operator. This operator requires that its operand be a pointer but it
>> does not mean that the operand is a pointer. If you mistakenly apply
>> this operator to a non-pointer operand, it does not magically make the
>> operand a pointer. Instead you have a syntax error.
>>
>> And of course you also have to distinguish between the unary
>> indirection operator and the binary multiplication operator.
>>
>> Consider
>> int a = 1;
>> int b = 2;
>> int *p = &a;
>> int c = b * *p;
>> All three asterisks mean something different.

>
> What I seem to be confused about is when the unary operator is needed
>and when it isn't.
>
>FILE *fp;
>fp=fopen()...
>not *fp=fopen()...
>And in the example I Shao gave,
>
>x=malloc(sizeof *x);
>x=malloc(sizeof x); WRONG


You use the name of the pointer when the pointer is what your code is
dealing with. You code the * to dereference the pointer when the
object pointed to is what your code is dealing with.

When you allocate space, the size of the object that will occupy the
space is what is important, not the size of the pointer that points to
that space.

Consider the case where x is pointer to struct and the structure
contains a dozen int. On a 32 bit system, the size of the pointer
will usually be 4. The size of the structure will be 48.

If you code the syntactically correct but logically incorrect
x = malloc(sizeof x);
it will only allocate 4 bytes. You will never be able to fit your
structure into that small a space. If you code
x = malloc(sizeof *x);
you are telling the system to reserve enough space for the object that
will eventually occupy that space.

--
Remove del for email
 
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
Allocating vector of strings seem to crash. Allocating array ofstrings seems to be ok . Rakesh Kumar C++ 5 12-21-2007 10:42 AM
segmenatation fault while allocating memory Sameer C++ 2 11-03-2003 09:24 AM
allocating memory for std::string? Axel C++ 1 10-27-2003 12:35 PM
basic allocating memory question soni29 C++ 6 09-05-2003 05:45 PM
Pushing memory allocating objects into a vector. hall C++ 4 08-20-2003 09:24 PM



Advertisments