CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2013
    Posts
    6

    can someone please point out what im doing wrong

    im trying to create a class type structure using struct instead of classes i dont know what im doing wrong i have one working code and i have some thing
    else im working on.

    working
    Code:
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    
    struct myclass
    {
       int * array;
       int nelements;
    };
    
    struct myclass constructor(struct myclass n,int size)
    {
       int * array = NULL;
        array = (int *)malloc(size * sizeof(int));
        if(array == NULL)
        {
          cerr << "memory could not be allocated." <<endl;
        }else 
        {
          cout << "memory allocated." <<endl;
        }
        
        n.array = array;
        n.nelements = size;
        
        return n;
    }
    
    void initializeobjects(int * array,int size)
    {
      for(int i = 0; i < size; i++)
      array[i] = rand() % 100 + 1;
    }
    
    void printobjects(int * array,int size)
    {
         for(int i = 0; i < size; i++)
         {
           cout << " value "  <<array[i] << endl;
         }
    }
    
    
    
    int main()
    {
        struct myclass m;
        m.nelements = 12;
        
        m = constructor(m,m.nelements);
        initializeobjects(m.array,m.nelements);
        printobjects(m.array,m.nelements);
        
        system("pause");
        return 0;
    }
    im trying to do the same thing only using arrays instead. the program malfunction

    Code:
    #include <iostream>
    
    using namespace std;
    
    struct myclass
    {
       int array[];  //size not defined
       int nelements;
    };
    
    void constructor(int * array,int size)
    {
        array = NULL;
        array = (int *)malloc(size * sizeof(int)); //size defined here
    }
    
    void initializeconstrut(int * array,int size)
    {
      for(int i = 0; i < size; i++)
      array[i] = i;
    }
    
    void print(int * array,int size)
    {
         for(int i = 0; i < size; i++)
         {
           cout << " value "  <<array[i] << endl;
         }
    }
    
    int main()
    {
        struct myclass m;
        m.nelements = 12;
        
        constructor(m.array,m.nelements);
        initializeconstrut(m.array,m.nelements);
        print(m.array,m.nelements);
        
        system("pause");
        return 0;
    }
    guess what im asking is using the pointer in the
    first code section better or is there another way'
    i dont know about making the second code work.

    thanks and best regards.
    Last edited by terrorofdeath; April 16th, 2013 at 04:56 PM.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: can someone please point out what im doing wrong

    Quote Originally Posted by terrorofdeath View Post
    im trying to create a class type structure using struct instead of classes i dont know what im doing wrong i have one working code and i have some thing
    else im working on.
    1) Learn to use vector.

    2) Do not call your member variable "array". There is already a standard class called "array" in the C++ library, and using the name "array" can be confusing.

    3) In C++, there is no need to redundantly use the keyword struct. That is a holdover from 'C' programming.

    4) Why are you using malloc() in a C++ program, when there is operator new[]? Better yet, skip new[] and use vector.

    5) Why is there no call to free()?

    Here is your entire attempt, using vectors. Note how much simpler this is:
    Code:
    #include <iostream>
    #include <vector>
    #include <stdlib.h>
    
    using namespace std;
    
    void print(int *pArray, int size)
    {
         for(int i = 0; i < size; i++)
              cout << " value "  << pArray[i] << endl;
    }
    
    int main()
    {
        std::vector<int> myVect(12);
        for (size_t i = 0; i < myVect.size(); ++i )
            myVect[i] = i;
        print( &myVect[0], myVect.size());
    }
    I kept the print() function to show you that usage of vector is compatible with usage of arrays. Also, no calls to malloc(), free(), new[]/delete[], etc.

    Regards,

    Paul McKenzie

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: can someone please point out what im doing wrong

    You've got all kinds of problems there.

    What's with the function called constructor? I don't feel like writing a book on constructors, but that's not even remotely close to how they work. You should read up on passing by value vs passing by reference also. Go back to chapter one and start again.

  4. #4
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: can someone please point out what im doing wrong

    in C++ struct and class are pretty much identical except for the fact that a struct is public by default and a class is protected by default
    so for most intents and purposes...

    a struct is equivalent to
    Code:
    class Struct
    {
    public:
     // struct stuff here
    };
    or the other way around, a class is equivalent to
    Code:
    struct Class
    {
    protected:
     // class stuff here
    };
    note, they aren't flat out interchangable, there is still a type difference of course.


    Or putting it another way, if you know how to work with classes, you know how to work with structs and the other way around.
    Last edited by OReubens; April 17th, 2013 at 10:05 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured