473,320 Members | 1,881 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

dynamic memory allocation question

Can you make an array of classes with dynamically allocated members?
For example:
###
class LIST {
public:
char * data;
LIST() {
data = (char *) malloc(10*sizeof(char); }
~LIST() { free(data); } };
LIST lists[52];
###
Is this legal? Also, can I reallocate the memory that was allocated in
the example?
###
lists[14].data = (char *) realloc(lists[14].data, 16*sizeof(char));
###

Thanks for any help!
-Chris

Mar 17 '06 #1
4 1457
Another question (sorry). Is it possible to dynamically allocate these
classes?
###
LIST * lists = (LIST *) malloc(sizeof(LIST));
###

Mar 17 '06 #2
Chris wrote:
Can you make an array of classes with dynamically allocated members?
Yes, *I* can.
For example:
###
class LIST {
public:
char * data;
LIST() {
data = (char *) malloc(10*sizeof(char); }
~LIST() { free(data); } };
Missing the copy constructor and the copy assignment op. Read about
"the Rule of Three".
LIST lists[52];
###
Is this legal?
Yes.
Also, can I reallocate the memory that was allocated in
the example?
###
lists[14].data = (char *) realloc(lists[14].data, 16*sizeof(char));
###


I don't know. Can you? The main question, of course, should you?
And if you need to ask all those questions, you probably shouldn't.
Use 'std::vector<char>' or 'std::string' instead.

V
--
Please remove capital As from my address when replying by mail
Mar 17 '06 #3
Chris wrote:
Another question (sorry). Is it possible to dynamically allocate these
classes?
###
LIST * lists = (LIST *) malloc(sizeof(LIST));
###


Yes, it's possible, but not this way. You _have_ to use 'new'.

V
--
Please remove capital As from my address when replying by mail
Mar 17 '06 #4
The main reason I ask these questions is because I'm trying to get
understanding, not necessarily the safest way to do it. I'm a very
amature programmer, and do it as a hobby. But thank you, I will look
into vectors.

Mar 17 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
11
by: Roman Hartmann | last post by:
hello, I do have a question regarding structs. I have a struct (profil) which has a pointer to another struct (point). The struct profil stores the coordinates of points. The problem is that I...
14
by: chai | last post by:
Can anyone help me in finding elements stored in a dynamic array in.
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
2
by: nayannovellus | last post by:
As per my knowledge i know that when a structure is defined, no memory is assigned to its members but when a variable of that structure is declared memory is allocated and also this allocation takes...
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
14
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is...
10
by: swornavidhya.mahadevan | last post by:
Which allocation (Static / Dynamic) is suitable for the situation when we are trying to allocate for a overloaded memory when the memory is full and no space to allocate. What will happen if both...
19
by: smarty | last post by:
how can I find the memory allocated dynamically? is there any possibility of finding it?
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.