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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:44:51+00:00 2026-05-23T06:44:51+00:00

#include <stdio.h> #include <stdlib.h> int main(int argc, char * argv[]) { char *p[2]; char

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


int main(int argc, char * argv[])
{
    char *p[2];
    char **pp = calloc(2, 4);

    p[0] = "ab";
    p[1] = "cd";
//  p[2] = "ef";

    pp[0] = "ab";
    pp[1] = "cd";
    pp[2] = "ef";

    printf("pp: %s, %s, %s\n", pp[0], pp[1], pp[2]);
    printf("size_p: %d\nsize_pp: %d\n", sizeof p, sizeof pp);
}

if ‘p[2]’ is defined and assigned a value – the resulting behavior is a segfault. if ‘pp[2]’ is assigned – the output is the following: “ab, cd, ef”. ‘sizeof’ returns 8 (2×4 bytes per pointer) for ‘p’ and only 4 bytes for ‘pp’. why am i being able to assign ‘pp[2]’, even though it should only be in possession of 8 bytes of allocated memory (that should be able to store only 2 pointer addresses)? also, how does ‘sizeof’ determine the actual memory size in both of the cases?

  • 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-23T06:44:52+00:00Added an answer on May 23, 2026 at 6:44 am

    With char *p[2]; you allocate 2 char* on the stack. When accessing p[2], you have a buffer overflow and might access any other fings belonging to the stack frame of the current method (some compilers check this in debug mode).

    With calloc, you allocate memory in the heap. Accessing pp[2] is (probabely) free memory, you no segfault here. But this memory could also be used by other objects, so this is absolutely not ok!

    For size calculation: sizeof(char**) is 4, as is for every 32-bit pointer. sizeof(char*[2]) is 8, because it is 2×4 bytes.

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

Sidebar

Related Questions

#include<stdio.h> #include<unistd.h> #include<stdlib.h> int main(int argc,char **argv) { int fd[2]; pid_t childpid; pipe(fd); childpid=fork();
I have the following piece of code: #include <stdio.h> #include <stdlib.h> int main(int argc,
#include <stdlib.h> #include <stdio.h> struct foo{ int id; char *bar; char *baz[6]; }; int
#include <stdio.h> #include <stdlib.h> #include <string.h> char *readLine(FILE *inFile) //Simply reads line in a
I am learning C and I have the following code: #include <stdio.h> #include <stdlib.h>
i've encoutered some issues again.. the script : #include <stdio.h> #include <stdlib.h> #include <gio/gio.h>
I am implementing parallel dot product in open MP I have this code: #include
I have been trying to base64 decode the input char * using the following
I wrote a basic Hippity Hop program in C, Python, and OCaml. Granted, this
I have been looking at this for hours and can't figure this out. If

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.