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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:37:11+00:00 2026-05-31T18:37:11+00:00

I am fairly new to writing C code and I am sure I have

  • 0

I am fairly new to writing C code and I am sure I have something wrong at the very basic level. I am running a small code to get the attributes of a file and this function below returns those attributes.
char *path would contain something like “/home/etc/bin”

/* copy the attributes into a character pointer */
unsigned char * copyAttributes (char *path)
{
    struct stat buf;
    stat (path, &buf);
    int nMalloc = sizeof(dev_t) + sizeof(ino_t) + sizeof(mode_t)+ sizeof(nlink_t)+ sizeof(uid_t)+ sizeof(gid_t)+ sizeof(dev_t)+ sizeof(off_t)+ sizeof(blksize_t)+ sizeof(blkcnt_t)+ sizeof(time_t)+ sizeof(time_t)+ sizeof(time_t)+ 1;
    char *pathForStatBuff = malloc(nMalloc);
    printf("%d\n",nMalloc);
    unsigned long base = 0;
    memcpy(pathForStatBuff + base,&buf.st_dev,sizeof(dev_t));
    base = base + sizeof(dev_t);    
    memcpy(pathForStatBuff + base,&buf.st_ino,sizeof(ino_t));
    base = base + sizeof(ino_t);
    memcpy(pathForStatBuff + base,&buf.st_mode,sizeof(mode_t));
    base = base + sizeof(mode_t);
    memcpy(pathForStatBuff + base,&buf.st_nlink,sizeof(nlink_t));
    base = base + sizeof(nlink_t);
    memcpy(pathForStatBuff + base,&buf.st_uid,sizeof(uid_t));
    base = base + sizeof(uid_t);
    memcpy(pathForStatBuff + base,&buf.st_gid,sizeof(gid_t));
    base = base + sizeof(gid_t);
    memcpy(pathForStatBuff + base,&buf.st_rdev,sizeof(dev_t));
    base = base + sizeof(dev_t);
    memcpy(pathForStatBuff + base,&buf.st_size,sizeof(off_t));
    base = base + sizeof(off_t);
    memcpy(pathForStatBuff + base,&buf.st_blksize,sizeof(blksize_t));
    base = base + sizeof(blksize_t);
    memcpy(pathForStatBuff + base,&buf.st_blocks,sizeof(blkcnt_t));
    base = base + sizeof(blkcnt_t);
    memcpy(pathForStatBuff + base,&buf.st_atime,sizeof(time_t));
    base = base + sizeof(time_t);
    memcpy(pathForStatBuff + base,&buf.st_mtime,sizeof(time_t));
    base = base + sizeof(time_t);
    memcpy(pathForStatBuff + base,&buf.st_ctime,sizeof(time_t));
    base = base + sizeof(time_t);   

    printf("Printing pathForStatBuff = %s\n",pathForStatBuff);
    return pathForStatBuff;
}

Printing pathForStatBuff = p
53
Printing pathForStatBuff = p
53
Printing pathForStatBuff = p
53
Printing pathForStatBuff = p
53
Printing pathForStatBuff = p
53
Printing pathForStatBuff = p
53
Printing pathForStatBuff = p
53
Printing pathForStatBuff = p
53
Printing pathForStatBuff = p
53
Printing pathForStatBuff = p
53
Printing pathForStatBuff = p

This is the output I always get, I can’t seem to figure out what I am doing wrong. Could you guys guide me on what it might be. Thanks.

  • 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-31T18:37:12+00:00Added an answer on May 31, 2026 at 6:37 pm

    At first sight, the code:

    int nMalloc = sizeof(dev_t)  + sizeof(ino_t) + sizeof(mode_t)+ ...
    

    is doing exactly the same as

    int nMalloc = sizeof(struct stat);
    

    Edit: WARNING – caf pointed out that the

    int nMalloc = sizeof(dev_t)  + sizeof(ino_t) + ...
    

    is not necessarily exactly the same as int nMalloc = sizeof(struct stat);

    The fields within struct stat might have alignment constraints which cause there to be ‘holes’ or padding in between fields. There might also be undocumented fields. So struct stat might be bigger than the sum of its published fields.

    This would be straightforward to check.

    if (nMalloc != sizeof(struct stat)) { fprintf(stderr, "...\n"); 
    

    but might change in future, so your original is more robust.

    Based on caf’s proposition, this is also not necessarily identical:

    unsigned long base = 0;
    memcpy(pathForStatBuff + base,&buf.st_dev,sizeof(dev_t));
    base = base + sizeof(dev_t);    
    ...
    memcpy(pathForStatBuff + base,&buf.st_ctime,sizeof(time_t));
    base = base + sizeof(time_t); 
    

    to:
    memcpy(pathForStatBuff, &buf, sizeof(struct stat));

    but the size test would detect that.

    So, field assignment to a new struct would allow you to get control of the fields needed, their order, and some aspects of layout (same compiler/same platform).

    If memcpy is preferred, look at mempcpy. It is like memcpy, but returns a pointer to the byte after the last one, so you don’t need to do:

    p = memcpy(pathForStatBuf  + base, ...
    base = base + sizeof(...)
    ...
    base = base + sizeof(...)
    p = memcpy(pathForStatBuf  + base, ...
    base = base + sizeof(...)
    

    but instead have an extra pointer and do

    p = memcpy(p, ...
    p = memcpy(p, ...
    

    Do you want to get a printable form? If that is the case, you will need to use sprintf, printf, or fprintf. As you are learning, I would convert all of the memcpy’s to printf("..., buf....field ...); and see them immediately, that will be easier than getting a string right first time.

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

Sidebar

Related Questions

I'm fairly new to C and have started out writing a small library with
I am fairly new to writing code, but I played around with some chrome
I'm fairly new to Lift/Scala but like the ideas of less code writing and
I've got a problem with some c# code I'm writing, I'm fairly new to
I am writing a piece of code in Java (I'm fairly new to Java)
I am fairly new to python, and have no experience with writing services for
I'm fairly new to writing test cases and this will be my first major
I'm fairly new to Java and am writing an app that needs an XML
I am fairly new to C++, having much more C experience. I am writing
My company is fairly new to unit testing our code. I've been reading about

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.