Jump to content

Beginners question

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
goatmafioso

goatmafioso

    Newbie

  • Members
  • Pip
  • 3 posts
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.

#2
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
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.

#3
goatmafioso

goatmafioso

    Newbie

  • Members
  • Pip
  • 3 posts
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.

#4
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts

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.
Danke.

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.
Is this restriction because it's a homework assignment? Or is it simply idiotic (certainly with a size of 5)? (Five characters? Why malloc in the first place?)

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.
C or C++? In C you shouldn't have the cast. In C++ you shouldn't be using malloc...

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.
...Most of what you are describing for increasing the size of the array, copying contents and freeing up the original, and returning a new pointer is something that realloc already does. So it's wheel reinvention here. Is that the point of this?

#5
goatmafioso

goatmafioso

    Newbie

  • Members
  • Pip
  • 3 posts
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.

#6
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts

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.
Cprogramming.com FAQ > Casting malloc