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 6908191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:31:53+00:00 2026-05-27T08:31:53+00:00

Can you please tell me what I did wrong? #include <stdio.h> #include <stdlib.h> void

  • 0

Can you please tell me what I did wrong?

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


void read(int *p,int n)
{
    int *q,i,j;
    q=p;
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
        {
            printf("matrix[%d][%d]=",i,j);
            scanf("%d",q);
            q=q+1;
        }
    printf("\n");
}

void alocate(int *p,int n)
{
    p=(int*)malloc(n*n*sizeof(int));
    if(p==NULL)
    {
        printf("Allocation error\n");
        exit(1);
    }
}

void realocate(int *p,int n)
{
    p=(int*)realloc(p,n*n*sizeof(int));
    if(p==NULL)
    {
        printf("Reallocation error\n");
        exit(1);
    }
}

void show(int *p,int n)
{
    int *q,i,j;
    q=p;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            printf("%d\t",*q);
            q=q+1;
        }
        printf("\n");
    }
}

void cleaner(int *p)
{
    free(p);
}
int main() {
    int *p,n;
    p=NULL;
    printf("n=");
    scanf("%d",&n);
    alocate(p,n);
    read(p,n);
    show(p,n);
    realocate(p,2);
    read(p,2);
    show(p,2);
    cleaner(p);
    return 0;
    system("pause");
}

NetBeans (MinGW):

RUN FAILED (exit value 5)

Signal received: SIGSEGV (?) with sigcode ? (?)
From process: ?
For program cppapplication_1, pid -1

Visual Studio:

Unhandled exception at 0x5c81e42e (msvcr100d.dll) in Capp.exe: 0xC0000005: Access violation writing location 0x00000000.


And if I delete p=NULL; from main function, it says:

Run-Time Check Failure #3 – The variable ‘p’ is being used without being initialized.
Unhandled exception at 0x5b4ee42e (msvcr100d.dll) in Capp.exe: 0xC0000005: Access violation writing location 0xcccccccc.

  • 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-05-27T08:31:54+00:00Added an answer on May 27, 2026 at 8:31 am

    Your alocate function correctly allocates memory, but does not return a pointer to the allocated memory. You can fix it like this

    int *alocate(int n)
    {
        int *p=(int*)malloc(n*n*sizeof(int));
        if(p==NULL)
        {
            printf("Allocation error\n");
            exit(1);
        }
    
        return p;
    }
    

    In your main function, you would use alocate like this:

    p = alocate(n);
    

    You need to make a similar change to your realocate function.

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

Sidebar

Related Questions

Can you please tell me what I did wrong? I'm getting SIGSEGV (Segmentation fault)
Did you know, the best MVC based stylesheet framework?. And, can please tell me
Can you please tell me how much is (-2) % 5 ? According to
Can anyone please tell me if Certifying Authorities (CAs) are allowed to make modifications
Can anyone please tell what is change request management tool and what is involved
Can anyone please tell me why compiling a C# application with a .cs file
Can you please tell me how can I set a global #define in my
Can anyone please tell me when should I use MessageContracts and when should I
Can anybody please tell me what is the use of %NOTFOUND operator? We can
Can someone please tell me why this doesn't work? I'm trying to show and

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.