Creating array of class objects using DMA

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
mahaju
Posts: 487
Joined: Mon 11 Oct 2010, 07:11
Location: between the keyboard and the chair

Creating array of class objects using DMA

#1 Post by mahaju »

Can you please tell me what's the mistake here?

Code: Select all

#include<iostream.h>
#include<conio.h>
#include<stdio.h>

typedef unsigned int uint;

class HalfAdder;
class FullAdder;

uint bin_array_error = 0;       // global variable to signal an overflow in the binary representation

/***********************************************************************
 *      Note that at the location where the logic functions are called
 *      (or practically anywhere in the code which use uint -> technically
 *      int 32 bit integer numbers to store one bit value)
 *      it should be ensured that uint type input variables are either
 *      0x00000000 or 0x00000001 only because this is what has been assumed
 *      in the functions
 *      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *      lower bit is stored in lower array index: Littel Endian
 *      var[3] = {x, y, z} ==> x=> LSB and z => MSB
 *      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *      a uint array here is (generally) an array of unsigned integers, where each
 *      array element is made up of 32 bits, considered by the compiler as an unsigned integer
 *      and used to store only one bit of a (multibit) binary number
/***********************************************************************/

/************** Functions needed for programming block **************
*********************************************************************/

/*** This function returns the numerical value of the bits represented in the uint array ***/
uint value_of_reg(uint *reg, uint no_of_bits){      // Note that  no_of_bits can be at maximum 31
    uint i, j,  return_val = 0, pow_of_2 = 1;
    for(i = 0; i < no_of_bits; i++){
        if(reg[i] != 0){
            pow_of_2 = 1;
            for(j = 0; j < i; j++)
                pow_of_2 = pow_of_2 * 2;
            return_val = return_val + pow_of_2;
 //           cout<<"\t\tInside function value_of_reg: \n"<<"\t\ti = "<<i<<"\treturn_val = "<<return_val<<endl;
        }
    }
    return return_val;
}

/*** this function increments the bit representation by specified number of bits ***/
uint inc_bin_num(uint *reg, uint no_of_bits, uint no_of_inc){
    uint i, carry = 0;
    for(;no_of_inc > 0; no_of_inc--){
        reg[0] = reg[0] + 1;
        if(reg[0]%2 == 0){      // after doing reg[0]+1, reg[0]%2 will be zero only if reg[0] was previously 1, that is, value of reg[0] now is 2
            reg[0] = reg[0]%2;  // this will change value of reg[0] to 0
            carry = 1;          // and generate a carry
        }
        for(i=1;i<no_of_bits;i++){
            if(carry == 1){
                reg[i] = reg[i] + 1; // if there was a carry from the lower bit, increment the next higher bit
                if(reg[i]%2 == 0) {                                 // if this higher bit became 2 after addition ( %2 == 0); note taht after addition if the bit becomes 1 then we do not need to do anything
                    if(i==no_of_bits-1) {carry = 1; bin_array_error = 1; reg[i] = reg[i]%2; return bin_array_error;}  // another carry should be generated; again, if this higher bit is the highest bit in the binary number, then an overflow is returned and the function terminates immediately
                    reg[i] = reg[i] % 2;        // if it's not the highest bit, convert it to 0 (note that this lline never converts it to 1, as code execution will reach here only if the higher bit is 0 after addition
                    carry = 1;
                }
                else {carry = 0; break;}
            }
        }
    }
}


/*********************************************************************
************** Functions needed for programming block ENDS HERE ****************/




/************** Logic function implementation block **************
*********************************************************************/

/*** Two input and gate ***/
uint and_2_inp(uint ip1, uint ip2){
    return(ip1 & ip2);
}

/*** Two input or gate ***/
uint or_2_inp(uint ip1, uint ip2){
    return(ip1 | ip2);
}

/*** 1 bit inverter (not gate) ***/
uint not_gate(uint ip1){
    ip1 = ~ip1;
    ip1 = ip1 - 0xfffffffe;     // assuming that all the uint type input variables are either 0x00000000 or 0x00000001 only
    return(ip1);
}

/*** Two input xor gate ***/
uint xor_2_inp(uint ip1, uint ip2){
    return(ip1^ip2);
}

uint inc_bin_num_using_HA(uint *reg, uint no_of_bits, uint no_of_inc){
/**** This implementation seems to be a type of carry propagation adder
        See the type that I intend to implement in materials\HA_array.JPG
        Note that this function uses the same register as source and destination
        that is, updates a registers value to store the incremented version
***/
//    HalfAdder *HA_array;
    HalfAdder HA_array;
    uint i;
//    HA_array = (HalfAdder *)calloc(no_of_bits, sizeof (HAlfAdder) );
    HA_array = new HalfAdder [no_of_bits];
//    HA_array = (HalfAdder*) calloc(no_of_bits, sizeof(HalfAdder) );
    for(i=0; i< no_of_bits; i++){
        if(i=0) {HA_array[i].compute(reg[i], 1); reg[i] = HA_array[i].get_sum();}   // to the LSB of the binary number add 1 (always 1 is added to the LSB, obvious)
        else{
            HA_array[i].compute( reg[i], HA_array[i-1].get_carry_out() );
            reg[i] = HA_array[i].get_sum();
        }
    }
    return( HA_array.get_carry_out() );     // funtion returns the carry out from the last halfadder (but I think it may be better if it returned something else)
    //delete [] HA_array;
}







/*********************************************************************
************** Logic function implementation block ENDS HERE **************/


class HalfAdder{
    private:
        uint sum, carry_out, ha_inp1, ha_inp2;
    public:
        HalfAdder():sum(0), carry_out(0), ha_inp1(0), ha_inp2(0) {}
        HalfAdder(uint x, uint y){
            ha_inp1 = x;
            ha_inp2 = y;
        }

        void compute(uint i1, uint i2){     // Note: Constructor sets the values of the three input bits (fa_inp1, fa_inp2, carry_in) during object creation; This function sets the value as well as calculates Full Adder function when called
            ha_inp1 = i1;
            ha_inp2 = i2;
            sum = xor_2_inp(ha_inp1, ha_inp2);
            carry_out = and_2_inp(ha_inp1, ha_inp2);
        }

        uint get_sum(){
            return sum;
        }

        uint get_carry_out(){
            return carry_out;
        }

        void show_state(){
            cout<<"\ninput 1 => "<<ha_inp1<<"input 2 => "<<ha_inp2<<endl;
            cout<<"\n\tsum => "<<sum<<"\n\tcarry_out => "<<carry_out<<endl;
        }
};


class FullAdder{
    private:
        uint sum, carry_out, fa_inp1, fa_inp2, carry_in;
    public:
        FullAdder():sum(0), carry_out(0), fa_inp1(0), fa_inp2(0), carry_in(0) {}
        FullAdder(uint x, uint y, uint z){
            fa_inp1 = x;
            fa_inp2 = y;
            carry_in = z;
        }
        void compute(uint i1, uint i2, uint c){     // Note: Constructor sets the values of the three input bits (fa_inp1, fa_inp2, carry_in) during object creation; This function sets the value as well as calculates Full Adder function when called
            fa_inp1 = i1;
            fa_inp2 = i2;
            carry_in = c;
            HalfAdder sec1(fa_inp1, fa_inp2), sec2;
            sec1.compute(fa_inp1, fa_inp2);
            sec2.compute( carry_in, sec1.get_sum() );
            sum = sec2.get_sum();       // the sum output of the full adder is the sum output of section 2 half adder; see materials folder --> fulladder.gif
            carry_out = or_2_inp( sec1.get_carry_out(), sec2.get_carry_out() );
        }

        uint get_sum(){
            return sum;
        }

        uint get_carry_out(){
            return carry_out;
        }

        void show_state(){
            cout<<"\ninput 1 => "<<fa_inp1<<"\ninput 2 => "<<fa_inp2<<"\ncarry_in => "<<carry_in<<endl;
            cout<<"\n\tsum => "<<sum<<"\n\tcarry_out => "<<carry_out<<endl;
        }
};

int main(){
    FullAdder obj;
    uint reg_3_bit[3]={0,0,0};      // LSB...MSB
    while(  /* value_of_reg( reg_3_bit, 3)<8*/ bin_array_error ==0 ){
//        cout<<"Current value of reg = "<<value_of_reg( reg_3_bit, 3)<<" "<<reg_3_bit[2]<<" "<<reg_3_bit[1]<<" "<<reg_3_bit[0]<<endl;
        obj.compute(reg_3_bit[0], reg_3_bit[1], reg_3_bit[2]);
        obj.show_state();
        cout<<endl;
//        inc_bin_num(reg_3_bit, 3, 1);
        inc_bin_num_using_HA(reg_3_bit,3,1);
        getch();
    }
}

here is the error message in CodeBlocks

Code: Select all

C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\backward\backward_warning.h|32|warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.|
D:\CB_Files\bit_level_manip\code.cpp||In function `uint inc_bin_num_using_HA(uint*, uint, uint)':|
D:\CB_Files\bit_level_manip\code.cpp|108|error: aggregate `HalfAdder HA_array' has incomplete type and cannot be defined|
D:\CB_Files\bit_level_manip\code.cpp|111|error: invalid use of undefined type `struct HalfAdder'|
D:\CB_Files\bit_level_manip\code.cpp|7|error: forward declaration of `struct HalfAdder'|
||=== Build finished: 3 errors, 1 warnings ===|
Neither new or calloc is allocating memory.
What is the mistake here?

User avatar
SirDuncan
Posts: 829
Joined: Sat 09 Dec 2006, 20:35
Location: Ohio, USA
Contact:

#2 Post by SirDuncan »

Rule number 1 of debugging: Always fix the first error first.

Your first error was not with the allocation of memory, but was:
D:\CB_Files\bit_level_manip\code.cpp|108|error: aggregate `HalfAdder HA_array' has incomplete type and cannot be defined|
This is because of the order in which you had things declared and defined. There were also a number of errors that appeared when using the GNU compiler that you did not receive from the Win compiler.

I'm not exactly sure what your code is supposed to do, so I can't fix any logic errors, but I managed to get it to compile with no errors using GCC by tweaking the code to look like this:

Code: Select all

#include<iostream>
//#include<conio.h>
#include<stdio.h>

#include<termios.h>//added for the getch implementation
#include<unistd.h>//added for the getch implementation

using namespace std; //cout, etc. are in the std namespace - I assume that the Win compiler is ignoring namespaces, but G++ needs them declared explicitly

typedef unsigned int uint;

//Declare the function that they can be used before they are defined.
//This bypasses a great many issues caused by functions depending on one another.
//It is also considered proper form.
uint value_of_reg(uint *reg, uint no_of_bits);
uint inc_bin_num(uint *reg, uint no_of_bits, uint no_of_inc);
uint and_2_inp(uint ip1, uint ip2);
uint or_2_inp(uint ip1, uint ip2);
uint not_gate(uint ip1);
uint xor_2_inp(uint ip1, uint ip2);
uint inc_bin_num_using_HA(uint *reg, uint no_of_bits, uint no_of_inc);
int getch();//conio does not exist on Linux, so I had to make a getch function to emulate it
//I did not write it, but I found it on http://cboard.cprogramming.com/faq-board/27714-faq-there-getch-conio-equivalent-linux-unix.html
//You should be okay to remove this, the definition, and the comment slashes before the conio
//include if you are compiling on a machine with conio (I think it is a Win only library).

class HalfAdder{
    private:
        uint sum, carry_out, ha_inp1, ha_inp2;
    public:
        HalfAdder():sum(0), carry_out(0), ha_inp1(0), ha_inp2(0) {}
        HalfAdder(uint x, uint y){
            ha_inp1 = x;
            ha_inp2 = y;
        }

        void compute(uint i1, uint i2){     // Note: Constructor sets the values of the three input bits (fa_inp1, fa_inp2, carry_in) during object creation; This function sets the value as well as calculates Full Adder function when called
            ha_inp1 = i1;
            ha_inp2 = i2;
            sum = xor_2_inp(ha_inp1, ha_inp2);
            carry_out = and_2_inp(ha_inp1, ha_inp2);
        }

        uint get_sum(){
            return sum;
        }

        uint get_carry_out(){
            return carry_out;
        }

        void show_state(){
            cout<<"\ninput 1 => "<<ha_inp1<<"input 2 => "<<ha_inp2<<endl;
            cout<<"\n\tsum => "<<sum<<"\n\tcarry_out => "<<carry_out<<endl;
        }
};


class FullAdder{
    private:
        uint sum, carry_out, fa_inp1, fa_inp2, carry_in;
    public:
        FullAdder():sum(0), carry_out(0), fa_inp1(0), fa_inp2(0), carry_in(0) {}
        FullAdder(uint x, uint y, uint z){
            fa_inp1 = x;
            fa_inp2 = y;
            carry_in = z;
        }
        void compute(uint i1, uint i2, uint c){     // Note: Constructor sets the values of the three input bits (fa_inp1, fa_inp2, carry_in) during object creation; This function sets the value as well as calculates Full Adder function when called
            fa_inp1 = i1;
            fa_inp2 = i2;
            carry_in = c;
            HalfAdder sec1(fa_inp1, fa_inp2), sec2;
            sec1.compute(fa_inp1, fa_inp2);
            sec2.compute( carry_in, sec1.get_sum() );
            sum = sec2.get_sum();       // the sum output of the full adder is the sum output of section 2 half adder; see materials folder --> fulladder.gif
            carry_out = or_2_inp( sec1.get_carry_out(), sec2.get_carry_out() );
        }

        uint get_sum(){
            return sum;
        }

        uint get_carry_out(){
            return carry_out;
        }

        void show_state(){
            cout<<"\ninput 1 => "<<fa_inp1<<"\ninput 2 => "<<fa_inp2<<"\ncarry_in => "<<carry_in<<endl;
            cout<<"\n\tsum => "<<sum<<"\n\tcarry_out => "<<carry_out<<endl;
        }
};

uint bin_array_error = 0;       // global variable to signal an overflow in the binary representation

/***********************************************************************
 *      Note that at the location where the logic functions are called
 *      (or practically anywhere in the code which use uint -> technically
 *      int 32 bit integer numbers to store one bit value)
 *      it should be ensured that uint type input variables are either
 *      0x00000000 or 0x00000001 only because this is what has been assumed
 *      in the functions
 *      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *      lower bit is stored in lower array index: Littel Endian
 *      var[3] = {x, y, z} ==> x=> LSB and z => MSB
 *      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *      a uint array here is (generally) an array of unsigned integers, where each
 *      array element is made up of 32 bits, considered by the compiler as an unsigned integer
 *      and used to store only one bit of a (multibit) binary number
***********************************************************************/

int main(){
    FullAdder obj;
    uint reg_3_bit[3]={0,0,0};      // LSB...MSB
    while(  /* value_of_reg( reg_3_bit, 3)<8*/ bin_array_error ==0 ){
//        cout<<"Current value of reg = "<<value_of_reg( reg_3_bit, 3)<<" "<<reg_3_bit[2]<<" "<<reg_3_bit[1]<<" "<<reg_3_bit[0]<<endl;
        obj.compute(reg_3_bit[0], reg_3_bit[1], reg_3_bit[2]);
        obj.show_state();
        cout<<endl;
//        inc_bin_num(reg_3_bit, 3, 1);
        inc_bin_num_using_HA(reg_3_bit,3,1);
        getch();
    }
} 

/************** Functions needed for programming block **************
*********************************************************************/

/*** This function returns the numerical value of the bits represented in the uint array ***/
uint value_of_reg(uint *reg, uint no_of_bits){      // Note that  no_of_bits can be at maximum 31
    uint i, j,  return_val = 0, pow_of_2 = 1;
    for(i = 0; i < no_of_bits; i++){
        if(reg[i] != 0){
            pow_of_2 = 1;
            for(j = 0; j < i; j++)
                pow_of_2 = pow_of_2 * 2;
            return_val = return_val + pow_of_2;
 //           cout<<"\t\tInside function value_of_reg: \n"<<"\t\ti = "<<i<<"\treturn_val = "<<return_val<<endl;
        }
    }
    return return_val;
}

/*** this function increments the bit representation by specified number of bits ***/
uint inc_bin_num(uint *reg, uint no_of_bits, uint no_of_inc){
    uint i, carry = 0;
    for(;no_of_inc > 0; no_of_inc--){
        reg[0] = reg[0] + 1;
        if(reg[0]%2 == 0){      // after doing reg[0]+1, reg[0]%2 will be zero only if reg[0] was previously 1, that is, value of reg[0] now is 2
            reg[0] = reg[0]%2;  // this will change value of reg[0] to 0
            carry = 1;          // and generate a carry
        }
        for(i=1;i<no_of_bits;i++){
            if(carry == 1){
                reg[i] = reg[i] + 1; // if there was a carry from the lower bit, increment the next higher bit
                if(reg[i]%2 == 0) {                                 // if this higher bit became 2 after addition ( %2 == 0); note taht after addition if the bit becomes 1 then we do not need to do anything
                    if(i==no_of_bits-1) {carry = 1; bin_array_error = 1; reg[i] = reg[i]%2; return bin_array_error;}  // another carry should be generated; again, if this higher bit is the highest bit in the binary number, then an overflow is returned and the function terminates immediately
                    reg[i] = reg[i] % 2;        // if it's not the highest bit, convert it to 0 (note that this lline never converts it to 1, as code execution will reach here only if the higher bit is 0 after addition
                    carry = 1;
                }
                else {carry = 0; break;}
            }
        }
    }
    return(0);//this needed to either return something or be changed to a void function
}
/*********************************************************************
************** Functions needed for programming block ENDS HERE ****************/

/************** Logic function implementation block **************
*********************************************************************/
/*** Two input and gate ***/
uint and_2_inp(uint ip1, uint ip2){
    return(ip1 & ip2);
}

/*** Two input or gate ***/
uint or_2_inp(uint ip1, uint ip2){
    return(ip1 | ip2);
}

/*** 1 bit inverter (not gate) ***/
uint not_gate(uint ip1){
    ip1 = ~ip1;
    ip1 = ip1 - 0xfffffffe;     // assuming that all the uint type input variables are either 0x00000000 or 0x00000001 only
    return(ip1);
}

/*** Two input xor gate ***/
uint xor_2_inp(uint ip1, uint ip2){
    return(ip1^ip2);
}

uint inc_bin_num_using_HA(uint *reg, uint no_of_bits, uint no_of_inc){
/**** This implementation seems to be a type of carry propagation adder
        See the type that I intend to implement in materials\HA_array.JPG
        Note that this function uses the same register as source and destination
        that is, updates a registers value to store the incremented version
***/
    HalfAdder *HA_array;
//    HalfAdder HA_array;
    uint i;
//    HA_array = (HalfAdder *)calloc(no_of_bits, sizeof (HAlfAdder) );
    HA_array = new HalfAdder [no_of_bits];
//    HA_array = (HalfAdder*) calloc(no_of_bits, sizeof(HalfAdder) );
    for(i=0; i< no_of_bits; i++){
        /*this was originally if(i=1) which is an assignment and would always evaluate to true*/if(i==0) {HA_array[i].compute(reg[i], 1); reg[i] = HA_array[i].get_sum();}   // to the LSB of the binary number add 1 (always 1 is added to the LSB, obvious)
        else{
            HA_array[i].compute( reg[i], HA_array[i-1].get_carry_out() );
            reg[i] = HA_array[i].get_sum();
        }
    }
    return( HA_array[i].get_carry_out() );     // funtion returns the carry out from the last halfadder (but I think it may be better if it returned something else)
    //delete [] HA_array;
}
/*********************************************************************
************** Logic function implementation block ENDS HERE **************/


/********************** Function Needed For Linux *********************/
//should emulate the getch from conio
int getch(){
  struct termios oldt,
                 newt;
  int            ch;
  tcgetattr( STDIN_FILENO, &oldt );
  newt = oldt;
  newt.c_lflag &= ~( ICANON | ECHO );
  tcsetattr( STDIN_FILENO, TCSANOW, &newt );
  ch = getchar();
  tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
  return ch;
}
You should not need the parts I added for getch, I just needed them because the conio library does not exist on Linux.
Be brave that God may help thee, speak the truth even if it leads to death, and safeguard the helpless. - A knight's oath

User avatar
mahaju
Posts: 487
Joined: Mon 11 Oct 2010, 07:11
Location: between the keyboard and the chair

#3 Post by mahaju »

Thanks a lot for your help
Your code does compile and I have corrected the logical errors present
But what I don't understand is where was the mistake in my original code
I thought I had defined all the functions before calling, so I should be fine
Even if some functions weren't defined before being called, I thought the compiler should show at least some kind of warning if not an error
but it seems it didn't show anything
Further more, in the function inc_bin_num_using_HA(), inside the for loop, there is a line "if(i=0) ... "
you seem to have corrected this for me, but I thought the compiler would show some warning for this
At first the output sort of went into an infinite loop and I had no idea why. I then opened this code in good old Turbo C++ and it shows a warning at this line
why doesn't a new compiler such as gcc do that? i am using CodeBlocks 8.02 as my IDE. Is there any settings for the compiler I need to set which aren't set by default? Or should I switch to some other IDE?

By the way, it seems we are not allowed to do line by line execution of code for debugging if we are working on only one file, in CodeBlocks IDE. How do I do it? I mean using functions such as trace into and step over. It was so much easier to do in Turbo C++, but I want to write my programs in standardized C++.

User avatar
SirDuncan
Posts: 829
Joined: Sat 09 Dec 2006, 20:35
Location: Ohio, USA
Contact:

#4 Post by SirDuncan »

mahaju wrote:Thanks a lot for your help
You're welcome.
mahaju wrote: . . . I thought the compiler should show at least some kind of warning if not an error . . . there is a line "if(i=0) ... " . . . I thought the compiler would show some warning for this . . . Turbo C++ and it shows a warning at this line . . . why doesn't a new compiler such as gcc do that? i am using CodeBlocks 8.02 as my IDE. Is there any settings for the compiler I need to set which aren't set by default? Or should I switch to some other IDE?
I am not familiar with what arguments CodeBlocks passes to g++. The options you will normally want are -Wall and -Werror. Usually you will also want to set the compiler optimization level with -O2 (-O3 is more highly optimized but may produce weird results in some cases, so 2 is often the highest you'll want to go).

-Wall would have shown you the warnings that were missed, including the declaration order problems and the 'i=0' problem.

So to summarize, you want:

Code: Select all

g++ -Wall -Werror -O2 ./file.c
if you are not planning on using GNU Debugger (gdb).
mahaju wrote:By the way, it seems we are not allowed to do line by line execution of code for debugging if we are working on only one file, in CodeBlocks IDE. How do I do it? I mean using functions such as trace into and step over. It was so much easier to do in Turbo C++, but I want to write my programs in standardized C++.
I'm not sure what CodeBlocks uses for debugging, but you can definitely trace and step with gdb. You just need to compile with the -g option to compile with debug information left in the executable.

Code: Select all

g++ -g -Wall -Werror -O2 ./file.c
Then run gdb on the executable:

Code: Select all

gdb ./a.out
For more information on gdb see
http://www.gnu.org/software/gdb/documentation/
You can also integrate gdb with Geany, Netbeans, Eclipse, and most other IDEs. I would be extremely surprised if you could not do this with CodeBlocks. All of the IDEs I have mentioned can be run on Windows, Linux, and OSX.

If you are wanting to write standard C++, you will need to avoid non-standard libraries like conio. It is a Windows library and not part of the C++ standard.

If you are just starting out coding in C/C++, I would suggest learning on a Linux machine without a full IDE. You are less likely to accidentally use non-standard libraries on Linux. If you use a full IDE it will hide some things from you in an effort to make things easier for you. This is not what you want when you are learning as it will mean that you are unaware of all that is happening. It is better that you have to do (nearly) everything manually when you are just starting to learn.
Be brave that God may help thee, speak the truth even if it leads to death, and safeguard the helpless. - A knight's oath

User avatar
mahaju
Posts: 487
Joined: Mon 11 Oct 2010, 07:11
Location: between the keyboard and the chair

#5 Post by mahaju »

Hello Sir
I was tinkering around in CodeBlocks and I was able to set -Wall but I couldn't find how to set -Werror, still, I think being able to read all warnings would be enough for now. If I do have to set -Werror I guess I can compile my code through command line any time

As for conio, I will have to use getch() many times, especially for troubleshooting while I write my code and since I am working in Windows I think I should keep it for now

And there was one more thing I wanted to ask you
It seems that the optimization options for gcc [such as -O, -O1, .. , -Os etc] are not set at all. So what does this mean? I had the idea that when working in a high level language instead of assembly mnemonics, the process of compiling would imply (by default) the removing of redundant instruction in the assembly level. So in my case, is this still done to some extent even if the optimization options are not set? That is to say, if I compile a function such as printf(), the compiler will give me the equivalent mnemonics of all the code in that function, no matter what is written in it (as long as it compiles, that is, free of syntax errors I mean) or will it do some modifications of it's own?

I am sorry if I couldn't state my question clearly. Actually I haven't quite understood how these optimization features work.

User avatar
SirDuncan
Posts: 829
Joined: Sat 09 Dec 2006, 20:35
Location: Ohio, USA
Contact:

#6 Post by SirDuncan »

There is some default optimization, but there is more that can be done. Compiler theory is not my strong suit, but I think I can give a basic explanation. The optimization levels are referring to speed optimizations. Some of these optimizations can actually increase the size of your executable or increase its memory footprint. This may not always be desirable. If you have a machine with a very fast processor but limited disk space and limited RAM, you may prefer that the execution takes a few more cycles instead of taking up more space. For complete information, see the GCC documentation:
http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

If you can't figure out how to set the optimization level in CodeBlocks, I wouldn't worry. It shouldn't result in any appreciable performance increase until you are writing larger, more sophisticated applications.
Be brave that God may help thee, speak the truth even if it leads to death, and safeguard the helpless. - A knight's oath

Post Reply