0

I'm new to C++ and I have a question. I have an array of float* type. I also have class Color* buffer. I want to store the array's components into the buffer.(or make the buffer's pointer look at the same memory space as the array's but I'm not really familiar yet with C++ and I still cant understand pointers clearly) Is this possible? If not is there any other way to make it somehow possible?

Edit: The Color class is a class which contains all the methods to modify a PPM file's data. I need to store this data into a float array. Then I have to modify the data. But then there is a Color* buffer initialized and I can't understand how to move the data from the array into this Color* buffer in order to use it for all the methods of this Image class:
class Image { public:

protected:
    Color * buffer;                              //! Holds the image data.

    unsigned int width,                          //! The width of the image (in pixels)
        height;                      //! The height of the image (in pixels)

public:
    // metric accessors

    /*! Returns the width of the image
    */
    const unsigned int getWidth() const { return width; }

    /*! Returns the height of the image
    */
    const unsigned int getHeight() const { return height; }

    // data accessors

    /*! Obtains a pointer to the internal data.
    *
    *  This is NOT a copy of the internal image data, but rather a pointer
    *  to the internally allocated space, so DO NOT attempt to delete the pointer.
    */
    Color * getRawDataPtr();

    /*! Obtains the color of the image at location (x,y).
    *
    *  The method should do any necessary bounds checking.
    *
    *  \param x is the (zero-based) horizontal index of the pixel to get.
    *  \param y is the (zero-based) vertical index of the pixel to get.
    *
    *  \return The color of the (x,y) pixel as a Color object. Returns a black (0,0,0) color in case of an out-of-bounds x,y pair.
    */
    Color getPixel(unsigned int x, unsigned int y) const;

    // data mutators

    /*! Sets the RGB values for an (x,y) pixel.
    *
    *  The method should perform any necessary bounds checking.
    *
    *  \param x is the (zero-based) horizontal index of the pixel to set.
    *  \param y is the (zero-based) vertical index of the pixel to set.
    *  \param value is the new color for the (x,y) pixel.
    */
    void setPixel(unsigned int x, unsigned int y, Color & value);

    /*! Copies the image data from an external raw buffer to the internal image buffer.
    *
    *  The member function ASSUMES that the input buffer is of a size compatible with the internal storage of the
    *  Image object and that the data buffer has been already allocated. If the image buffer is not allocated or the
    *  width or height of the image are 0, the method should exit immediately.
    *
    *  \param data_ptr is the reference to the preallocated buffer from where to copy the data to the Image object.
    */
    void setData(const Color * & data_ptr);

    // constructors and destructor

    /*! Default constructor.
    *
    * By default, the dimensions of the image should be zero and the buffer must be set to nullptr.
    */
    Image() {};

    /*! Constructor with width and height specification.
    *
    * \param width is the desired width of the new image.
    * \param height is the desired height of the new image.
    */
    Image(unsigned int width, unsigned int height);

    /*! Constructor with data initialization.
    *
    * \param width is the desired width of the new image.
    * \param height is the desired height of the new image.
    * \param data_ptr is the source of the data to copy to the internal image buffer.
    *
    * \see setData
    */
    Image(unsigned int width, unsigned int height, const Color * data_ptr);

    /*! Copy constructor.
    *
    * \param src is the source image to replicate in this object.
    */
    Image(const Image &src);

    /*! The Image destructor.
    */
    ~Image();

    /*! Copy assignment operator.
    *
    * \param right is the source image.
    */
    Image & operator = (const Image & right);

    /*!
    * Loads the image data from the specified file, if the extension of the filename matches the format string.
    *
    * Only the "ppm" extension is supported for now. The extension comparison should be case-insensitive. If the
    * Image object is initialized, its contents are wiped out before initializing it to the width, height and data
    * read from the file.
    *
    * \param filename is the string of the file to read the image data from.
    * \param format specifies the file format according to which the image data should be decoded from the file.
    * Only the "ppm" format is a valid format string for now.
    *
    * \return true if the loading completes successfully, false otherwise.
    */

    bool load(const std::string & filename, const std::string & format);

    /*!
    * Stores the image data to the specified file, if the extension of the filename matches the format string.
    *
    * Only the "ppm" extension is supported for now. The extension comparison should be case-insensitive. If the
    * Image object is not initialized, the method should return immediately with a false return value.
    *
    * \param filename is the string of the file to write the image data to.
    * \param format specifies the file format according to which the image data should be encoded to the file.
    * Only the "ppm" format is a valid format string for now.
    *
    * \return true if the save operation completes successfully, false otherwise.
    */
    bool save(const std::string & filename, const std::string & format);

};
Celestia12
  • 13
  • 4
  • You probably would rather want a `std::vector` instead of a raw pointer. – user0042 Nov 27 '17 at 12:55
  • Why? It is an assignment and I have to make the array float*. – Celestia12 Nov 27 '17 at 12:58
  • It's hard to tell without knowing what a `Color` is. Do you have some reason to believe that it has exactly the same layout in memory as a `float`? If not, what _do_ you mean? – Useless Nov 27 '17 at 12:58
  • 4
    It looks like you need to read more about [*strict aliasing*](https://stackoverflow.com/questions/98650/what-is-the-strict-aliasing-rule). What you try to do (I think) is not valid. You can't have a pointer to one kind of data, and then use it as a pointer to some completely different kind of data. – Some programmer dude Nov 27 '17 at 12:59
  • Please show some code; right now it's unclear what you're trying to do. – AndyG Nov 27 '17 at 13:00
  • @Someprogrammerdude While not valid (in the C++ standard) such conversions are widely supported and indeed used. For example when two structs start with common field sequence. – freakish Nov 27 '17 at 13:01
  • I'm making a program that will read a ppm file, modify it's components and write a new one. Color is the class which contains all the color modifying methods. When I read the file, I had to store all the data of the ppm file into a float* array. That's what the assignment asked. Now I need to modify the data and I have to do it using a Color* buffer pointer which has been initialized already. I will post the header file in which it has been initialized – Celestia12 Nov 27 '17 at 13:07
  • @freakish Structure like that is common in *C* yes, as a way to emulate inheritance. In C++ it's much less common. It's also have to be done very carefully since mistakes in the ordering of structure members *will* lead to UB. It should also be considered not portable, not even between different compilers on the same system (and with "different compilers" I also include different versions of the same compiler). – Some programmer dude Nov 27 '17 at 13:08
  • @Celestia12 In that case you probably should read [Why isn't sizeof for a struct equal to the sum of sizeof of each member?](https://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member) as well. – Some programmer dude Nov 27 '17 at 13:08
  • I understand that what I said is impossible. There must be another way Thank you! – Celestia12 Nov 27 '17 at 13:13

0 Answers0