GIDForums

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 17-Mar-2008, 20:08
Mr_Peepers Mr_Peepers is offline
New Member
 
Join Date: Feb 2008
Posts: 18
Mr_Peepers is on a distinguished road

Compiling with stubs for objects


We need to compile our code using -Wall, and although the objects in the String class don't need to be complete, they need to be valid stubs, meaning they should still accept the right input, but just return zero.

" a) Your file must contain a syntactically valid function definition
("stub") for each of the functions you are required to implement
for the project. "

I'm curious though as how to declare these 'stubs'

namely, the operators
CPP / C++ / C Code:
String& operator=( const String& RHS); 
String& operator+=( const String& RHS);

// Return string which is the concatenation of two strings
//
String operator+( const String&, const String& );

// Compare two strings (equality and relational operators)
//
bool operator==( const String&, const String& );
bool operator!=( const String&, const String& );
bool operator< ( const String&, const String& );
bool operator<=( const String&, const String& );
bool operator> ( const String&, const String& );
bool operator>=( const String&, const String& );

These stubs I'm not so sure about... perhaps just returning zero?


Also I had issues in constructing the String itself, We're not allowed to use anything from the string.h, cstring, and string directives.
CPP / C++ / C Code:
String::String(const String& RHS)
{
  Length = RHS.Length;
  Capacity = RHS.Capacity;
  Mem = RHS;
}

and we have to do the same thing, but like
CPP / C++ / C Code:
String::String(const char[] RHS)

I know I'm somewhat close with the first one, but I'm not sure how to construct the strings



....The reason I'm posting this is when I go to compile I get the following error message

ld: fatal: file /user/cse232/Projects/project07.check.o: wrong ELF machine type: EM_386
ld: fatal: File processing errors. No output written to a.out
collect2: ld returned 1 exit status

From what I've learned ELF machines means i'm unable to do something that normally the other machine can?

Help is greatly appreciated,
-Mr_Peepers
  #2  
Old 17-Mar-2008, 21:12
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 353
Peter_APIIT is on a distinguished road

Re: Compiling with stubs for objects


No idea what u talkng here.
__________________
Linux is the best OS in the world.
  #3  
Old 18-Mar-2008, 03:45
L7Sqr L7Sqr is offline
Junior Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 94
L7Sqr will become famous soon enough

Re: Compiling with stubs for objects


Regarding your 'stub' issue:
A stub usually refers to an empty definition of a function (therefore the requirement to be syntactically correct). So if you have the following declaration
CPP / C++ / C Code:
int foo (int bar);
And you wanted to create a stub for that you would produce something along the lines of
CPP / C++ / C Code:
int foo (int bar) {
   return 0;
}
Realize, though, that most of your function declarations (I am assuming the list you provided is what you have to produce stubs for) return something other than an int, so returning 0 would cause all sorts of output with the -Wall flag. As an aside, the -W (if you are using it) flag should also complain about unused arguments - so be aware when you start see that output during the compilation process.

As far as the ELF problem you are having:
It seems that you have compiled that file either on another machine or with a cross-compiler. In either case the binary does not match your system. You can simply delete the .o file and recompile the source files again. If you do not have access to the source then you can try to get to the target platform of that binary (possibly an ssh to another machine) or ask the person who generated the .o to recompile for your target platform. Realize that these are somewhat stabs in the dark as I do not know enough from what you provide to know for sure.
Last edited by admin : 18-Mar-2008 at 04:42. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #4  
Old 18-Mar-2008, 17:34
Mr_Peepers Mr_Peepers is offline
New Member
 
Join Date: Feb 2008
Posts: 18
Mr_Peepers is on a distinguished road

Re: Compiling with stubs for objects


thanks for the help on the stubs, I'm just not sure now as to how to construct these string objects

we have two ways to do it, one is by copying another string
CPP / C++ / C Code:
String::String(const String& RHS)

and the other is in a c-style approach
CPP / C++ / C Code:
String::String(const char[] RHS)

our variables for type String are
CPP / C++ / C Code:
unsigned capacity;
unsigned length;
char * mem;

I know mem is the array for the 'string' to be stored in, but what about constructing the string initially? I thought I might have had the right idea in my previous post
  #5  
Old 19-Mar-2008, 00:59
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 353
Peter_APIIT is on a distinguished road

Re: Compiling with stubs for objects


You can check the standard string library function and get idea how their implement this class.

I hope this help
__________________
Linux is the best OS in the world.
  #6  
Old 19-Mar-2008, 04:05
L7Sqr L7Sqr is offline
Junior Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 94
L7Sqr will become famous soon enough

Re: Compiling with stubs for objects


it seems to me that you have been given a class with the operations declared and you need to provide the definitions for that, yes? Given the following:
CPP / C++ / C Code:
struct String {
   unsigned capacity, length;
   char * mem;
}
you would have to do the following when constructing a string:
CPP / C++ / C Code:
String::String (const char * rhs) {
   /*
    * Determine that rhs is a valid pointer and then determine it's length. If you can
    * use strlen otherwise you will need to search the char array looking for the
    * terminating '\0' character (indicating the end of the c-style string
    * Once you know the length, assign it to the appropriate field of the String object
    */

   /*
    * Allocate memory for your new string using the size you just determined. new would
    * work just fine here, but you need to remember to delete in the destructor or you
    * will have caused a memory leak
    */

   /*
    * Once you have a fresh buffer, copy the argument into it
    */

   /*
    * The capacity should initially be the size of the string (or the next higher power of 2)
    * And as the string grows the capacity should indicate how large a chunk of memory
    * you have in 'mem' and length should indicate how much of that memory is used
    * Realize that at no time should length > capacity == true
    */
}
The fourth comment there is up for debate. You could construct a string of default size always and only change that if the argument to the constructor was greater than the default size - in that case you would check the argument, set length, set the capacity, allocate memory (size of capacity, not length), and then copy data.
In either case though, you should always increase the buffer by a power of two when growing the capacity. (google 'amortized analysis accounting method' for details)

So try and fill in each of those comment areas with the code that does what the comment says and post back with any problems.
  #7  
Old 19-Mar-2008, 12:43
Mr_Peepers Mr_Peepers is offline
New Member
 
Join Date: Feb 2008
Posts: 18
Mr_Peepers is on a distinguished road

Re: Compiling with stubs for objects


so when concatenating two of these Strings, if the capacity wouldn't hold both of them, would I create a new String object that holds both of them, or can one's capacity just be updated?

I guess logically I don't see how updating the capacity variable effects how much memory my Mem variable can hold.
  #8  
Old 19-Mar-2008, 14:35
L7Sqr L7Sqr is offline
Junior Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 94
L7Sqr will become famous soon enough

Re: Compiling with stubs for objects


It doesnt affect how much the buffer can hold, it details it. In other words, to implement the += operator you would do the following
CPP / C++ / C Code:
String& String::operator+= (const String& rhs) {
   /*
    * determine the length of rhs
    * determine the length of this
    * if capacity is less than the sum of those two values, you
    * know that you have to allocate more memory. If you 
    * listened to what I mentioned earlier, you will just allocate
    * double this->capacity and then this->capacity should get
    * a new value of twice itself (since you just increased the size
    * of the buffer)
    * Otherwise, you do not need to allocate more space, just copy
    * rhs onto the end of this and return
    */
   
   return *this;
}

Essentially the capacity is the only way you know how much room is in the buffer. Without it, you would need to calculate the length of each string, allocate the new buffer, copy both strings to the new buffer, free the old buffers (if necessary) - every time you did the operation. That would be terribly wasteful and would defeat the purpose of wrapping the details in a class to begin with.
  #9  
Old 19-Mar-2008, 15:06
Mr_Peepers Mr_Peepers is offline
New Member
 
Join Date: Feb 2008
Posts: 18
Mr_Peepers is on a distinguished road

Re: Compiling with stubs for objects


so without getting down to the nitty gritty, it's something like this?

CPP / C++ / C Code:
String& String::operator+= (const String& rhs)
{
  unsigned totalLen = rhs.Length + Length;
  if (totalLen < Capacity)
    {
      //fill the Mem[] with the rhs String
    }
  else
  {
    Capacity = totalLen; //or Capacity = Capacity*2, just something that
    //fill the Mem[] with the rhs String once Capacity has been increased
  }
  return *this;
}

also, without overloading the << operator, is there a way to print and test my constructors and the += operator?
  #10  
Old 20-Mar-2008, 03:49
L7Sqr L7Sqr is offline
Junior Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 94
L7Sqr will become famous soon enough

Re: Compiling with stubs for objects


You have the general idea. However, there is something in your comment that is not correct. It may be semantics but I believe it deserves a mention (I've added to your code my own explanation)
CPP / C++ / C Code:
String& String::operator+= (const String& rhs)
{
  unsigned totalLen = rhs.Length + Length;
  if (totalLen < Capacity)
    {
      //fill the Mem[] with the rhs String
      /*
       * You will want to place rhs.mem into this->mem starting
       * at location this->mem[this->length] since this->mem contains
       * the string for *this* object, the one we are adding to
       */
    }
  else
  {
    Capacity = totalLen; //or Capacity = Capacity*2, just something that
    //fill the Mem[] with the rhs String once Capacity has been increased
      /*
       * First, you will need a new copy of a buffer (returned by new), into that
       * you copy this->mem and then rhs.mem. Dont forget your bookeeping...
       * Adjust this->length to account for the new length of the string and free
       * the buffer that was too small before exiting this call
       */
  }
  return *this;
}
As far as seeing that everything is working as you would like, you could always just add cout statements inside the function. For example, in the above function you would have something like this:
CPP / C++ / C Code:
String& String::operator+= (const String& rhs) {
   /*
    * blah, blah, blah - do the copy stuff....
    */
   std::cout << "Inside operator+=, returning new String: " << mem << std::endl;
   return *this;
}
 

Recent GIDBlogNon-US citizens serving in the military by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with C++ Compiling Error Altmaniac CPP / C++ Forum 5 21-Aug-2007 06:29
basic question regarding compiling a program cornsnap CPP / C++ Forum 24 16-May-2007 14:08
getting an error while compiling and running using different IDE. jaro C Programming Language 0 25-Aug-2006 09:14
issues compiling .h and .cpp files with Dev C++ iceman2006 CPP / C++ Forum 9 17-May-2006 19:32
C++ Compiling Error pointer MS Visual C++ / MFC Forum 3 12-May-2005 04:08

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:34.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.