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

  • Home
  • SEARCH
  • 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 6011741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:18:12+00:00 2026-05-23T02:18:12+00:00

I am allocating some memory in a function name myalloc() and using and freeing

  • 0

I am allocating some memory in a function name myalloc() and using and freeing it in main().
I am using double pointer to do this, here is the code which works fine,

//Example # 1

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

void myalloc( char ** ptr)
{
    *ptr = malloc(255);
    strcpy( *ptr, "Hello World");
}

int main()
{
    char *ptr = 0;
    myalloc( &ptr );
    printf("String is %s\n", ptr);
    free(ptr);

    return 0;
}

But following code does not work and gives segmentation fault.
I think this is another way to use double pointers.

//Example # 2

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

void myalloc( char ** ptr)
{
    *ptr = malloc(255);
    strcpy( *ptr, "Hello World");
}

int main()
{
    char **ptr = 0;
    myalloc( ptr );
    printf("String is %s\n", *ptr);
    free(*ptr);

    return 0;
}

Please clarify me, why it is giving me seg fault in second example.

Note: Language = C, Compiler = GCC 4.5.1, OS = Fedora Core 14

Also, i know that there are some question already been asked related to memory allocation using double pointers, but they don’t address this issue, so please don’t flag it as repetitive question.

  • 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-23T02:18:13+00:00Added an answer on May 23, 2026 at 2:18 am
    char **ptr = 0;
    *ptr = malloc(255);
    

    tries to write the pointer returned by malloc to the address(of type char*) pointed to by ptr. The address turns out to be … 0, which is not writable memory.

    ptr should point to an address you can write to. You can do one of the following:

    char *stackPtr; // Pointer on the stack, value irrelevant (gets overwritten)
    ptr = &stackPtr;
    // or
    char **ptr = alloca(sizeof(char*)); // Equivalent to above
    // or
    char **ptr = malloc(sizeof(char*)); // Allocate memory on the heap
    // note that ptr can be 0 if heap allocation fails
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am profiling a C program using Mac's Shark which shows that some of
We are using visual studio 2008-My requirement is to allocate some memory, store data
I don't truly understand some basic things in C like dynamically allocating array of
I'm having problems allocating and deallocating my memory in a recursive C++ program. So
Is it necessary to check for nullptr after allocating an object using gcnew?
What are all the other things the new operator does other than allocating memory
Does String.ToLower() return the same reference (e.g. without allocating any new memory) if all
Using C, is there a safer way to allocate memory than using malloc? For
I'm writing some quick code to try and extract data from an mp3 file
For brushing up my C, I'm writing some useful library code. When it came

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.