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

The Archive Base Latest Questions

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

I´m getting a segmentation fault with a pointer when it´s declared as local. The

  • 0

I´m getting a segmentation fault with a pointer when it´s declared as local. The program is too long to copy it here, but it´s something like this:

void f(){
    int* p;
    int n = 0;
    for (...) {
        n++;
        p = realloc(p, n * sizeof(int));
        if (p == NULL) error(); 
    }
    //Code using the pointer
    free(p);
}

int main() {
    f();
    puts("Finish");
}

the puts(“Finish”); is executed, but i get a segmentation fault after this.

If I declare p as global, before the funcion, it works perfectly without the fault and this problem gets solved but I dont know why is this happening.

Running the debugger I can´t see where the problem is, all values seem ok. This is the trace after the fault:

Program received signal SIGSEGV, Segmentation fault.
0x000000361206dbd1 in _IO_flush_all_lockp () from /lib64/libc.so.6
(gdb) bt
#0  0x000000361206dbd1 in _IO_flush_all_lockp () from /lib64/libc.so.6
#1  0x000000361206e725 in _IO_cleanup () from /lib64/libc.so.6
#2  0x00000036120334b2 in exit () from /lib64/libc.so.6
#3  0x000000361201d99b in __libc_start_main () from /lib64/libc.so.6
#4  0x0000000000400ce9 in _start ()

Just in case it can be an issue, it´s an multiprocess application (fork, execv, etc…)

Thanks in advance for your answers

  • 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-27T16:33:31+00:00Added an answer on May 27, 2026 at 4:33 pm

    Looks like you don’t bother to initialize p — with a global var it will be implicitly initialized to 0, but a local var is not. So you end up calling realloc/free with a random garbage pointer that corrupts the heap and causes a later crash…

    Initialize p and that should fix it.

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

Sidebar

Related Questions

I am getting segmentation fault in this code but i cant figure out why.
Trying to copy a char *str to char c[] but getting segmentation fault or
When I try to execute this program I am getting segmentation fault. What could
I am getting segmentation fault in getaddrinfo(). This is the stack trace. Program received
Possible Duplicate: getting segmentation fault in a small c program Here's my code: char
I'm trying to delete whole linked list but getting segmentation fault and not able
I've a 'C' program which has encountered a strange problem.. I'm getting segmentation fault
Im working on this bit of code and I keep getting a segmentation fault.
Whats wrong with this?I am getting segmentation fault during runtime. int size; scanf(%d,&size); int
I'm getting a segmentation fault from SDL_BlitSurface in C++. here's the code mainScreen->draw( 0,

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.