Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 structs :shock:
Index -> Programming, C++ -> C++ Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
bugzpodder




PostPosted: Sat Mar 26, 2005 1:19 pm   Post subject: structs :shock:

code:
struct BigInteger{
       
        int sz,start,maxsize, *arr;
        BigInteger(){start=0;maxsize=1;sz=1; arr=new int[1];}
             BigInteger(int val){BigInteger(); arr[0]=val;}
}


I dont know what the heck the problem is with the code above, but whenever I call BigInteger using the second constructor, sz is always 0!!

this can be seen from:

code:
#include<iostream>
using namespace std;
struct BigInteger{
       
        int sz,start,maxsize, *arr;
        BigInteger(){start=0;maxsize=1;sz=1; arr=new int[1]; cout<<sz<<endl;}
             BigInteger(int val){BigInteger(); cout<<sz<<endl; arr[0]=val;}
};

int main(){
        BigInteger u(20);
        return 0;

}

in which you'll see a 1 and 0 outputted. why on earth is that happening?


And also, for something like:

code:

pair<BigInteger,int> div(BigInteger &v1, int v2){
             .......
        cout<<ret.first.arr[0]<<endl;
        return ret;
}
            ....
        p=div(v,1e9);  //although 1e9 is a double rather than an int, it is not causing the problmes
        cout<<p.first.arr[0]<<endl;


and this produce totally different outputs!!!
I am completely lost as to whats going on...
Sponsor
Sponsor
Sponsor
sponsor
rizzix




PostPosted: Sat Mar 26, 2005 1:34 pm   Post subject: (No subject)

try:
c++:
class BigInteger {
   protected:
   int sz,start,maxsize, *arr;
   
    public:
    BigInteger(){
         start=0;
         maxsize=1;
         sz=1;
          arr=new int[1];
     }
   
    BigInteger(int val): BigInteger() {
          arr[0]=val;
     }
};
wtd




PostPosted: Sat Mar 26, 2005 1:40 pm   Post subject: (No subject)

First thing's first. You should be using proper constructor syntax, and since you're dynamically allocating memory, you should have a destructor.

c++:
struct BigInteger
{
   int sz,start,maxsize, *arr;

   BigInteger()
   : start(0)
   , maxsize(1)
   , sz(1)
   , arr(new int[1])
   { }

   BigInteger(int val)
   : start(0)
   , maxsize(1)
   , sz(1)
   , arr(new int[1])
   {
      arr[0] = val;
   }

   ~BigInteger()
   {
      delete [] arr;
   }
};
rizzix




PostPosted: Sat Mar 26, 2005 1:46 pm   Post subject: (No subject)

hmmm is that syntax C friendly?
bugzpodder




PostPosted: Sat Mar 26, 2005 1:47 pm   Post subject: (No subject)

i do have a destructor, but didnt bother to post it. my full code is 200 lines long. Okay, so a class probably works, but what the heck is wrong with struct?
wtd




PostPosted: Sat Mar 26, 2005 1:48 pm   Post subject: (No subject)

None of this is valid C. It is, however, perfectly valid C++ code.
bugzpodder




PostPosted: Sat Mar 26, 2005 1:49 pm   Post subject: (No subject)

hmm interesting, never seen code like that before wtd.

and the second problem is... well here is the full code (first problem was fixed): funny things are happening in toString (outputs dont match), which calls div

no clue why it is happening

code:

#include<iostream>
#include<fstream>
#include<string>
#include<algorithm>
using namespace std;


#define pb push_back            //pushes an element in the back
#define pf push_front           //pushes an element in the front

#define fe first                //first element of a pair container
#define se second               //second element of a pair container
#define len length()            //length of a string

using namespace std;

typedef unsigned int uint;      //32-bit unsigned int
typedef long long ll;           //64-bit signed long long
typedef unsigned long long ull; //64-bit unsigned long long

struct BigInteger{
       
        int sz,start,maxsize;
        uint *arr;
        BigInteger(int val=0){start=0;maxsize=1;sz=1; arr=new uint[1];arr[0]=val;}
        ~BigInteger(){delete [] arr;}
       
        int operator[](int i) {return arr[(start+i)%maxsize];}
       
        void resize(int size){
                if (sz>=size) return;
                uint *oldarr=arr;
                arr=new uint[size];
                for (int i=0;i<sz;i++) arr[i]=oldarr[(start+i)%maxsize];
                delete [] oldarr;
                start=0; maxsize=size;
               
        }
        void pb(uint v){
                if (sz==maxsize) resize(maxsize*2);
                arr[(start+(sz++))%maxsize]=v;
        }
        void pop_back(){
                if (sz>0) sz--;
        }
        void pf(uint v){
                if (sz==maxsize) resize(maxsize*2);
                sz++; start=(start+maxsize-1)%maxsize;
                arr[start]=v;
        }

};


typedef pair<bool,BigInteger> bigint;   //signed BigInt
typedef pair<BigInteger,uint> divtype;  //type returned by division



BigInteger add(BigInteger v1, BigInteger v2){
  bool carry=false;
  int i; ull v;BigInteger ret;
  for (i=0;i<v1.sz || i<v2.sz;i++){
    v=(ull)(i<v1.sz?v1[i]:0)+(i<v2.sz?v2[i]:0)+carry;
    carry=v>=(1ULL<<32);
    ret.pb((uint)v);   
  }
  if (carry) ret.pb(1);
  return ret;
}

BigInteger sub(BigInteger v1, BigInteger v2){
  bool carry=false;
  int i; ull v;BigInteger ret;
  for (i=0;i<v1.sz || i<v2.sz;i++){
    v=(ull)(i<v1.sz?v1[i]:0)-(i<v2.sz?v2[i]:0)-carry;
    carry=v>=(1ULL<<32);
   
    ret.pb((uint)v);   
  }
  if (carry) ret.pb(1);
  return ret;
}



divtype div(BigInteger &v1, uint v2){
        int i; ull v=0;
        divtype ret; ret.fe.sz=0;
        for (i=v1.sz-1;i>=0;i--){
                v=v*(1ULL<<32)+v1[i];      
                ret.fe.pf(v/v2);
                v%=v2;
        }
        if (ret.fe.sz>1 && ret.fe[ret.fe.sz-1]==0) ret.fe.pop_back();
        ret.se=v;
        cout<<ret.fe.start<<' '<<ret.fe[0]<<' '<<ret.fe.sz<<' '<<ret.se<<endl;
        return ret;
}

BigInteger convert(uint val){
        BigInteger ret;ret.pb(val); return ret;
}

int compareTo(BigInteger &v1, BigInteger &v2){
        if (v1.sz!=v2.sz) return v1.sz<v2.sz?1:-1;
        for (int i=v1.sz-1;i>=0;i--)
        if (v1[i]!=v2[i]) return v1[i]<v2[i]?1:-1;
        return 0;
}

string toString(BigInteger v){
  if (v.sz==1 && v[0]==0) return "0";
  divtype p; string ret;
  BigInteger zero(0);
  int i;
 
  //while(compareTo(v,zero)!=0){
        p=div(v,1e9);
        v=p.fe;
        cout<<v.start<<' '<<v[0]<<' '<<v.sz<<' '<<p.se<<endl;
        for (i=0;i<9;i++){ret+=p.se%10+'0';p.se/=10;}
  //}
  reverse(ret.begin(),ret.end());
  for (i=0;i<ret.len-1 && ret[i]=='0';i++);
  return ret.substr(i);
}




BigInteger num[5001];
int main(){
        BigInteger u(50);
        cout<<u.sz<<endl;
        u.resize(20);
        cout<<toString(u)<<endl;
        cout<<u[0]<<endl;
return 0;
}
bugzpodder




PostPosted: Sat Mar 26, 2005 1:56 pm   Post subject: (No subject)

and i still dont get what the heck is wrong with what I did (in my first post) the second constructor calls the first, and it IS getting called, so it seems perfectly fine
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Sat Mar 26, 2005 2:04 pm   Post subject: (No subject)

Do NOT do I/O in a function like that.
bugzpodder




PostPosted: Sat Mar 26, 2005 2:04 pm   Post subject: (No subject)

??? err?? why not? its just debugging output? my code is getting SIGFPE (division/mod by 0) and SIGSEV's... these outputs pinpointed where the error is... I was wondering why the hell my code is getting SIGFPE until i found out that the variable sz mysteriously changed its value to 0 when the second constructor is called (so i made everything into one constructor... still not knowing why I cant keep two constructors like what I did before) and then I am getting an infinite loop in toString, which it turns out that the return value from div is different than the value I am getting in toString?!?! I am completely lost as to why this is happening...

anyhow i think the problem is with structs since the code has being tested if you remove the struct and use typedef deque<uint> BigInteger;

unfortunately deque is much slower... Sad and I dont get why structs are giving me these resuts.
wtd




PostPosted: Sat Mar 26, 2005 2:27 pm   Post subject: (No subject)

Because I/O is not meat to go into functions designed purely to output a new value. It's just bad. First make your changes, then check to see if things are as you expected. If you feel the need to do this inside a function, it's a good sign that function is doing too much.

Anyway, a sane basis for such work:

c++:
#include <vector>

template <typename T>
class LimitedVector : public std::vector<T>
{
   protected:
      size_t max_size;
   public:
      LimitedVector() : std::vector<T>() { }
      LimitedVector(size_t s) : std::vector<T>(s) { }   
      LimitedVector(size_t s, const T& v) : std::vector<T>(s, v) { }
      ~LimitedVector() { delete this; }

      void resize(size_t new_size)
      {
         if (new_size <= max_size)
            std::vector<T>::resize(new_size);
      }
};

class BigInteger : public LimitedVector<unsigned int>
{
   public:
      BigInteger() : LimitedVector<unsigned int>(1, *new unsigned int) { }
      BigInteger(unsigned int init_val)
      : LimitedVector<unsigned int>(1, *new unsigned int)
      {
         at(0) = init_val;
      }
      ~BigInteger() { delete this; }
};
rizzix




PostPosted: Sat Mar 26, 2005 2:37 pm   Post subject: (No subject)

bugzpodder wrote:
i do have a destructor, but didnt bother to post it. my full code is 200 lines long. Okay, so a class probably works, but what the heck is wrong with struct?


its because u have to pay close attention to the Consturctor() : initialization {} statements

although it appears to be two ways to intialize members of a class/struct in c++. there really is only one recognized official way. i suggest u create get/set accessor/mutators or simply define variables protected, for this to work out well.

also note Constructor(some_args) : Consturct() {} is the only working way to go about with calling previously defined constructs including the parent class' constuctor in c++. that is you cannot call the constuctor within the {} block of another constrcutor. although this should work, its does not always work well with all compilers.

although in D, Java, and a hell a lot of other languages, you dont have such annoying complications.
bugzpodder




PostPosted: Sat Mar 26, 2005 2:39 pm   Post subject: (No subject)

wtd, One of my original plans is to push performance, since there is just too much overhead in vectors.

rizzix, ic, well it seemed to work :S heh
rizzix




PostPosted: Sat Mar 26, 2005 2:46 pm   Post subject: (No subject)

the fist most common way in most languages:
code:
class BigInteger {
   protected:
   int sz,start,maxsize, *arr;
   
   public:
   BigInteger(){start=0;maxsize=1;sz=1; arr=new int[1];}
};


the second the more c++ specific way"
code:
class BigInteger {
   protected:
   int sz,start,maxsize, *arr;
   
   public:
   BigInteger() : start(0), maxsize(1), sz(1), arr(new int[1]) {}
};


Note that if you want to initialzise members of class using a previously defined consructor always use the second method:
code:
BigInteger(int val) : BigInteger() {arr[0]=val;}


you may if you wish, initialize member of the class directly using the second way, although the first way will work just as well.
wtd




PostPosted: Sat Mar 26, 2005 2:48 pm   Post subject: (No subject)

bugzpodder wrote:
wtd, One of my original plans is to push performance, since there is just too much overhead in vectors.


I can guarantee std::vector will be faster than what you can cook up. That's not to insult your skills. but a lot of people have invested a lot of time in making the STL fast.

Also, your use of macros, and lack of spacing in your code will not make it any faster.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 28 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: