Dynamic memory allocation and structure fields

In summary: Please take a look at my program. Please help.In summary, the problem is that you don't know how to use pointers, and you need to learn more about them before you try to write code.
  • #1
pairofstrings
411
7
Please take a look at my program. Please help.

I want to allocate memory dynamically for one field in structure.
I tried to write the code but it has error. I posting a paint document for you to see. Help me please. Please tell me why is my code not working properly and what needs to be done.
Thank you.

dmaandstructures.png
 
Technology news on Phys.org
  • #2
pairofstrings said:
Please take a look at my program. Please help.

I want to allocate memory dynamically for one field in structure.
I tried to write the code but it has error. I posting a paint document for you to see. Help me please. Please tell me why is my code not working properly and what needs to be done.
Thank you.

dmaandstructures.png
Is this homework? It looks a little too pro.

Your problem is that you don't seem to know what a pointer is or does. Think about it a while. It takes a bit of getting used to. Like s.x is not the same as s->x.
 
  • #3
The definition for struct student should be moved to before main. The instance of s is OK to have in main. You need to allocate s before attempting to allocate s.name. There are other problems such as sizeof(name), which needs to be sizeof(struct student s.name).
 
  • #4
pairofstrings said:
Please take a look at my program. Please help.

I want to allocate memory dynamically for one field in structure.
I tried to write the code but it has error. I posting a paint document for you to see. Help me please. Please tell me why is my code not working properly and what needs to be done.
Thank you.

dmaandstructures.png

You want to have
struct student s;
instead of
struct student *s;

In the second case you have an uninitialized pointer, which means it points at garbage. There's no advantage to that.
 
  • #5
ImaLooser said:
You want to have
struct student s;
instead of
struct student *s;
or allocate s first and then allocate s->name. I'm not sure if you want s to be a pointer to a structure or an instance of a structure. You could also use a typedef to avoid having to use struct with each declaration:

typedef struct
{
int rno;
char *name;
float marks;
}STUDENT;

then later in main you can use

void main()
{
STUDENT s; // declare an instance of s

or if you want a pointer

STUDENT *s = malloc(sizeof(STUDENT));
 
  • #6
This is how far I got after trying for three days. I know I can access structure members using .* or -> operators but it is not working in this case.
dmaandstructures3.png
 
Last edited:

Related to Dynamic memory allocation and structure fields

1. What is dynamic memory allocation?

Dynamic memory allocation is a programming concept that allows for the allocation and management of memory during the execution of a program. This means that memory can be allocated or freed up as needed, rather than being fixed at compile time.

2. How is dynamic memory allocation different from static memory allocation?

In static memory allocation, memory is allocated at compile time and remains fixed throughout the execution of the program. Dynamic memory allocation, on the other hand, allows for the allocation and deallocation of memory during runtime, providing flexibility in memory usage.

3. What are structure fields in dynamic memory allocation?

Structure fields are variables that are contained within a structure or object. In dynamic memory allocation, these fields can be dynamically allocated and managed, providing more flexibility in the organization and usage of memory.

4. How is dynamic memory allocation used in data structures?

Data structures, such as linked lists and arrays, often require dynamic memory allocation to function efficiently. This is because the size of the data structure may change during runtime, and dynamic memory allocation allows for the allocation and deallocation of memory as needed.

5. What are the potential drawbacks of using dynamic memory allocation?

One potential drawback of dynamic memory allocation is the risk of memory leaks, where memory that is allocated is not properly deallocated and remains in use, leading to decreased performance and potential crashes. Additionally, dynamic memory allocation can be more complex and error-prone compared to static memory allocation.

Similar threads

  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
19
Views
1K
  • Programming and Computer Science
4
Replies
118
Views
7K
  • Programming and Computer Science
Replies
3
Views
768
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
4
Views
747
  • Programming and Computer Science
Replies
9
Views
3K
Back
Top