Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dynamic memory allocation using malloc()/calloc()
#3
raveendran Wrote:Hi,
The third problem is with writing portable code. Its quite common to use codes like (X*)0->data1 to get the offset to data1 , where X is a structure ( struct X { }; ) and data1 is a member within that structure. Are we not supposed to use these types of casts too [ (X*)0 ] ???

Dear Ravenndran

Writing code that depends on knowing the offset of a member of a struct is already not really portable. There should be better solutions. By the way, dereferencing a NULL pointer is not ANSI compliant and might lead to surprises on some systems.

But I admit that if you HAVE to know the offset, it's an elegant way to compute it without declaring a variable of that struct type, and it works on most compilers. We also failed in our efforts to find a compliant way to compute the offset of a member.

Regards Roger


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)