LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-18-2005, 02:16 PM   #16
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
why typedef was invented :P


Quote:
The disadvantage of that is in the case that you accidentally change the type of *r without changing the malloc call.

Hmmm intresting but I belive this is why typedef was invented or eazier then that include a #define.

using a pointer because in the off chance the value may be a diffrent kind of varible is silly and dangerous. Even if its not an issue at run time it makes the code hard to understand to others. I would say that just cause you can do something doesent mean you have to do it that way or is the best way to do it.


Code:
 
//dont know if a defind would work this way. 

#define type float
 

char *r; 

r = malloc(sizeof(type));

or hell a better eazier to understand methood.

Code:
 

#include<stdio.h> 

int main() 
{ 

   char *r; 
   int sze; 

   sze = sizeof(float); //change this if need diffrent value. 


   r = malloc(sze); 


  return 0; 
}
just my
 
Old 12-18-2005, 02:51 PM   #17
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
In a complex application, using a typedef or a define would become unwieldy because of the sheer number you need. Of course, a macro might be useful in allocation in some cases:
Code:
#define ALLOCARRAY(val,num) val=malloc((num)*sizeof(*val))

void workwitharray(){
    double *r;
    ALLOCARRAY(r,10);
}
Yes, this would still generate the sizeof(*r) mechanism, but this is probably the cleanest way to handle allocation of an array. Also, it is better self-documentation than malloc(count*sizeof(*r)) is. Thoughts?
[/code]
 
Old 12-18-2005, 03:53 PM   #18
elyk1212
Member
 
Registered: Jan 2005
Location: Chandler, AZ USA
Distribution: Mandrake/Mandriva 10.2
Posts: 186

Rep: Reputation: 30
For me, changing a sizeof(type) would be way easier to spot as something to change, need be, If you need to change the type of the pointer, change the type of the evaluating size (sizeof(type2),etc), That is very easy to debug. This makes a lot more sense to me, since the size of a primitive is not based on info in the variable, it is based on the type definition for the architecture. Pointer issues of not being valid, is a lot tougher to debug, for me at least.

That said, in my opinion, I still think it is poor practice to deallocate pointers that are not initialized. To each their own I suppose. Let us just hope that this technique is not used by accident in a construct that actually dereferences the pointer. That would be a bit difficult to debug, as the results would vary.

The macro you are proposing would simply be the same code after the precompiler parsed it, in place. But, again, to each their own.

My thoughts were this:
-Pointer values or variable values (in theory, not range) are separate from the space allocated to them
-Space allocated to variables/objects is dependent on the architecture's/language grammer's/programmer's definition of that type, not on an individual instantiation of
that type.

These are the other reasons I may opt to use a general case 'type' to define the size, instead of a individual case of a type (I am defining individual/special cases of a types to be instantiations of objects, variables, etc).

I guess my conclusion would be to use whatever method is more readable and in good practice for your specifications, while still avoiding pointer issues.
 
Old 12-18-2005, 04:23 PM   #19
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
All being said the OP never posted compleate code. We dont know what the varible r is.

Im assuming from the code that its a mesurement of some kind thus being a float or a double. If thats the case using this methood is kinda confusing but not illegal.


if it is an int then i would rather see size_t used instead.

if its a void pointer that changes its type thoughout the program and needs diffrent allocated memory for each type then i would say use the largest type that is used. Using a little more memory but probably safer then reallocateing memory as the type changes.
 
Old 12-18-2005, 05:55 PM   #20
elyk1212
Member
 
Registered: Jan 2005
Location: Chandler, AZ USA
Distribution: Mandrake/Mandriva 10.2
Posts: 186

Rep: Reputation: 30
I had thought that the pointer was a double*.

Yes, if it is a void pointer much consideration would have to be made of size. In the case of an object, structure or array this could be from anywhere from 1 to N. Where N mod word size = 0. (in theory, the largest size ever needed would be infinity, where infinity is a multiple of the word size). I agree, if a singular primitive is what the pointer is referring to, for flexibility it would be a good idea to allocate the largest primitive size to the pointer space.
 
Old 12-18-2005, 06:00 PM   #21
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
elyk1212, I agree that dereferencing a pointer of unknown context is a really bad idea. That being said, sizeof() is evaluated at compile time, so is not problematic. Also, in some cases, a single pointer may have memory allocated in a number of places (say, a general purpose char* buffer), and occasionally it is easy to miss a change in every location (to, say, wchar_t) and this can also cause bugs.

On the flip side of all this, I am honestly not sure how sizeof(*(void *)) returns. What is the size of a (void)?

I think it is all really a matter of personal preferences and specific usage.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to cast objects lucky6969b Programming 1 12-08-2005 06:41 PM
makes pointer from integer without a cast ? hubabuba Programming 2 01-28-2005 05:28 PM
pointer from integer without a cast bcf2 Programming 7 12-30-2004 02:04 PM
what does a cast in c++ actually do? markhod Programming 2 12-23-2004 09:01 AM
Passing arg 3 of blah makes pointer from integer without a cast xconspirisist Programming 6 08-22-2004 08:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration