473,495 Members | 2,058 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Dynamic Array of Structs Problem

5 New Member
Greetings all, I had another post on here about a Dynamic array of ints problem, and with your help, I managed to solve that problem. Now, I've another issue. I need to dyanically allocate an array of structs.

The struct is defined as:

Expand|Select|Wrap|Line Numbers
  1. struct song{
  2.  
  3.   char name[50];
  4.   char title[50];
  5.  
  6. };
So, for work, they want me to sort through the songs, but due to memory constraints on the tiny machine they want it to run on, I have to use arrays instead of Linked Lists. :( I tried this method, the way it worked with ints:

Expand|Select|Wrap|Line Numbers
  1. int * list;
  2.  
  3. list = (int *)malloc(sizeof(int) * length);
And that would actually give me an array of ints. But if I try that with the song struct, it just gives me one.

Thanks in Advance.
Jul 19 '07 #1
5 2689
ravenspoint
111 New Member
Please post code that fails, i.e. the attempt to allocate an array of structs.
Jul 19 '07 #2
RedSon
5,000 Recognized Expert Expert
Greetings all, I had another post on here about a Dynamic array of ints problem, and with your help, I managed to solve that problem. Now, I've another issue. I need to dyanically allocate an array of structs.

The struct is defined as:

Expand|Select|Wrap|Line Numbers
  1. struct song{
  2.  
  3.   char name[50];
  4.   char title[50];
  5.  
  6. };
So, for work, they want me to sort through the songs, but due to memory constraints on the tiny machine they want it to run on, I have to use arrays instead of Linked Lists. :( I tried this method, the way it worked with ints:

Expand|Select|Wrap|Line Numbers
  1. int * list;
  2.  
  3. list = (int *)malloc(sizeof(int) * length);
And that would actually give me an array of ints. But if I try that with the song struct, it just gives me one.

Thanks in Advance.
First, using a linked list is more memory efficient then using an array. Every time you need to add to the array you will have to create an array that is one element larger and copy your existing array into the new array then delete your previous array. When this occurs you will then have a array of length N and an array of length N+1 which means that you are using virtually double the memory.

If you use a linked list, anytime you add something all you need to do is create the element then iterate to the last element in the list and reset the pointer to your new element.

The problem you may be experiencing is that the sizeof call may not work for your struct. I am not too clear on how it works exactly since I have never used it in this way. Consider trying to create your list using the sizeof (char[50]) to see if that will allocate properly, then moving on from there.
Jul 19 '07 #3
ravenspoint
111 New Member
This works:

Expand|Select|Wrap|Line Numbers
  1.  
  2.       struct song{
  3.         char name[50];
  4.         char title[50];
  5.  
  6.       };
  7.  
  8.      int length = 10;
  9.      song * list = (song *)malloc(sizeof(song) * length);
  10.  
  11.      char buf[20];
  12.      for( int k=0; k < 10; k++ ) {
  13.          sprintf(buf,"song #%d", k );
  14.          strcpy( list[ k ].name, buf );
  15.      }
  16.  
  17.      for( int k=0; k < 10; k++ ) {
  18.         printf( "%s\n",     list[ k ].name );
  19.      }
  20.  
Jul 19 '07 #4
mac11
256 Contributor
no need for me to post
Jul 19 '07 #5
viperdriver87
5 New Member
Excellent, this worked! Thanks to ravenspoint, RedSon, and mac111!
Jul 19 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

11
3357
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...
10
4086
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr;...
1
1761
by: mrhicks | last post by:
Hello all, I need some advice/help on a particular problem I am having. I have a basic struct called "indv_rpt_rply" that holds information for a particular device in our system which I will...
5
3108
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
2
2452
by: xinsir | last post by:
dynamic array as a byref parameter by used in function and have a Marshal error ,what is the matter?thanks source like as this . ------------------------------------------------------ Declare...
4
5049
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
11
2602
by: Cliff Martin | last post by:
Hi, I am reading a fairly large file a line at a time, doing some processing, and filtering out bits of the line. I am storing the interesting information in a struct and then printing it out....
9
18514
by: johndale | last post by:
hi, I am pretty new to C#, but have some experience with other lang.(delphi,php,asp...). I wanted to create dynamic array of structs type, but it wont work. So I google it, and found that C#.NET...
1
2738
by: superdad | last post by:
Hello; I have 2 questions: 1- I know you can allocate memory to an array dinamically in C and C++. But, can this be done if the array is part of a structure? 2- Can the size of a dynamic array...
0
7120
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6991
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7196
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6878
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4583
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
649
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
286
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.