CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 18 of 18
  1. #16
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: problems on resize an array:(

    Code:
    int ReDim(void *ag,size_t i)
    {
    	(typeid(ag).name()*) realloc (ag, i * sizeof(typeid(ag).name()));
    	return 0;
    }

    Whatever you thought this code was going to accomplish, it doesn't!

    realloc returns a pointer to the new memory and needs to be assigned. ag is a pointer to void passed by value so that even if ag is assigned to the return value of realloc, the new value won't get returned to the caller. ag either needs to be passed as a pointer to a pointer or (better) as a reference to a pointer. typeid(..).name() returns a const char * string so sizeof will always give the wrong value (and in any case doesn't provide what you think it provides!).

    As Paul already stated, why are you trying to incorrectly use the 'c' method of memory allocation when there are so much better ways of doing this in c++???? If you insist on using malloc/realloc/free then you need a good understanding of how this all hangs together and how pointers work and how to pass values back from functions etc etc etc.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

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

    Re: problems on resize an array:(

    Quote Originally Posted by Cambalinho View Post
    sorry to all.. finally i resolve it.. with very hard work and head pain(but works)
    There is one thing you need to realize:

    When you do something incorrect when it comes to memory handling, just because the program "works" doesn't mean the program is correct. In C (or C++), if you make a mistake, there may be no indication that something is wrong, until you run the program on another computer, or the program may run some time and crash another time, etc.

    That's why your attempt at trying to manage your own memory is flawed. I have never seen any code like this to allocate memory, and I've been doing C and C++ for 25 years now:
    Code:
    int ReDim(void *ag,size_t i)
    {
       (typeid(ag).name()*) realloc (ag, i * sizeof(typeid(ag).name()));
       return 0;
    }
    Now, if I've never seen anything like this before from a beginner, then you know something just isn't right.

    First, there is no such thing as typeid in the 'C' language. So why are you using it?

    Second, even if you were to do something like this in C++, you would use a template:
    Code:
    template <typename T>
    T* ReDim(void *ag,size_t i)
    {
       return (T*) realloc (ag, i * sizeof(T));
    }
    Third, why do this when again, you have std::vector? All you need to do is call the resize() method to resize the vector.
    4 - after we don't need. we must delete it.
    (sorry can anyone tell me how?)
    See what I mean when I stated you must know what you're doing? If you're asking us "where to delete the memory", then frankly, you're not ready for this type of programming where you're handling dynamically allocated memory. But again, there is no need to do this type of programming at all for your purposes.
    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:
    Your program is written poorly from the beginning. Trying to answer your question concerning a syntax error will just encourage you to do more of the same bad programming.

    Regards,

    Paul McKenzie

  3. #18
    Join Date
    Apr 2009
    Posts
    1,355

    Re: problems on resize an array:(

    Quote Originally Posted by Paul McKenzie View Post
    There is one thing you need to realize:

    When you do something incorrect when it comes to memory handling, just because the program "works" doesn't mean the program is correct. In C (or C++), if you make a mistake, there may be no indication that something is wrong, until you run the program on another computer, or the program may run some time and crash another time, etc.

    That's why your attempt at trying to manage your own memory is flawed. I have never seen any code like this to allocate memory, and I've been doing C and C++ for 25 years now:
    Code:
    int ReDim(void *ag,size_t i)
    {
       (typeid(ag).name()*) realloc (ag, i * sizeof(typeid(ag).name()));
       return 0;
    }
    Now, if I've never seen anything like this before from a beginner, then you know something just isn't right.

    First, there is no such thing as typeid in the 'C' language. So why are you using it?

    Second, even if you were to do something like this in C++, you would use a template:
    Code:
    template <typename T>
    T* ReDim(void *ag,size_t i)
    {
       return (T*) realloc (ag, i * sizeof(T));
    }
    Third, why do this when again, you have std::vector? All you need to do is call the resize() method to resize the vector.
    See what I mean when I stated you must know what you're doing? If you're asking us "where to delete the memory", then frankly, you're not ready for this type of programming where you're handling dynamically allocated memory. But again, there is no need to do this type of programming at all for your purposes.
    Your program is written poorly from the beginning. Trying to answer your question concerning a syntax error will just encourage you to do more of the same bad programming.

    Regards,

    Paul McKenzie
    thanks to all

Page 2 of 2 FirstFirst 12

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