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

The Archive Base Latest Questions

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

I have a question regarding this code. I write this code in my framework,

  • 0

I have a question regarding this code. I write this code in my framework, and it caused the framework crashed. But when I rewrite this code below in a single file, but it works just fine. I was just wondering, is the code below is correct for memory allocation and freeing it? (especially for the part of msg->context_var.type = f;)
Thank you

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


typedef struct
{
  int value;
  int price;
  int old;
} type_t;


typedef struct {
  type_t            *type;  
} context_t;


typedef struct {
  context_t context_var; 
} send_request;



void send_Message(send_request *msg)
{   

type_t *f = 0;
f = malloc(sizeof(f));
msg->context_var.type = f;
msg->context_var.type->price = 1;
msg->context_var.type->value = 100;
msg->context_var.type->old =120;


printf("value of %d/n", msg->context_var.type->price);
free(f);
}

int main()
{   

    send_request *msg = 0;
    msg = (send_request *) malloc(sizeof(send_request));

    send_Message(msg);
    free(msg);
    return 0;
}
  • 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-23T17:18:48+00:00Added an answer on May 23, 2026 at 5:18 pm

    It’s wrong.

    f = malloc(sizeof(f)); /* Wrong */
    f = malloc(sizeof(*f)); /* Better ? */
    

    sizeof(f) will give you the size of a pointer on your machine; sizeof(*f) will give you the size of the object pointed to.

    EDIT As requested by @Perception

    When you allocate less than you need you’re eliciting Undefined Behavior. Anything can happen (even the desired behavior) and it all depends on the platform, the environment (the moon phase, etc).

    msg->context_var.type->value = 100; /* Writes beyond what's allocated. */
    

    So, depending on the memory layout of the “framework” this might simply overwrite some memory and “work”, or it could crash. Frankly I prefer when it crashes straight away.

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

Sidebar

Related Questions

Again I have a question regarding this plugin: http://t.wits.sg/2008/06/20/jquery-progress-bar-11/ What I want to achieve
I have this very straight forward question regarding Thread and Timer classes in Java
I have seen different questions regarding this, but I still find this topic to
I hope this is allowed but I have a number of questions regarding Facebook
I have a question regarding JavaScript and methods by which its code can be
I was reading this question (which you do not have to read because I
I have question regarding the SQLAlchemy. How can I add into my mapped class
I have question regarding the use of function parameters. In the past I have
I have a question regarding the two additional columns (timeCreated, timeLastUpdated) for each record
i have a question regarding some complex data-binding. I want to be able to

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.