![]() |
|
#1
|
|||
|
|||
realloc in c++is there anything like realloc in c++? Because realloc is a c function and i need exactly c++.
|
|
#2
|
|||
|
|||
Re: realloc in c++Quote:
A fundamental feature of C++ language and standard library design, C Standard Library functions, including malloc(), realloc(), etc. are part of the C++ Standard Library also, and have the same functionality. In C program you #include <stdlib.h>, and in C++, you #include <cstdlib>. That being said, the C++ new operator is preferred, for a Lot of Good Reasons (although the advantages might not be obvious for arrays of simple objects). Moreover Instead of arrays of char, std::<string> objects have lots of advantages (including automatic resizing without the application program worrying about resizing the length of the array as the strings grow). Similarly, for data types other than arrays of char, std::vector objects have the same advantage: no need for the application program to manage memory with realloc(), etc., to resize the "array". Regards, Dave |
|
#3
|
|||
|
|||
Re: realloc in c++i know about cstdlib, but then how can i resize the array of objects? with realloc i'm gettings windows errors
|
|
#4
|
|||
|
|||
Re: realloc in c++Quote:
You use realloc the same way as you used it in C. In other words: Post the Code. I hate to sound like I'm harping on it, but is there any particular reason you want to use realloc() instead of just using std::vectors or other C++ standard library classes in C++? Regards, Dave |
|
#5
|
|||
|
|||
Re: realloc in c++CPP / C++ / C Code:
that's what i'm trying to do with realloc, there's no reason i want to youse realloc just i don't know those standart c++ clasess, that's what i asked at first Last edited by LuciWiz : 02-May-2007 at 03:59.
Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
|
|
#6
|
|||
|
|||
Re: realloc in c++I think realloc really is something replaced by use of the STL container classes. Instead of arrays, you'd use something like this:
CPP / C++ / C Code:
That I believe would be the equivalent of what you wrote above. Vectors manage all the memory for you. There are others, list, deque, but I have no experience or knowledge on how they work in comparison to vectors. Here's some more info on vectors: http://www.cplusplus.com/reference/stl/vector/ |
|
#7
|
|||
|
|||
Re: realloc in c++Quote:
Well, you can't do it that way. Period. Why do I say that you can't do it? Because it doesn't work (won't work; can't work). C++ std::strings are not native data types. Creation of string objects involves constructors that do certain things whose details are mercifully hidden from "normal" programmers. In C++ these details are taken care of by the 'new' operator, but not by malloc (or realloc). Merely malloc'ing enough memory to hold the pointers that represent the names of the strings (if that's what "an array of strings" is internally, and I think it is) does not create the code to actually make the strings. Declaring an array of strings does the job, but not just mallocing some bytes of memory. Array sizes in C++ are fixed (even for 'dynamically allocated' arrays), and aren't easily re-dimensioned by your program. Use of the "new" operator gives the equivalent of an array, but there is no simple way to "resize" stuff that you got from "new". If you wanted to use "new" in your C++ program for an array of class objects and you wanted to enlarge the array, you could use "new" to allocate a larger amount of memory and then copy the old stuff to the new stuff and then use "delete" to de-allocate memory for the old stuff. Guess what? These operations are taken care of noiselessly and painlessly by the vector template class. Why would you want to do it when it's already been done? For native data types (not classes), it could be done by realloc, as it is done in C. In C you have no choice. Over the years there has been discussion (heated at times) among experts, who are shaping the future of the language, about something like a "renew" operator to resize the allocated memory for general class objects. There are Very Good Reasons not to do this. On the other hand, the resize() member function for the vector class can be used to enlarge the vector arbitrarily, and could be implemented for any custom classes that you care to create on your own. Quote:
The example by dlp shows something similar to what you would do with realloc in c. It's actually easier and safer than realloc (and, by the way, it works). Note that the overloaded [] operator allows familiar notation to access the elements (looks like good old C-style arrays), and that's a Good Thing, in my opinion. However---and this is important---it does not prevent programs from crashing if they happen to try to access memory beyond the size of the vector. (That is to say that the [] operator does not do any bounds checking.) In other words, it is the responsibility of you, the programmer, to make sure not to screw it up. In C you have to keep track of the size of the array (so that you can know when it needs to be resized) and you have to pass a separate argument to functions that use the array, since there is no other way (other than a global variable) that a function can know how many elements are in the array or how many elements to use. On the other hand, unlike arrays, with vectors, there is a built-in mechanism to keep track of the current size without making you pass some parameter to functions that use the vectors. For an example similar to the one by dlp: CPP / C++ / C Code:
Output: Code:
Regards, Dave Last edited by davekw7x : 01-May-2007 at 23:38.
|
Recent GIDBlog
Last Week of IA Training by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Need help with pointer to pointer | nkhambal | C Programming Language | 5 | 21-Oct-2006 04:13 |
| Dynamic vs Static Arrays | WaltP | Miscellaneous Programming Forum | 5 | 16-Feb-2006 15:54 |
| Dynamic memory allocation of unknown/variable data length | Radi0ShacK | CPP / C++ Forum | 18 | 14-Feb-2006 21:26 |
| Incorrect usage of free() | kobi_hikri | C Programming Language | 7 | 08-Dec-2005 23:27 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The