-
April 25th, 2013, 04:48 PM
#13
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)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|