Here's my situation. I'm currently writing the functions for a program, I will not be doing any of the coding of Main. I'm going to have to dynamically allocate memory using malloc for an array of characters inside of a struct each time the array fills to it's capacity. My questions are as follows:
1) One of the functions is to initialize the struct, which is simplistic normally but when working with an unspecified array size do I initialize all the components of the array as usual, assigning values etc, than use malloc on the array or do I have to malloc the entire struct and than procede to specifically malloc the array inside?
2) Each time I need to increase the size of the array inside is it possible to just do the following (Assume the struct is Q1 and the array is called Items). Q1->Items= (char *)malloc(<whatever size I need>); or do I have to go about it some other way like creating a temp array, assigning it memory and than setting Q1->Items equal to the temp.
Thank you all in advance.
Beginners question
Started by goatmafioso, Feb 08 2010 09:08 AM
5 replies to this topic
#1
Posted 08 February 2010 - 09:08 AM
|
|
|
#2
Posted 08 February 2010 - 10:40 AM
1) I would assume that if you have no size yet at initialization, that you would not initialize the array to any arbitrary size. Essentially, you'd have a size of zero and init the pointer (which will later point to dynamically allocated memory) to NULL.
2) I'd generally use realloc to reallocate memory. In fact, if used with what I said above, you could just use realloc and skip malloc.
2) I'd generally use realloc to reallocate memory. In fact, if used with what I said above, you could just use realloc and skip malloc.
#3
Posted 08 February 2010 - 11:03 AM
That makes perfect sense, and I thank you for your reply. Unfortunately, i'm being limited in what I can use to complete this specific program. Here is a bit more information to possibly give you an idea of what i'm trying to get going.
The basic idea is that there will be an array of characters inside of a struct. The array of characters will have a maximum size of, say, 5. The definition of the struct cannot be changed and it is listed as "char *items;". I can only use malloc to dynamically assign my memory.
This leads me to believe that when I call the function to initialize everything within the struct I will just do so normally than use malloc to assign the value for items like so: struct->items=(char *)malloc(sizeof(char)*5);. This should give me what i'm looking for. The issue, now, comes up that if another function in the program attempts to add a character into the array and it is full it must dynamically double the size of the array.
My belief is that I should just create a new array with double the size using malloc, copy the contents of the old array into the new array, free the old array and set the point of struct->items to the new array. My issue is I can't seem to hit on the right syntax for copying the data over to the temporary array that I used malloc to create.
The basic idea is that there will be an array of characters inside of a struct. The array of characters will have a maximum size of, say, 5. The definition of the struct cannot be changed and it is listed as "char *items;". I can only use malloc to dynamically assign my memory.
This leads me to believe that when I call the function to initialize everything within the struct I will just do so normally than use malloc to assign the value for items like so: struct->items=(char *)malloc(sizeof(char)*5);. This should give me what i'm looking for. The issue, now, comes up that if another function in the program attempts to add a character into the array and it is full it must dynamically double the size of the array.
My belief is that I should just create a new array with double the size using malloc, copy the contents of the old array into the new array, free the old array and set the point of struct->items to the new array. My issue is I can't seem to hit on the right syntax for copying the data over to the temporary array that I used malloc to create.
#4
Posted 08 February 2010 - 01:49 PM
goatmafioso said:
That makes perfect sense, and I thank you for your reply. Unfortunately, i'm being limited in what I can use to complete this specific program. Here is a bit more information to possibly give you an idea of what i'm trying to get going.
goatmafioso said:
The basic idea is that there will be an array of characters inside of a struct. The array of characters will have a maximum size of, say, 5. The definition of the struct cannot be changed and it is listed as "char *items;". I can only use malloc to dynamically assign my memory.
What I described previously make no change to the struct, so that's a moot point.
goatmafioso said:
This leads me to believe that when I call the function to initialize everything within the struct I will just do so normally than use malloc to assign the value for items like so: struct->items=(char *)malloc(sizeof(char)*5);. This should give me what i'm looking for. The issue, now, comes up that if another function in the program attempts to add a character into the array and it is full it must dynamically double the size of the array.
goatmafioso said:
My belief is that I should just create a new array with double the size using malloc, copy the contents of the old array into the new array, free the old array and set the point of struct->items to the new array. My issue is I can't seem to hit on the right syntax for copying the data over to the temporary array that I used malloc to create.
#5
Posted 08 February 2010 - 08:54 PM
Yes, it was an assignment which is why it was such a pain. If we didn't have such bizarre restrictions on the assignment to simply prove a point it would have been cake. I did, finally, get it running just fine so thank you for that.
On a side note, I thought in C that malloc returned a void pointer meaning, while not necessary in a lot of cases, it's good practice to typecast it.
On a side note, I thought in C that malloc returned a void pointer meaning, while not necessary in a lot of cases, it's good practice to typecast it.
#6
Posted 09 February 2010 - 07:08 AM
goatmafioso said:
On a side note, I thought in C that malloc returned a void pointer meaning, while not necessary in a lot of cases, it's good practice to typecast it.


Sign In
Create Account

Back to top









