Actually wait... if it's sorted, you couldn't, because it's a list. But with a sorted array you could.
Actually wait... if it's sorted, you couldn't, because it's a list. But with a sorted array you could.
How do you say I can do it with an array? cause I can't have a fixed array if I want to support multiple connections or can I?
Why do you assume you can't do this with a fixed array? You could also dynamically resize an array if necessary. I recommend against the linked lists for the reasons stated in my previous post. You aren't doing that many connections anyway. You aren't writing a server comparable to google's.
Right. I'd just stick with an array and a loop to find the elements. Keep it simple.
Can you give me an example of how can i resize the array each time I need to do it? Well.. I'm trying to do this program to communicate some offices and save all the files into a server.. I might need a big array..
You can use realloc(). With any sort of web application I typically allocate large pools anyway. I prefer to manage my own memory. That not withstanding, why not just make a 1MB buffer or something for your arrays? I doubt you will need more than 1MB (1048576 bytes).
[edit]I don't know what it is with people new to programming. Memory is a luxury that we have to work with in this day and age. Nowadays if you need a 15MB buffer, its not so outlandish. Don't be afraid of allocating chunks of memory that are 10,000 * sizeof YourClass. In the grand scheme of things that usually ends up being like 40k of memory...[/edit]
Mmm.. I've been reading about realloc(), but how can i change the size of an array using bytes size? and to assign a value to the new element of the array? can you give me a little example? I'll try using realloc() to learn as much as I can about c =)
Last edited by lautarox; 09-17-2008 at 08:23 PM.
search the board.
Mmm.. looking in the forums I saw an array of strucs, and I liked it more than a simple array, I want to have more info about each file transfer..
Is this ok?
Code:struct _fileinfo { FILE *file; int parts; int totalparts; }; int totalconnections; void memoryfunct() { struct _fileinfo *arr; realloc(arr, (totalconnections * sizeof(*arr))); arr[i].file="fopen(filepath, "ab"); .. .. .. }
Last edited by lautarox; 09-18-2008 at 11:24 AM.
Indent your code.
http://www.eskimo.com/~scs/cclass/notes/sx11c.html
Is it ok like this?
Well.. I'm still not understanding how can I create or resize an array using realloc(), I mean, I create a pointer, or I give it a bigger size taking in consideration the size of my struct.. how the program knows that I'm creating an array?
Code:struct _fileinfo { FILE *file; int parts; int totalparts; }; int totalconnections; void memoryfunct() { struct _fileinfo *arr; realloc(arr, (totalconnections * sizeof(*arr))); arr[i].file="fopen(filepath, "ab"); .. .. }
Because you used realloc. What else is it going to be? (Answer: a single object, but if you try hard enough, you can think of that as a 1-element "array". I'm putting "array" in quotes because you are not creating a thing of array type, but you are creating a thing that you can use index[notation] with.)
>Is it ok like this?
no. read the link.
I need a little help understanding the code..
And about this..Code:#define MAXLINE 100 char line[MAXLINE]; int *ip; int nalloc, nitems; nalloc = 100; -> //100.. ip = malloc(nalloc * sizeof(int));/* initial allocation */ -> // Why is is multiplying the size with 100? is it the same as "someinteger[100]" ? if(ip == NULL) { printf("out of memory\n"); exit(1); }
Code:nitems = 0; while(getline(line, MAXLINE) != EOF) { if(nitems >= nalloc) { /* increase allocation */ int *newp; nalloc += 100; newp = realloc(ip, nalloc * sizeof(int)); // -> Is it adding 200 more integers to the arr? if(newp == NULL) { printf("out of memory\n"); exit(1); } ip = newp; } ip[nitems++] = atoi(line); -> // what does ip[nitems++] is supposed to do? }
>ip = malloc(nalloc * sizeof(int));/* initial allocation */ -> // Why is is multiplying the size with 100? is it the same as "someinteger[100]" ?
An array's elements are all next to each other in order, so yes.
>nalloc += 100;
>newp = realloc(ip, nalloc * sizeof(int)); // -> Is it adding 200 more integers to the arr?
No, it adds 100, for a total of 200.
>ip[nitems++] = atoi(line); -> // what does ip[nitems++] is supposed to do?
ip[nitems++] = atoi(line);
is the same as
ip[nitems] = atoi(line); nitems = nitems + 1;
Last edited by robwhit; 09-19-2008 at 07:35 AM.