![]() |
| | #1 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,946
| Here's the issue: I want to read in a text file consisting of lines like this bob 3874 habit scram 9984 sweater flux 12 then so each line ends up in a struct like this: Code: struct info {
char *name;
int *num;
char *also;
} ray[3];
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is online now | |
| | #2 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| |
| tabstop is offline | |
| | #3 | ||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,368
| Quote:
Code: #include <stdlib.h>
struct info {
char *name;
int *num;
char *also;
};
int main(void) {
struct info *ray;
ray = malloc(3 * sizeof(*ray));
/* Use ray[0], ray[1] and ray[2] here. */
free(ray);
return 0;
}
Quote:
EDIT: Gah, it looks like the tutorials and FAQs here only touch on dynamic memory allocation with respect to C++. Anyway, search the Web and you will find more info. Also, you might note that my example does not include checking the return value of malloc() for NULL, but checking whether the allocation failed is good practice.
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way Last edited by laserlight; 08-27-2008 at 10:43 AM. | ||
| laserlight is online now | |
| | #4 |
| and the hat of sweating Join Date: Aug 2007 Location: Toronto, ON
Posts: 3,120
| Switch to C++ and use a vector which is like an array and it can grow. |
| cpjust is offline | |
| | #5 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,946
| malloc and realloc will do! I just didn't see how to use them with an array...thanks. phew! I thot i was crazy.
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS Last edited by MK27; 08-27-2008 at 10:46 AM. Reason: phew! I thot I was crazy. |
| MK27 is online now | |
| | #6 | |
| Registered User Join Date: Aug 2008
Posts: 67
| Quote:
http://cslibrary.stanford.edu/103/LinkedListBasics.pdf | |
| kpreston is offline | |
| | #7 |
| Banned Join Date: Aug 2001 Location: Visalia, CA, USA
Posts: 3,699
| The STL vector idea isn't a horrible one, but I wouldn't tell someone who says "How much is gas, my Tahoe is a gas guzzler" "Oh the Toyota dealership is down the road to sell you a more fuel efficient vehicle." So in the spirit of answering the question without intense sarcasm: The linked list thing may be a better solution since it can be cheaper than resizing. On the other hand, for what you are doing, since you can just resize by a large factor, you may find it no more expensive to just use malloc() and realloc(). |
| master5001 is offline | |
| | #8 |
| and the Hat of Ass Join Date: Dec 2007
Posts: 731
| I gave a link to a nice reference to dynamic memory allocation in the other thread. |
| rags_to_riches is offline | |
| | #9 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,946
| hmmm...what's "fuel efficiency" about here: the awkwardness of having to use realloc, or the expense of learning C++ when I just rolled C off the lot last month... I gotta thank kpreston, actually, as I have saved his refs for later. But right now a simple malloc suffices (the array doesn't really need to grow, but its initial size is a variable, and the struct must be global). It didn't occur to me to use sizeof that way; up to now the only (error checked) malloc calls I've made have been simple character arrays, and multipying by 1 is easy. I think the reason I started to write this was that I thot master5001 might have some deeper performance issues in mind. Now I'm not so sure.
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is online now | |
| | #10 | ||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,368
| Quote:
Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | ||
| laserlight is online now | |
| | #11 |
| and the Hat of Ass Join Date: Dec 2007
Posts: 731
| Realloc example: Code: #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINE_BUF_SZ 1024
static const unsigned int RAY_CHUNK_SIZE = 10;
typedef struct _info
{
char *name;
int num;
char *also;
} info;
void print_data(const info **data, const unsigned int count)
{
unsigned int i = 0;
for ( ; i < count; ++i)
{
printf("Entry %u: %s %d %s\n", i,
data[i]->name, data[i]->num, data[i]->also);
}
}
void free_data(info **data, const unsigned int count)
{
unsigned int i = 0;
for ( ; i < count; ++i)
{
free(data[i]->name);
free(data[i]->also);
free(data[i]);
}
}
int main()
{
char line[LINE_BUF_SZ];
char temp_name_buf[LINE_BUF_SZ], temp_also_buf[LINE_BUF_SZ];
FILE *fp = fopen("dyndata.txt", "r");
if (!fp)
return -1;
unsigned int ray_count = RAY_CHUNK_SIZE;
info **ray = calloc(ray_count, sizeof(*ray));
if (!ray)
return -1;
unsigned int line_count = 0;
while(NULL != (fgets(line, sizeof(line), fp)))
{
if (line_count + 1 == ray_count)
{
// Need realloc
info ** temp = realloc(ray, (ray_count += RAY_CHUNK_SIZE) * sizeof(*ray));
if (!temp)
{
free_data(ray, line_count);
free(ray);
return -1;
}
ray = temp;
}
ray[line_count] = calloc(1, sizeof(info));
if (!ray[line_count])
return -1;
if (3 != sscanf(line,
"%s %d %s",
temp_name_buf,
&ray[line_count]->num,
temp_also_buf))
{
return -1;
}
ray[line_count]->name = strdup(temp_name_buf);
ray[line_count]->also = strdup(temp_also_buf);
++line_count;
}
print_data((const info **)ray, line_count);
free_data(ray, line_count);
free(ray);
fclose(fp);
return 0;
} Last edited by rags_to_riches; 08-27-2008 at 11:51 PM. Reason: Consistency |
| rags_to_riches is offline | |
![]() |
| Tags |
| absurdity, arrays |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Dynamic Array Resizing | dld333 | C++ Programming | 13 | 11-04-2005 12:13 AM |
| need help with dynamic array syntax | soldyne | C Programming | 3 | 10-11-2005 01:59 PM |
| Class Template Trouble | pliang | C++ Programming | 4 | 04-21-2005 04:15 AM |
| 2D dynamic array problem | scsullivan | C Programming | 3 | 12-30-2002 10:02 PM |
| Dynamic array allocation and reallocation | purple | C Programming | 13 | 08-01-2002 11:48 AM |