Saturday, May 22, 2010

How to compare structure in C language? Whether it is compiler dependent?

This questions dependent on C language....

How to compare structure in C language? Whether it is compiler dependent?
The previous answer is not only wrong, it is


inefficiently wrong :-)





With respect to the efficient part, you do NOT


need to write a "compare" function as described


because it already exists in all ANSI C


implementations. See the function "memcmp()" in


your documentation.





With respect to correctness, this solution won't


consistently work. What's really terrible is


that it does work *some* of the time.





Consider this struct:





struct foo {


    char a;


    long count;


}





While field "a" will be at offset "0" within the


struct, on many machines "count" will be at


offset "4" (with 3 bytes of padding between


them). It will be as if you declared the struct


as:





struct foo {


    char a;


    char padding[3];


    long count;


}





If you create the struct as a stack variable or


from a call to "malloc", it can initially have


garbage in those padding bytes. Then if you


assign a value to "a" and "count", you still have


garbage in the padding area. Two structs with


identical values for "a" and "count" will still


compare unequal if they have different garbage in


the padding area.





Thus, the "memcmp" strategy for comparing them


won't consistently work if there is any padding


in the struct. In this case, you are forced to


compare fields.
Reply:Not to "pile on", but it may be worth pointing


out that the alignment padding isn't the only


source of bugs in the solution suggested by


"Vasudev TS".





A similar problem can be caused by bit fields


which don't completely fill up the word that


they're in:





typedef struct {


int id:12;


int group:12


int flag:1


} key_t;





struct x {


key_t key;


struct x *next;


}





In this simple example, there are 7 unused bits


in the "key_t" bitfield, which can vary between


otherwise identical copies of "struct x".





Even if you carefully check that your structure


doesn't contain any alignment padding or unused


bitfield space, how can you protect against


someone making a change (ie. adding another field)


some time in the future? What's insidious is


that it might not fail during your regression


tests, but only on some particular data set


while running your customer's data.





Bottom line: Bitwise struct comparisons don't work.
Reply:If you are comparing two structures of the same type, you can do so by writing a c function that takes te two structures and the size as arguments and compare them as if they are characters. To make it clearer the function could be some thing like this:





int compare(char *a, char *b, size)





This function compares both a and b and returns 1 if same and 0 if not.





while calling it from your main you could call it as below:





struct str *s1, *s2;


//assign values to the members of both ....





sts=compare ((char *)s1, (char *)s2, sizeof(struct str));





Additional Details:


I have answered a similar question earlier and provided a tested program as an example: Please refer to the question titled:





"How can we check whether the contents of two structure variables are same or not?"





in yahoo answers under programming %26amp; design section


Where can I download a free C and C++ IDE with built in compiler?

I am working in a Windows environment. I need to learn the language so I can distribute free softwares. I am also planning to create my own computer language from C++ or C. Is it possible?





Its one of that plans that I am purely blind of its whereabouts. That is why I must learn the language. I welcome any recommendations.





My motivation is to stop piracy that arises because of the Nemrod type of the worlds affection in the game of playing with the licenses of the Monopolies. I am pro to free software ventures and building a platform based on RIGHTEOUSNESS.





Is there someone who can lead my journey right? Is there anybody willing to help.





Please speak out.

Where can I download a free C and C++ IDE with built in compiler?
go to www.eclipse.org





there will be an ide for c and c++. download that.





eclipse is one of the best tool and ide to devolop projects.





I think you ll be helped a lot by this.





enjoy programming in it. There would be more details of the ide in the website. all the best
Reply:You might take a look on SourceForge. There are plenty of open source IDEs there as well as other open source projects.
Reply:http://www.microsoft.com/express/vc/


Which of the following may result in a compiler error in C#?

1) Referring to a derived class object with a base class reference





2) Referring to a base class object with a derived class reference





3) Referring to a base class object with a base class reference





4) Referring to a derived-class object with a derived class reference








Sorry, but I don't understand this at all. It's really confusing. Can you explain it to me, please?

Which of the following may result in a compiler error in C#?
#2 will be an error.





Imagine you have a superclass called GeometricFigure, and two subclasses called Circle and Square.





Create one object of each type.





Then, since the Circle object IS A GeometricFigure object (it's a subclass), then it's ok to have a reference of type GeometricFigure that refers to the Circle. That's the whole point of inheritance.





However...it's not true that a GeometricFigure IS A Circle. It might be or it might not be. But if it WASN'T a Circle (maybe it was a Square), and you tried to call its circumference() function...big problem.





So as a rule, for any given object, you can use a reference to its actual class or any of its PARENT classes or interfaces. You can't use a reference for any of its subclasses.





(#3 and #4 are obviously ok to do.)

bloom

How do I find my compiler requirments for c++ programing?

Your question is ambiguous and lacks sufficient detail upon which to base a comprehensive answer. Do you mean you have a C++ compiler, and you want to know system requirements to run it? Or are you looking for a C++ compiler that meets some established functionality requirements? Or are you planning to write a C++ compiler, and you're looking for language requirements?





Please clarify, to make your question possible to answer.


What is a good free compiler for learning c++ coding on win 98?

There are a couple to choose from. There is the Borland International free community compiler, Bloodshed Dev-C++, Open Watcom C++ and also that "Visual Studio Toolkit" that is freely available from Microsoft, but does not come with an IDE.





The Microsoft Platform SDKs can be downloaded from Microsoft's Developer Network site as well.

What is a good free compiler for learning c++ coding on win 98?
Try cygwin + gcc. It comes with a variety of development tools all for free. One drawback is that this is simulating a unix programming environment and that may not be what you want.
Reply:borland C/C++ compiler for free downloads---


http://community.borland.com/article/0,1...





http://community.borland.com/article/0,1...
Reply:Try Dev C++





Search Yahoo for: "Bloodshed Dev C++ download"


I'm not sure the exact URL, but that should find it for you!


Sunday, August 2, 2009

Which is the best(most recommended) compiler for compiling C/C++ programs on windows xp???

That depends on what criteria you put under Best.


don't know about recommended.


What you want is one that will optomize code to the CPU being complied on.


I figure if you a microsoft person you would recommend the Complier that Microsoft uses to compile its code.


you can get one through a subscription or outright buy.





for those more hardy and really want the best, I suggest the GNU compiler. This is for those that understand compilers and can write compiler code if need be.


Also this lets you have consistent code across other platforms.





check out GNU code for windows.


Is there any freeware compiler for ethier c or c++ for mac osx or mac os?

other than xcode and eclipes

Is there any freeware compiler for ethier c or c++ for mac osx or mac os?
http://developer.apple.com/tools/gcc_ove...





Short of that just literally go to





http://developer.apple.com they have a lot of info there :)
Reply:If you are looking for a freeware compiler, there is still MPW from apple:





http://developer.apple.com/tools/mpw-too...

dogwood