Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8825205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:49:23+00:00 2026-06-14T06:49:23+00:00

I am new very interested in c programming now i am writing a c

  • 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);


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:49:24+00:00Added an answer on June 14, 2026 at 6:49 am

    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.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please be gentle, I'm very new to programming and only own one book which
I'm very new to GPGPU and Programming. I'm interested to know if statistical hypothesis
I am new to RAZOR and am very interested in this new tech. Now
I am new to debugging and I'm very interested in knowing how to setup
Very new to JQuery and MVC and webdevelopment over all. I'm now trying to
I'm very interested in learning about the new feature in HTML5 called web sockets.
Funcional Programming is very new to me and can't seem to understand how to
I am very new to Node and still in the beginning stages of understanding
I'm very interested in module loaders for node.js and also the new harmony modules
Very new to python and can't understand why this isn't working. I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.