Questions

I have a list containing many questions about C++...some of them were answered in these forums, but there are always more!

11(ANSWERED). In a division of int by int, is the result an integer or a real number? I know that if the result is written to an int variable there will be rounding, but what happens when the result is written to a float or when it's used as output with cin?

16. Is dynamic memory automatically returned to the system when the program ends, or I have to use delete?

17. How are data files (graphics, sound, binary, text, etc.) put into the EXE file and how are they used in the C+ code? I know external files can be used with the file r/w functions easily, but what about files that don't have a path? They are inside the EXE. How does it work?

19(ANSWERED). Is there a pi constant in cmath or cstdlib? And what is M_PI ? when defining pi or any number with too many digits, how many digits are significant? How many of them actually get into the floating-point variable? and do the other digits affect the translation of pi into binary?

20(ANSWERED). If I'm not mistaken, giving values with { } is only possible when initializing an array in the declaration line, like this: int a[]={2,6,4,2,4};
Does this also work with objects?
1
2
3
4
5
6
7
8
9
class A
{
public:
    int a;
    float b;
    bool c;
};
 
A obj{6,4.2f,true};


21. Usually there are many functions in a project that are not used. For example, the iostream library is included for i/o operations, but I only need cin and cout and their << operator functions. Does the compiler still incude the whole library? Compiled libraries maybe yes, but what about a cpp file? I have the source of a library as a header file with all function declarations and a cpp file with their definitions. In the object file there are probably all the contents of the cpp file, but in the linking stage - does the compiler eliminate functions that aren't used?

23. In float a=0; , does the compiler automatically convert the 0 into 0.0f in the compilation process or using 0 means there will be type casting in run time so using 0.0f in the code is faster?

24. In pointer arithmetics, do members of objects considered "defined object of their type"? For example, if there's class A with members a,b and c, all of type int, and an object obj of type A, does &(obj.a)+1 give a pointer to to obj.b?


Indeed, I have lots of questions. XD

Last edited on
I'll try to answer a few questions.
But most of them you could have found out by simply trying it ;-).


11) It will be an int.
Example :
1
2
int a = 1 / 2 ; // the same as int a = 0
real a = 1 / 2 ; // the same as real a = 0.0 or real a = 0 

The conversion from int to real is done as late as possible e.g. after the division.

 
real a = 1.0/2 ; //equals real a = 0.5 

to calculate 1.0/2, 2 has to be promoted to floating point (double or real). The two floats are then divided using floating point algorithms therefore resulting in 0.5


17) I wonder how you include your images in the source code? If you include text data, they should be stored according to the corresponding encoding (mostly ascii or unicode)

20) Use an constructor, i.e.

1
2
3
4
5
6
7
8
9
10
11
12
class A
{
public: //better use private
    int a;
    float b;
    bool c;

public: 
    A(int an_a, float a_b, bool a_c) {a := an_a ; b:= a_b; c:= a_c } ;
};
 
A obj(6,4.2f,true);


23) Try both an compare the resulting binary files! I suppose there should be no difference.
There should even be no difference in this case,
1
2
3
const int c1 = 4 ;

float var1 = 2 * c1 + 1 ; // as fast as float var1 = 9.0f ; 


since the compiler optimizes compile time constant expressions.


19) I did a google search and a foung this thread about pi: http://bytes.com/groups/c/743610-extracting-pi-value-cmath
It looks like that page has some methods for getting pi in a simple way and I found it under cmath.
As for the floating point, I read in another document that a float usually is accurate up to 7 digits.
20) the rules for what you can and can't define with a {} are quite complex, but basically if it looks like a c struct, you can get away with it. As onur says, using a constructor is better.

FYI, you can also us the constructor syntax for arrays:

A objs[3] = { A(6, 4.2f, true), A(7, 5.2f, true), A(6, 4.2f, false) };
just a note to add on to onur #11
it will strait truncate the decimal no rounding down or w/e just disregards
Thanks for the answers! I'll mark the answered questions in the first post. But I still need some answers...
Last edited on
Can someone please answer my questions? :)
16) It is automatically freed though it is still good programming practice to release the resources you use.

17) The resource gets compiled as a byte array with an internal name, eg:
char internal_name_here[] = { /* bytes of data */ };
As the programmer, you refer to the resource with a different name. A table
is generated:
struct data {
const char* name_the_programmer_uses;
const char* internal_name_here;
} resources[] = { /* ... */ };
There is then a wrapper function you call with the external name. It searches
the table for the name you gave, and voila! there's the data.

21) Generally if one symbol from a .o/.obj/.a etc file is needed by your program then
the whole file is linked. The linker does not try to pick out just the parts you
need. But if you need nothing from a .o/.obj/.a file then the linker can elide
linking the entire file.

23) Yes, the cast is done at compile time.

24) You can't do arithmetic on pointers to members. In your example, a has type
A::int, not int, and &a has type A::int*. Arithmetic on pointers to members is
not allowed.
16. Is dynamic memory automatically returned to the system when the program ends, or I have to use delete?


You have to delete, or the dynamically allocated memory would be freed only after the computer has been shut down
Thanks for the answers! This forum is so useful... :)
16. Is dynamic memory automatically returned to the system when the program ends, or I have to use delete?


You have to delete, or the dynamically allocated memory would be freed only after the computer has been shut down

@Bazzy... it is freed as soon as your program terminates.

You can try that out writing a simple code.
Topic archived. No new replies allowed.