Find the answer to your Linux question:
Results 1 to 10 of 10
Hi all, I just have basic doubt on sizeof. any function name size is giving as 1 byte. int main() { printf("%d", sizeof(main); // or printf("%d", sizeof(printf)); return 0; } ...
  1. #1
    Just Joined!
    Join Date
    Aug 2008
    Posts
    49

    sizeof function

    Hi all,

    I just have basic doubt on sizeof. any function name size is giving as 1 byte.

    int main()
    {
    printf("%d", sizeof(main); // or
    printf("%d", sizeof(printf));
    return 0;
    }

    or some user defined function also the same.

    Please any one tell, why it is giving as 1 byte, if it pointer i can understand that it is 4 bytes.

  2. #2
    Linux Engineer b2bwild's Avatar
    Join Date
    Jul 2008
    Location
    Behind You!
    Posts
    1,108
    #include<iostream>
    using namespace std;
    int main()
    {
    cout << sizeof(main());
    return 0;
    }

    It returns 4.
    Never make any misteaks.

    Read my Blog at --> Penguin Inside Subscribe Feed

  3. #3
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    It should be returning either 4 or 8 bytes depending on the system 32 or 64 bit....Gerard4143
    Make mine Arch Linux

  4. #4
    Linux Engineer b2bwild's Avatar
    Join Date
    Jul 2008
    Location
    Behind You!
    Posts
    1,108
    he wrote sizeof(main) instead sizeof(main())
    Never make any misteaks.

    Read my Blog at --> Penguin Inside Subscribe Feed

  5. #5
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Quote Originally Posted by b2bwild View Post
    he wrote sizeof(main) instead sizeof(main())
    I always thought you could use sizeof(main)...I guess I learned something today...Gerard4143
    Make mine Arch Linux

  6. #6
    Just Joined!
    Join Date
    Apr 2009
    Location
    South East England
    Posts
    40
    Remember that although a function pointer can be assigned to from the function name, the function name itself is not a pointer. K&R states that sizeof cannot be applied to an operator of function type, and that if you do you will get an implementation-dependant unsigned integer ... in your case 1 it would seem.

    I think the trick is to do sizeof(&main).

  7. #7
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Quote Originally Posted by PastAsNew View Post
    Remember that although a function pointer can be assigned to from the function name, the function name itself is not a pointer. K&R states that sizeof cannot be applied to an operator of function type, and that if you do you will get an implementation-dependant unsigned integer ... in your case 1 it would seem.

    I think the trick is to do sizeof(&main).
    When at C, listen to Captains Kernighan and Ritchie. They will keep you on course!
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  8. #8
    Just Joined!
    Join Date
    Aug 2008
    Posts
    49

    find executable file path

    Hi All,

    I have to find the executable file path by using the PID (process ID).

    User will input some PID and required output is corresponding executable file path.
    For this i have to write a C program using system call. Anyone help me.

  9. #9
    Just Joined!
    Join Date
    Apr 2009
    Location
    South East England
    Posts
    40
    Quote Originally Posted by dayananda.ms View Post
    Hi All,

    I have to find the executable file path by using the PID (process ID).

    User will input some PID and required output is corresponding executable file path.
    For this i have to write a C program using system call. Anyone help me.
    I don't know the answer, but you'll stand a much better chance of the right person seeing this if you post it into its own new thread, rather than this one that's all about sizeof.

    Edit: Ah I see you did. This one was obviously a mistook

  10. #10
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Quote Originally Posted by dayananda.ms View Post
    Hi All,

    I have to find the executable file path by using the PID (process ID).

    User will input some PID and required output is corresponding executable file path.
    For this i have to write a C program using system call. Anyone help me.
    Open /proc/<pid>/cmdline for read, then read the single line found in the file. That is the command line that was used to start the process.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...