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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:18:28+00:00 2026-05-26T04:18:28+00:00

char *t = malloc(2); t = as; t = realloc(t,sizeof(char)*6); I am getting error

  • 0
char *t = malloc(2);
t = "as";

t = realloc(t,sizeof(char)*6);

I am getting error “invalid pointer: 0x080488d4 *“..

I am getting strange errors in using memory allocation functions. Is there any good tuts/guides which could explain me memory allocation functions.
I am using linux..

Please help..

  • 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-26T04:18:29+00:00Added an answer on May 26, 2026 at 4:18 am

    This is your problem:

    char *t = malloc(2);
    t = "as";
    

    You probably thought this would copy the two-character string "as" into the buffer you just allocated. What it actually does is throw away (leak) the buffer, and change the pointer to instead point to the string constant "as", which is stored in read-only memory next to the machine code, not on the malloc heap. Because it’s not on the heap, realloc looks at the pointer and says “no can do, that’s not one of mine”. (The computer is being nice to you by giving you this error; when you give realloc a pointer that wasn’t returned by malloc or realloc, the computer is allowed to make demons fly out of your nose if it wants.)

    This is how to do what you meant to do:

    char *t = malloc(3);
    strcpy(t, "as");
    

    Note that you need space for three characters, not two, because of the implicit NUL terminator.

    By the way, you never need to multiply anything by sizeof(char); it is 1 by definition.

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

Sidebar

Related Questions

char char* c[30]; c = (char*) malloc(30*sizeof(char) ); How does this give an incompatible
I usually use pointers in the following manner char *ptr = malloc( sizeof(char) *
int length = strlen(src); char *structSpace = malloc(sizeof(String) + length + 1); String *string
int main() { char *s1, *sTemp; s1 = (char*)malloc(sizeof(char)*7); *(s1 + 0) = 'a';
#include<stdio.h> #include<string.h> char *y; y=(char *)malloc(40); // gives an error here int main() {
#inlcude <stdio.h> #inlcude <stdlib.h> #inlcude <string.h> int main() { char *buff = (char*)malloc(sizeof(char) *
char* foo = (char*) malloc(sizeof(char)*50); foo = testing; In C, i can see the
jobjectArray ret; int i; char *data = malloc(3 * sizeof(char *)); if(data != NULL)
I want to know how malloc and free work. int main() { unsigned char
Imagine I am doing something like this: void *p = malloc (1000); *((char*)p) =

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.