1

I am trying to create a dynamic array of size 32, and then read intergers from some file, and store them in the array. When the array gets filled up, then double its size (create another of twice the size, copy elements to it from old array and free old array) till the input file is exhausted.

Here is the code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

#define limit 32

int main (int argc, char *argv[]) {

    FILE *file = fopen ("myFile1.txt", "r");
    int i = 0, num;
    int *B, *C;

    B = malloc (sizeof (int) * limit);

    while (fscanf (file, "%d", &num) > 0) {
        if (i < limit) {
            B[i] = num;
            i++;
        }
        else {
            C = malloc (sizeof (int) * 2 * limit);
            memcpy (C, B, limit * sizeof (int));
            free (B);

            B = C;
            B[i] = num;
            i++;
            limit = limt * 2;
            i++;
        }
    }
    return 0;
}

I am getting an error like: "lvalue required as left operand of assignment" and 2nd: "segmentation fault".

Since, I am trying to explore new possibilities related to dynamic arrays, to increase my knowledge; help me out by modifying the code.

Any help will be highly appreciated.

8
  • please fix the formatting and it seems your limit variable is missing. Commented Jan 21, 2016 at 18:04
  • @Bruce He used the preprocessor command #define to "define" the limit Commented Jan 21, 2016 at 18:05
  • 1
    You can resize an array and copy its data with realloc(): limit = limit * 2; B = realloc(B, limit*sizeof(int));. No need to create a temporary array called C. Commented Jan 21, 2016 at 18:05
  • 1
    Growing an array to twice its size can be a bit greedy - how about increasing it by a fixed amount - say 32 or 1000 whatever is appropriate. Commented Jan 21, 2016 at 18:07
  • 2
    You need to make limit a variable, not a macro. Commented Jan 21, 2016 at 18:08

1 Answer 1

3

You can actually allocate more memory for your array using realloc() :

void *realloc(void *ptr, size_t size)

Instead of doing that :

  {
    C=malloc(sizeof(int)*2*limit);
    memcpy(C,B,limit*sizeof(int));
    free(B);
    B=C;
    B[i]=num;
            i++;
            limit=limt*2;
    i++;
       }

You can simply do :

B = realloc(B,new_size_in_bytes);

Talking about your code:

The preprocessor command #define will replace every occurrence of the word "limit" with the value associated to it (32, in this case) before the compilation. So you can't really change the value of a macro during run-time. If you wish to do that my advice would be not to define limit but use a variable instead. About the segfault I'm not having one. Be sure to have the file called "myFile1.txt" in the same folder where the .c file is, also check if you misspelled it.

Sign up to request clarification or add additional context in comments.

6 Comments

@Trisha I have very few experiences with C++, but I can assure you that realloc is the right way to resize an array in C.
Okk it is also a great option but the method that i was used is incorrect. need suggestion.
Its also great.but i just want to implement that also..thats why asking.
You don't seem very strong in the reading department (you did not notice the typo pointed out), and you still don't seem to have done your own research on realloc.
@WeatherVane I was editing the post, I'm sorry. BTW I'm still learning malloc() and realloc(), but I think that I get the point with them. What problem did you noticed? As a student any help is appreciated.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.