September 2, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Hottest Forum Q&A on CodeGuru - December 29th

  • December 29, 2003
  • By Sonu Kapoor
  • Send Email »
  • More Articles »

Introduction:

Lots of hot topics are covered in the Discussion Forums on CodeGuru. If you missed the forums this week, you missed some interesting ways to solve a problem. Some of the hot topics this week include:


How do I port VC++ code to Linux? (top)

Thread:

vir_123 wrote some code in VC++. Now, he needs to convert that code for a Linux OS. How can he do that?

I have developed VC++ applications on the Windows platform.
I want to convert it to the Linux platform. Is any cross complier
method to convert it to the Linux platform? Please suggest an easy
method to do so.

Several questions come to mind before you can convert any VC++ code to a Linux OS.

  • Is your program graphical?
  • Do you use MFC?
  • Do you use ATL?
  • Do you use straight WIN32 functions?
  • On what system components does the program rely?
  • What does the program do?

Porting an application from VC++ can be easy, but you have to be careful that you use APIs that can be run on both OSes (Windows as well as Linux). If you still want to use Microsoft APIs, you can try WINE (a Windows emulator). Or, you should use wxWindows, a cross-platform GUI API. See the Web site for more info.


How can I use STL with Linux? (top)

Thread:

This is an another Linux question. Here, KnNeeded needs to run his simple "hello world" program on a Linux system. But, unfortunately, he did not know how to use the STL in Linux. Do you?

I am trying to port my current C++ and C skills to Linux
programming. I have successfully created the standard Hello World
starting project in C for Linux, using both files and standard
output.

I am trying to do something simular using the standard C++
libraries. I tried to use the iostream template class in the STL
library. I don't whether it is supported in Linux; however, I can
seem to use #include <iostream> without a linking error being
generated. The linker throughs an error "Undefined reference" when
I write the complete Hello World program:
#include <iostream>
using namespace std;

int main(void)
{
    cout << "\n Hello World";
}
I am at the moment thinking the problem is that I am not linking the
program with the required stl library. However, I have no idea how
to do this in Linux.

I have look through the standard included documentation but cannot
find a reference of the libraries included in Linux (Red Hat 8.0).
Does anyone know of a good reference on the C++ libraries in Linux?
Is this my problem or does Linux not support STL?

I am new to Linux programming so I am at the moment only really
looking for a good reference site. Thanks in advance.

You can download the STLport here for free. An installation instruction is included.


How do I make a function that allocates memory for you? (top)

Thread:

yiannakop does have some problems with his function. He is trying to allocate memory in a function; it should not be that difficult. But, the problem is in passing the parameters to the function. Here is his problem:

Suppose I want to implement a function like this:
void function_name(int length, short *array)
{
  array = (short*)malloc(sizeof(short)*length);
  // do something
}
This, unfortunately, doesn't work. To make it work, I must place the
allocation line ( array = (short*)malloc(sizeof(short)*length ) in
my main(), just before calling the function. Why is this happening?

MrViggy presented the solution very well. Here is what he says about that problem:

If you are trying to get the function to allocate memory for you, you either need to pass the array pointer by value, or pass in a pointer to the pointer to the array. Currently, you are passing the array pointer by reference; therefore, the function will not be able to modify it.

Pass by value:

void function_name(int length, short* &array)
{
  array = (short*)malloc(sizeof(short)*length);
}

Or, pointer to pointer:

void function_name(int length, short **array)
{
  *array = (short*)malloc(sizeof(short)*length);
}

int main()
{
  short *myShort;
  function_name(3, &myShort);
}


How can I create a 3D vector? (top)

Thread:

AfricanDude knows how to create a 2D vector, but unfortunately, he has some problems with creating a 3D vector.

I am trying to use the STL vector multi-dimenstional. I know how 
to do 2D, but I want to use 3D.

This is what I have tried:
vector < vector <vector <int> >> m(3, vector< vector<int>
                              >(4,5));
I also tried the following:
vector < vector <vector <int> >> m(3, vector<int> (5, vector<int>
(4)));
I get the similar error as shown above. Can you please shed some
light? Also, is there another way to avoid using a 3D vector if I
want to use it for the following?
int i,j,k;
float a[i][j][k]=value;
Of course, I would like to vary the size of array dynamically.
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\
vector(357): error C2664: 'std::vector<_Ty>::_Construct_n' :
cannot convert parameter 2 from 'int' to 'const std::vector<_Ty> &'
with
[
_Ty=std::vector<int>
]
and
[
_Ty=int
]

The answer was provided by Paul McKenzie:

Use typedefs:

#include <vector>
// To much work!!
std::vector < std::vector <std::vector <int> > >
m(3,std::vector< std::vector<int> >(4, std::vector<int>(4,0)));

// Easier to maintain
typedef std::vector<int> Int1D;
typedef std::vector<Int1D> Int2D;
typedef std::vector<Int2D> Int3D;

int main()
{
   Int3D My3DArray(10, Int2D(10, Int1D(10,0)));
}

The reason for the typedefs is that, at some point, you may need an iterator. When you define the iterator, what's easier?

std::vector< std::vector< std::vector<int> > >::iterator it;

or

Int3D::iterator it;


How do I return a vector from a function? (top)

Thread:

halmark6Z is new to C++. His background is actually Java. But, now he needs to write some C++ code in which he wants to return a vector in a fuction.

I'm rather new to C++ (I'm changing from Java to C++), so please
bear with me. I have a question about STL and returning a vector
array from a static method.

How do I do it correctly, so that the function contructs a vector
that can be returned to the caller? I know if I create a local
object from a class and return its reference, it will point to null
(if I'm right) after the function call. So, the object should be
created with the new operator. But, how do I do it with a template
call? I just get errors when I try to compile.

It should go something like this (please, do correct my syntax.
Pointers and references are used very wrong, I think):
static &vector<MyClass> loadFromFile()
{
  vector<MyClass> *data = NULL;
  try
  {
    // load from file and populate the vector
  }
  catch(...)
  { }
  return &data;
}

The perfect answer is again provided by Paul McKenzie:

A vector has proper copy semantics, so there is no need to use references or pointers.

#include <vector>
class MyClass
{
};

static std::vector<MyClass> loadFromFile();    // if you really need
                                               // a static function

std::vector<MyClass> loadFromFile()
{
  std::vector<MyClass> data;
  try
  {
    // load from file and populate the vector
  }
  catch(...)
  { }
  return data;
}

int main()
{
   std::vector<MyClass> M;
   M = loadFromFile();
}

The code is much more easier than you thought it would be.

I know if I create a local object from a class and return its
reference,

Return it by value (as my code does above), not by reference.

It will point to null (if I'm right) after the function call. So,
the object should be created with the new operator.
You're right, you do come from a Java background. You do not need to create an object with "new" in C++.







Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel