0

I am new very interested in c programming now i am writing a c program which for dynamically memory allocation.

These are the the stages of the program:

  1. it gets n numbers from user
  2. it prints them
  3. it gets another n numbers from the user
  4. it prints both sets of numbers

my program works fine for small values of n, but not for large ones, such as 200000 numbers. i.e first I entered 100000 numbers then I entered another 100000 numbers.

It executes fine for the first set but then it gives a segmentation fault. I use gdb for debugging. It shows SIGSEGV error.

Can anybody explain what happened and give me a solution for it?

#include<stdio.h>
#include<malloc.h>
int main(void)
{

    unsigned int *p=NULL;
    unsigned int n;
    unsigned int i;
    unsigned int *a;
    unsigned int *t;
    unsigned int k=0;

    printf("Enter no.of elements...");
    scanf("%d", &n);

    p = (unsigned int*)malloc(n*sizeof(unsigned int));
    a = p;
    t = p;
    for (i=0; i<n; i++, *p++, k++) {
        scanf("%d",p);
    }

    for(i=0;i<n;i++,a++) {
        printf("Element No-%d %d Address->%d\n", i, *a, a);
    }

    a=t;

    printf("next time...how many elements do you enter");
    scanf("%d",&n);

    t=p;
    a = (unsigned int*)realloc((void *)a, n*sizeof(int));
    for (i=0; i<n; i++, *t++ ,k++) {
        scanf("%d",t);
    }

    printf("next time...printing..\n");

    for (i=0; i<k; i++, a++) {
        printf("Element No-%d %d Address->%d\n",i,*a,a);
    }

    free(p);
    free(a);


}
prasad
  • 1,277
  • 2
  • 16
  • 39

2 Answers2

3

realloc changes the size of the allocation, it doesn't grow the allocation by the size you specify.

So you need to do

a=realloc(a, original_size + extent_size);

(You don't need to cast the return value of malloc or realloc in C.)

Another note:

t=p;
...
a=t;
...
t=p;
a=realloc(...);

You can't use t after the realloc, since realloc could have changed the block's address. Put t=a; after the realloc. (And use more descriptive names, your code is very hard to follow.)

Community
  • 1
  • 1
Mat
  • 202,337
  • 40
  • 393
  • 406
  • Thanks for responding my program and you said that realloc changes the size of allocation so how can i get back the numbers which are allocated at the previous location and how can add another numbers after at the end of previous memory location ? – prasad Nov 11 '12 at 19:22
  • There's no standard/portable way to know, you need to keep track of that yourself. – Mat Nov 11 '12 at 19:22
1

You should #include <stdlib.h> and #include <stdio.h>, not <malloc.h> which is deprecated. Please also indent correctly your code

The two lines:

for(i=0;i<n;i++,*p++,k++)
   scanf("%d",p);

seem suspicious. Why the *p? I would make it

for (i=0; i<n; i++, p++)
  if (scanf(" %d", p)<1) 
    exit (EXIT_FAILURE);

Don't forget to enable all warnings and debugging info during compilation (with gcc that means gcc -Wall -g) and to use the debugger (e.g. gdb) and the memory leak detector (e.g. valgrind) to debug your program.

You might want to use calloc instead of malloc, and you definitely should test the result of malloc, calloc, or realloc (all could fail by returning NULL).

And as Mat answered, your realloc call was incorrect.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547