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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:54:53+00:00 2026-06-06T00:54:53+00:00

Is there a leak in this C code? Although the answer is coming correct,

  • 0

Is there a leak in this C code?
Although the answer is coming correct, but just wanted to know if it is Ok to write like this:

// _mm_adds_epi16 : Adds the 8 signed 16-bit integers in a to the 8 signed 
//16-bit integers in b and saturates
__m128i t7=_mm_adds_epi16(  t5 ,t6  );
unsigned short *p= (unsigned short *)malloc(8);
p=(unsigned short *)&t7;
for(int i=0;i<8;i++)
{
    printf("%d\n", p[i]);
}

Updated

So now I am updating it as follows:

// _mm_adds_epi16 : Adds the 8 signed 16-bit integers in a to the 8 signed 
//16-bit integers in b and saturates
__m128i t7=_mm_adds_epi16(  t5 ,t6  );
unsigned short *p= (unsigned short *)malloc(8);
p=(unsigned short *)&t7;
for(int i=0;i<8;i++)
{
    printf("%d\n", p[i]);
}

 free(p);

Do I still have leak?

What is the correct way to print t7

  • 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-06-06T00:54:55+00:00Added an answer on June 6, 2026 at 12:54 am

    Every malloc in C needs to have some corresponding free which is reachable before termination. Any exception to this constitutes a memory leak.

    You store the return of malloc in p and lose the pointer by overwriting it. As such there is no chance for the pointer to be freed. In your particular case, the malloc itself is redundant since you are not using the return in any way.


    In response to your question of whether the new code has a memory leak, yes it does. malloc allocates memory and returns a pointer to the allocated memory. You are losing the pointer by overwriting its value then never using the allocated memory. Even worse you are now calling free on what appears to be an automatic variable which is undefined behaviour.

    If the printing is working correctly, you can just do this:

    __m128i t7 = _mm_adds_epi16(t5, t6);
    unsigned short *p = (unsigned short *)&t7;
    for(int i=0;i<8;i++) {
        printf("%d\n", p[i]);
    }
    

    The malloc is redundant because you are not even using the memory it allocates.

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

Sidebar

Related Questions

Instruments tells me there's a mem leak in this code, but I can't seem
It seems like there is memory leak in this piece of code.I am using
Is there a leak in this code? // Move the group Group *movedGroup =
This might not appear like a programming question, but inherently deals with code. I
I know this is a long-shot, but since I need this code to work,
There is a memory leak in a legacy application, so I don't know the
This is not my code. I just arrived at this site and am doing
Could someone please advice why i have a memory leak in this code? I
Does this code create a memory leak? WebClient client = new WebClient(); client.DownloadDataCompleted +=
Will this kind of code cause memory leak in Android? class MyActivity extends Activity

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.