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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:05:17+00:00 2026-05-28T14:05:17+00:00

First off I am pretty new to C, so I probably just have a

  • 0

First off I am pretty new to C, so I probably just have a fundamental misunderstanding here. Given code such as this:

int main()
{
    char ack[100];
    char *bar;
    bar = malloc(100);
    strncpy(ack, "testing", 7);    
    bar = "testing";

    return 0;
}

ack when examined by gdb looks like this:

(gdb) p ack
$1 = "testing\000\360WV\000\000\000\000\000\277\000\000\000\000\000\000      
'\000'\000\220\005@",<repeats 13 times>, "\003\004@", '\000' <repeats 13 times>    
"\325,\005@\000\000\000\000\000H\214\246\367\377\177\000\000\220\005@",
'\000' <repeats 13 times>,     "P\004@\000\000\000\000\000\360\342\377\377"` 

This makes sense to me given how I initialized ack. What I don’t quite get is why bar looks like this:

(gdb) p bar
$2 = 0x40066c "testing"

I allocated the same amount of storage (as far as I know) as I did when I requested space for ack but malloc doesn’t have the extra junk. As I understand it, malloc doesn’t do any sort of data initialization or anything, so I’m a bit confused. The reason that this came up is an issue I was having with strstr. Basically, when I would read in data from a file (fgets), to a char array with a specific size strstr() would fail (which I was assuming was because of the extra junk). Working with a pointer and malloc’d memory worked just fine. Anyway, I have a few specific questions.

  1. Is the behavior of this malloc’d variable expected? Is there some optimization going on here (I compiled with gcc, but didn’t do any optimizations) or is gdb not showing me everything? Should there be “junk” associated with that variable?

  2. Am I even using malloc correctly? Should I be initializing all of the memory I requested? If so, how?

Thank you!

EDIT

Thanks to everyone who responded! I’ve learned quite a few things from you all and it’s much appreciated. I now see the problem with the code I posted above, and the original issue that I was having with fgets() and strstr().

  • 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-28T14:05:18+00:00Added an answer on May 28, 2026 at 2:05 pm
    bar = "testing";
    

    re-assigns the pointer bar to point to a static buffer holding the string "testing", i.e. it no longer points to your malloc‘d array. This is a memory leak.

    To get a string into the malloc‘d buffer, use strcpy, strncpy or memcpy, like you did with ack.

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

Sidebar

Related Questions

First off, I am pretty new to java and I am just staring to
First off I use this code to make the navigation bar always stay fixed;
First off, this may be the wrong Forum for this question, as it's pretty
I'm quite new to nHibernate, so this'll probably be a pretty stupid question. But
First off, I'm pretty new so I hope I hadn't missed anything too trivial.
First off I am brand new to C and have been thrown into a
Okay so first off, Im pretty new to programming, Ive only read a bit
First off, please let me say I'm pretty new to CSS. Still lots to
First off, let me preface this question by stating that I'm really a pretty
first off I'm a noob to PHP but here is my problem. I am

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.