[RESOLVED] problems on resize an array:(
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: [RESOLVED] problems on resize an array:(

  1. #1
    Join Date
    Apr 2009
    Posts
    733

    [RESOLVED] problems on resize an array:(

    i have 1 class with several class's and 1 struture:
    Code:
    struct Images
    {    
        HBITMAP ImageImage;
        BITMAP Imagebm;
        HDC ImagehdcMem;
        HBITMAP ImageMaskImage;
        BITMAP ImageMaskbm;
        HDC ImageMaskhdcMem;
    };
    
    //the subclass
    class subimages
    {
        public: int ActualSubimage;
        public: int TotalSubimages;
        public: struct Images *SubImages;
    ....................................
    //how i use it:
    ..................
    sprite2 a,b,c;
    a.Visible =true;
    a.Direction=Left;
    a.Images.Image1.Stop.SubImages=(struct Images)malloc(1 * sizeof(struct Images));
    ....................
    but i get these error:
    "--------------------Configuration: Sprite2 - Win32 Debug--------------------
    Compiling...
    Test Sprite2.cpp
    C:\Users\Joaquim\Documents\Visual C 98\Sprite2\Test Sprite2.cpp(23) : error C2440: 'type cast' : cannot convert from 'void *' to 'struct Images'
    No constructor could take the source type, or constructor overload resolution was ambiguous
    Error executing cl.exe.

    Sprite2.exe - 1 error(s), 0 warning(s)"
    i try search more code, but i get more errors
    can anyone advice me?

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

    Re: problems on resize an array:(

    Why are you using malloc instead of new?

    Why are you multiplying by 1?

    Most importantly, malloc returns a pointer. Why are you casting it to a struct?

  3. #3
    Join Date
    Apr 2009
    Posts
    733

    Re: problems on resize an array:(

    Quote Originally Posted by GCDEF View Post
    Why are you using malloc instead of new?

    Why are you multiplying by 1?

    Most importantly, malloc returns a pointer. Why are you casting it to a struct?
    you have right the 'new' is more easy to use, but i get errors too
    by 1, because, in these test, i only need 1 subimage
    Code:
    a.Images.Image1.Stop.SubImages= new Images[1];
    i execute the program but the windows give me an errror and then close it(sorry not a normal error message). and in subclass i have 1 pointer like you see that code.
    i'm trying but seems that is more complicaded with struct that normal types

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

    Re: problems on resize an array:(

    What is a.Images.Image1.Stop.SubImages?
    What's the point of creating an array of size 1?

    Your code seems pretty confused.

  5. #5
    Join Date
    Apr 2009
    Posts
    733

    Re: problems on resize an array:(

    Quote Originally Posted by GCDEF View Post
    What is a.Images.Image1.Stop.SubImages?
    What's the point of creating an array of size 1?

    Your code seems pretty confused.
    sorry about that.. i will try do my best(i'm portuguese)
    i'm doing a class Sprite these class have several subclass's for be more easy to use
    sprite a;
    a.Images.Image1.Stop.SubImages....;
    the SubImages word is 1 variable from a struture:
    Code:
    struct Images
    {    
        HBITMAP ImageImage;
        BITMAP Imagebm;
        HDC ImagehdcMem;
        HBITMAP ImageMaskImage;
        BITMAP ImageMaskbm;
        HDC ImageMaskhdcMem;
    };
    like you see the SubImages is a Images type\structure. but i need do a dinamic array, because not all animations have 100 subimages. and a good programmer don't use so many memory, if he don't need
    i try use the 'new' keyword(from C++) and malloc() function(from C), but without sucess. maybe because i'm using a struture.
    i can share all the code
    please tell me something for i start see\read. and loking for some code it's very complicated, because theres several C\C++ versions and not all code works on Visual C++ 6(i have the 2010 too).
    sorry if my english is bad, but i'm trying
    Last edited by Cambalinho; April 25th, 2013 at 10:25 AM.

  6. #6
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,755

    Re: problems on resize an array:(

    i execute the program but the windows give me an errror and then close it(sorry not a normal error message).
    What error? What debugging of the program have you done to find out what is the cause of the error? Which line in the program is giving the error? If you want our help you'll need to provide more info than just 'gives an error' and also you'll need to post the code - not just a few lines - so that we can look at it (properly formatted with code tags please).
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

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

    Re: problems on resize an array:(

    Quote Originally Posted by Cambalinho View Post
    sorry about that.. i will try do my best(i'm portuguese)
    i'm doing a class Sprite these class have several subclass's for be more easy to use
    sprite a;
    a.Images.Image1.Stop.SubImages....;
    the SubImages word is 1 variable from a struture:
    Code:
    struct Images
    {    
        HBITMAP ImageImage;
        BITMAP Imagebm;
        HDC ImagehdcMem;
        HBITMAP ImageMaskImage;
        BITMAP ImageMaskbm;
        HDC ImageMaskhdcMem;
    };
    like you see the SubImages is a Images type\structure. but i need do a dinamic array, because not all animations have 100 subimages. and a good programmer don't use so many memory, if he don't need
    i try use the 'new' keyword(from C++) and malloc() function(from C), but without sucess. maybe because i'm using a struture.
    i can share all the code
    please tell me something for i start see\read. and loking for some code it's very complicated, because theres several C\C++ versions and not all code works on Visual C++ 6(i have the 2010 too).
    sorry if my english is bad, but i'm trying
    Your English is fine. I'd get away from VC 6 and focus on 2010. Then you can take advantage of STL and dynamic arrays such as vector. If you're using MFC, even in VC 6 you can use their dynamic arrays such as CPtrArray or CObArray.

    FWIW, it's not necessarily true that you don't use more memory than you need. An fixed array of size 100 that just stores pointers, even if you don't use them all, isn't really an excessive use of memory.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,444

    Re: problems on resize an array:(

    Quote Originally Posted by Cambalinho View Post
    i have 1 class with several class's and 1 struture:
    Code:
    struct Images
    {    
        HBITMAP ImageImage;
        BITMAP Imagebm;
        HDC ImagehdcMem;
        HBITMAP ImageMaskImage;
        BITMAP ImageMaskbm;
        HDC ImageMaskhdcMem;
    };
    
    //the subclass
    class subimages
    {
        public: int ActualSubimage;
        public: int TotalSubimages;
        public: struct Images *SubImages;
    ....................................
    //how i use it:
    ..................
    sprite2 a,b,c;
    a.Visible =true;
    a.Direction=Left;
    a.Images.Image1.Stop.SubImages=(struct Images)malloc(1 * sizeof(struct Images));
    ....................
    but i get these error:
    "--------------------Configuration: Sprite2 - Win32 Debug--------------------
    Compiling...
    Test Sprite2.cpp
    C:\Users\Joaquim\Documents\Visual C 98\Sprite2\Test Sprite2.cpp(23) : error C2440: 'type cast' : cannot convert from 'void *' to 'struct Images'
    No constructor could take the source type, or constructor overload resolution was ambiguous
    Error executing cl.exe.

    Sprite2.exe - 1 error(s), 0 warning(s)"
    i try search more code, but i get more errors
    can anyone advice me?
    The biggest advice is to improve your C++ skills. Regardless of what you want to do, you can't throw together a program in C++ that requires knowledge beyond the basics and have it work.

    First, I would suggest renaming your struct. You called it "Images" when it really can be only one "Image".

    Second:
    Code:
    #include <vector>
    #include <memory>
    
    struct ImageInfo
    {    
        HBITMAP ImageImage;
        BITMAP Imagebm;
        HDC ImagehdcMem;
        HBITMAP ImageMaskImage;
        BITMAP ImageMaskbm;
        HDC ImageMaskhdcMem;
    };
    
    typedef std::shared_ptr<ImageInfo>  ImageInfoPtr;
    typedef std::vector<ImageInfoPtr> ImageInfoPtrVect;
    So you now have two things here:

    1) You have a smart pointer of ImageInfo called ImageInfoPtr.
    2) You can now store multiple ImageInfoPtr into a vector of type ImageInfoPtrVect. This will be the dynamic array.
    Code:
    ImageInfoPtrVect imVector;
    ImageInfoPtr imPtr = ImageInfoPtr(new ImageInfo);
    //..now assume imPtr has been set up with its information
    //... add it to our vector
    imVector.push_back( imPtr );
    So what does this give you?

    1) You now have a dynamic array of Images
    2) The memory management is controlled by the shared_ptr. When the last reference is gone, the memory manager takes care of deleting the memory.

    For 2), you may want to use unique_ptr instead of shared_ptr for your purposes. But regardless, this is how an experienced C++ programmer would approach this issue -- that is to use the standard library, containers, and smart pointers to simplify the work. Approaching the problem as if it's low-level 'C' programming as you did in your attempts is not the way to go about this.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Apr 2009
    Posts
    733

    Re: problems on resize an array:(

    Quote Originally Posted by Paul McKenzie View Post
    The biggest advice is to improve your C++ skills. Regardless of what you want to do, you can't throw together a program in C++ that requires knowledge beyond the basics and have it work.

    First, I would suggest renaming your struct. You called it "Images" when it really can be only one "Image".

    Second:
    Code:
    #include <vector>
    #include <memory>
    
    struct ImageInfo
    {    
        HBITMAP ImageImage;
        BITMAP Imagebm;
        HDC ImagehdcMem;
        HBITMAP ImageMaskImage;
        BITMAP ImageMaskbm;
        HDC ImageMaskhdcMem;
    };
    
    typedef std::shared_ptr<ImageInfo>  ImageInfoPtr;
    typedef std::vector<ImageInfoPtr> ImageInfoPtrVect;
    So you now have two things here:

    1) You have a smart pointer of ImageInfo called ImageInfoPtr.
    2) You can now store multiple ImageInfoPtr into a vector of type ImageInfoPtrVect. This will be the dynamic array.
    Code:
    ImageInfoPtrVect imVector;
    ImageInfoPtr imPtr = ImageInfoPtr(new ImageInfo);
    //..now assume imPtr has been set up with its information
    //... add it to our vector
    imVector.push_back( imPtr );
    So what does this give you?

    1) You now have a dynamic array of Images
    2) The memory management is controlled by the shared_ptr. When the last reference is gone, the memory manager takes care of deleting the memory.

    For 2), you may want to use unique_ptr instead of shared_ptr for your purposes. But regardless, this is how an experienced C++ programmer would approach this issue -- that is to use the standard library, containers, and smart pointers to simplify the work. Approaching the problem as if it's low-level 'C' programming as you did in your attempts is not the way to go about this.

    Regards,

    Paul McKenzie
    sorry i'm learning C by me... then i will learn C++
    sorry can you give me a nice link about these for i understand more?
    thanks for all

  10. #10
    Join Date
    Apr 1999
    Posts
    27,444

    Re: problems on resize an array:(

    Quote Originally Posted by Cambalinho View Post
    sorry i'm learning C by me...
    Then you should be using a 'C' compiler only. You are mistakingly using a C++ compiler, and using a C++ compiler that uses C++ rules to learn 'C' programming is totally wrong.
    Sprite2.cpp
    //...
    class subimages
    This is in your first post. This means that the program is a C++ program. If you're using Visual C++, and you haven't changed any of the IDE options, any file with a .cpp extension is a C++ program. In addition, there is no such thing as a class in the 'C' language.

    Either rename your file with a .C extension, or force Visual Studio to apply the rules of 'C' to your program.
    then i will learn C++
    If your goal is to learn C++ programming, learning 'C' is not a requirement. Either learn C, or learn C++.

    http://www.parashift.com/c++-faq/don...n-c-first.html

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; April 25th, 2013 at 12:09 PM.

  11. #11
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,640

    Re: problems on resize an array:(

    You are not casting the return value from malloc to the correct type:

    Code:
    a.Images.Image1.Stop.SubImages=(struct Images *)malloc(1 * sizeof(struct Images));

  12. #12
    Join Date
    Apr 2009
    Posts
    733

    Re: problems on resize an array:(

    Quote Originally Posted by Paul McKenzie View Post
    Then you should be using a 'C' compiler only. You are mistakingly using a C++ compiler, and using a C++ compiler that uses C++ rules to learn 'C' programming is totally wrong.
    This is in your first post. This means that the program is a C++ program. If you're using Visual C++, and you haven't changed any of the IDE options, any file with a .cpp extension is a C++ program. In addition, there is no such thing as a class in the 'C' language.

    Either rename your file with a .C extension, or force Visual Studio to apply the rules of 'C' to your program.
    If your goal is to learn C++ programming, learning 'C' is not a requirement. Either learn C, or learn C++.

    http://www.parashift.com/c++-faq/don...n-c-first.html

    Regards,

    Paul McKenzie
    thanks i didn't knew
    more quickly i start for C++

  13. #13
    Join Date
    Apr 2009
    Posts
    733

    Re: problems on resize an array:(

    Quote Originally Posted by Philip Nicoletti View Post
    You are not casting the return value from malloc to the correct type:

    Code:
    a.Images.Image1.Stop.SubImages=(struct Images *)malloc(1 * sizeof(struct Images));
    sorry i'm trying do these thing, but now i have a head pain
    and i, always, get errors

  14. #14
    Join Date
    Apr 1999
    Posts
    27,444

    Re: problems on resize an array:(

    Quote Originally Posted by Cambalinho View Post
    sorry i'm trying do these thing, but now i have a head pain
    and i, always, get errors
    That is because handling dynamic memory requires that you know what you're doing. You can't just hope to get it right, you must know that it's right. You must start out with something very simple, and know what you're doing before going further. In addition, where are you freeing the memory? You have to do that also, or else it's a memory leak.

    However, if you went straight to C++, you have very little, if any contact with malloc(). Look at the code I wrote -- you don't see one call to malloc() because it's not needed. I have a dynamic array, and I can now go forward with the rest of the program, while you're still having problems. So what is your goal, to write a program that processes images, or fight with malloc()?

    Regards,

    Paul McKenzie

  15. #15
    Join Date
    Apr 2009
    Posts
    733

    Re: problems on resize an array:(

    Quote Originally Posted by Paul McKenzie View Post
    That is because handling dynamic memory requires that you know what you're doing. You can't just hope to get it right, you must know that it's right. You must start out with something very simple, and know what you're doing before going further. In addition, where are you freeing the memory? You have to do that also, or else it's a memory leak.

    However, if you went straight to C++, you have very little, if any contact with malloc(). Look at the code I wrote -- you don't see one call to malloc() because it's not needed. I have a dynamic array, and I can now go forward with the rest of the program, while you're still having problems. So what is your goal, to write a program that processes images, or fight with malloc()?

    Regards,

    Paul McKenzie
    sorry to all.. finally i resolve it.. with very hard work and head pain(but works)

    how i did:

    1 - i must create a pointer in my class with my struture name(my sample is these way, but we can use any type and class's elements):
    Code:
    public: ImageInfo *SubImages;
    ImageInfo is my struture name.

    2 - now we must put that pointer to null:
    Code:
    classname class1;
    class1.SubImages=NULL;
    3 - now we can use realloc() for resize the array(i think that malloc() can be used too):
    Code:
    class1.SubImages = (ImageInfo*) realloc (class1.SubImages, 1 * sizeof(ImageInfo));
    the '1' is for tell you that i only need with 1 element on array

    4 - after we don't need. we must delete it.
    (sorry can anyone tell me how?)

    5 - if you execute the program and the windows gives you an error and close the program, see if you have another variable with that pointer. the pointer is declared but without size(elements), then it can't be used normaly, only after give it elements(i have tested without know it)

    anotherthing: i'm trying do a nice function for resize arrays, but i get some errors:
    Code:
    int ReDim(void *ag,size_t i)
    {
    	(typeid(ag).name()*) realloc (ag, i * sizeof(typeid(ag).name()));
    	return 0;
    }
    error message:
    "--------------------Configuration: Sprite2 - Win32 Debug--------------------
    Compiling...
    Test Sprite2.cpp
    C:\Users\Joaquim\Documents\Visual C 98\Sprite2\Test Sprite2.cpp(12) : error C2059: syntax error : ')'
    Error executing cl.exe.

    Sprite2.exe - 1 error(s), 0 warning(s)"
    strange error... i have 14 parenteses... they are multiply of 2, then why the error?
    and here how use it:
    Code:
    ReDim(&class1.SubImages,1);

Page 1 of 2 12 LastLast

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center