-
malloc string array
Hi. Im quite new to c programming. Im trying to figure out how to use malloc. I saw a question similar to this in the forum but it was a good bit more complicated and I didnt really understand it.
I want to create a 1D array of ints from a file. I had a stab at it but the while loop isnt correct, am going about it the right way?
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fid;
int * iarray;
int n=0;
fid = fopen("unsorted.txt", "r");
if (fid == NULL)
{
printf("Cannot open file\n");
exit(EXIT_FAILURE);
}
iarray = (int *) malloc ( n * sizeof(int) );
iarray[n] = fgets(fid);
while( ( iarray[n] = fgets(fid) ) != EOF )
/*or ? while(fscanf(fid, "%s", &iarray[n]) != EOF )*/
{
fscanf(fid, "%d", &iarray[n]);
n++;
}
fclose(fid);
printf("%d", iarray[n]);
return(0);
}
-
You are using it correctly however for every element that you add you must malloc() storage which is being invoked only once.
n != 0 otherwise the call to malloc() will return a null pointer since you are essentially requesting no storage at all.
Changes to be made to the source are in red.
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fid;
int * iarray;
int n=0; /* should not be initialized to zero */
fid = fopen("unsorted.txt", "r");
if (fid == NULL)
{
printf("Cannot open file\n");
exit(EXIT_FAILURE);
}
iarray = (int *) malloc ( n * sizeof(int) ); /* goes inside while loop because you need to store each new array element added */
iarray[n] = fgets(fid);
while( ( iarray[n] = fgets(fid) ) != EOF )
/*or ? while(fscanf(fid, "%s", &iarray[n]) != EOF )*/
{
fscanf(fid, "%d", &iarray[n]);
n++;
}
fclose(fid);
printf("%d", iarray[n]);
return(0);
}
-
Thanks for your help. The problem now is I am making a string array and comparing this to an int. I messed around a lot and came up with the following line:
while( (a = atoi(fgets(iarray, 2, fid) )) != EOF )
It compiled with no errors but when i tried to run the program I got error while dumping....Could you help me with this while statement please?
-
What string array? You don't have a string array in your code anywhere. You have iarray, which is an array of integers that you are filling in an ... interesting way. You don't have a char array either.
Maybe you should start by figuring out what the program is supposed to do. If you're supposed to read integers from a file, you don't use fgets (unless you're ultra paranoid).
-
Ok.
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fid;
int * iarray;
int n=0;
fid = fopen("unsorted.txt", "r");
if (fid == NULL)
{
printf("Cannot open file\n");
exit(EXIT_FAILURE);
}
while( (fscanf(fid, "%d", &iarray[n]) != EOF ) )
{
iarray = (int *) malloc ( n * sizeof(int) );
/*fscanf(fid, "%d", &iarray[n]);*/
n++;
}
fclose(fid);
printf("%d\n", iarray[0]); prints the last number in the file
printf("%d\n", iarray[1]); prints 0 ???
printf("%d\n", iarray[2]); prints 0 ???
printf("%d\n", iarray[32]); prints 0 ???
printf("%d\n",n); 33 - there are 33 ints in the file
return(0);
}
What I take out of this is that the malloc part isnt working. My array will only take in 1 value, and keeps replacing this value with a new value until it exits the loop. Is this because you cannot update the array size after you have declared it once?
-
Each malloc gives you a new piece of memory, yes. If you want to resize the current piece of memory, you should use realloc.
-
I'm afraid itCbitC was a little off.
You cannot use a pointer before you allocate some memory!
The malloc should be before the loop and stored inside the pointer for the total number of elements you need. The formula is generally: n * sizeof(type), where n is the number of elements and type is the type of array.
-
I believe you will want to use realloc in the loop.
Note that at the moment you have a buffer overrun and a memory leak.
-
Now my printfs dont print anything
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fid;
int * iarray;
int n=0;
fid = fopen("unsorted.txt", "r");
if (fid == NULL)
{
printf("Cannot open file\n");
exit(EXIT_FAILURE);
}
iarray = (int *) malloc ( (n+1) * sizeof(int) );
while( (fscanf(fid, "%d", &iarray[n]) != EOF ) )
{
iarray = realloc(iarray, (n+1)*sizeof (int));
n++;
}
fclose(fid);
printf("%d\n", iarray[0]);
printf("%d\n", iarray[1]);
printf("%d\n", iarray[2]);
printf("%d\n", iarray[32]);
printf("%d\n",n);
free(iarray);
return(0);
}
btw iMalc on your website I cant get your "best bubsort" to show source code
-
Once you free(iarray), it's gone (or could be gone, or may be gone -- we don't know, is the point). So you would have to printf before you free anything.
-
Why would you free an array then try to print it's members?
-
-
Code:
iarray = realloc(iarray, (n+1)*sizeof int);
-
If i knew what was wrong with that line would it be there?
-
The point is that is what the line should be -- you need n+1 ints, not n+1 bytes.