GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-May-2007, 13:30
baccardi baccardi is offline
Junior Member
 
Join Date: Mar 2006
Posts: 44
baccardi is on a distinguished road

realloc in c++


is there anything like realloc in c++? Because realloc is a c function and i need exactly c++.
  #2  
Old 01-May-2007, 13:59
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: realloc in c++


Quote:
Originally Posted by baccardi
is there anything like realloc in c++? Because realloc is a c function and i need exactly c++.

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  
Old 01-May-2007, 14:03
baccardi baccardi is offline
Junior Member
 
Join Date: Mar 2006
Posts: 44
baccardi is on a distinguished road

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  
Old 01-May-2007, 14:32
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: realloc in c++


Quote:
Originally Posted by baccardi
i know about cstdlib, but then how can i resize the array of objects? with realloc i'm gettings windows errors

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  
Old 01-May-2007, 14:40
baccardi baccardi is offline
Junior Member
 
Join Date: Mar 2006
Posts: 44
baccardi is on a distinguished road

Re: realloc in c++


CPP / C++ / C Code:
string *a;

 a = (string*) malloc(sizeof(string) * 2); //two elements
 a[0] = "hi";
 a[1] = "bye";
 a = (string*) realloc(a, sizeof(string) * 3);
 a[2] = "three";

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  
Old 01-May-2007, 19:27
dlp dlp is offline
Junior Member
 
Join Date: May 2006
Posts: 88
dlp will become famous soon enough

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:
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector< string > a(2); // creates a vector of 2 string
    a[0] = "hi"; // has array style access to data
    a[1] = "bye";
    a.resize(3); // Makes vector size 3
    a[2] = "three";
    return 0;
}

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  
Old 01-May-2007, 22:09
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: realloc in c++


Quote:
Originally Posted by baccardi
that's what i'm trying to do with realloc

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:
Originally Posted by baccardi
there's no reason i want to youse realloc just i don't know those standart c++ clasess
Instead of trying to learn the absolute least amount that will allow you to try to do something that you are comfortable with (but doesn't work), I respectfully suggest that you embrace the advantages of not having to worry about malloc and realloc, etc. (These mechanisms or something equivalent to them are undoubtedly involved in the implementations of vectors and other container classes in C++, but it is done behind the scenes.)

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:
#include <iostream>
#include <vector>
using namespace std;

// a function to print the elements of a vector of strings
void PrintStringVector(vector<string>);
int main()
{
    vector<string> a;   // creates an empty vector of strings

    a.push_back("hi");    // now a has one element
    cout << a.size() << ":" << endl;
    PrintStringVector(a);

    a.push_back("bye");   // now it has two
    cout << a.size() << ":" << endl;
    PrintStringVector(a);

    a.push_back("three"); // now it has three
    cout << a.size() << ":" << endl;
    PrintStringVector(a);

    return 0;
}
void PrintStringVector(vector<string>v)
{
    for (unsigned i = 0; i < v.size(); i++) {
        cout << "  v[" << i << "] = " << v[i] << endl;
    }
    cout << endl;
}

Output:
Code:
1: v[0] = hi 2: v[0] = hi v[1] = bye 3: v[0] = hi v[1] = bye v[2] = three

Regards,

Dave
Last edited by davekw7x : 01-May-2007 at 23:38.
 
 

Recent GIDBlogLast Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 22:37.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.