Passing pointers as function parameters

This is a discussion on Passing pointers as function parameters within the C Programming forums, part of the General Programming Boards category; Hello, Good C programming practices talk about passing variables as parameters to function opposite to global vars. I have seen ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    145

    Passing pointers as function parameters

    Hello,

    Good C programming practices talk about passing variables as parameters to function opposite to global vars. I have seen in my own programs this is a good practice as the compiler optimize those variables very well.

    Now I have a problem. My new program uses a vector of very large structs. I have noted I can not pass the complete struct as a parameter as it takes many time to copy the structure, so the only plausible way to do it is passing a pointer to the struct I am going to modify.

    So far is ok, but I have doubts about the way the compiler optimize the struct access thought the pointer. I have hear of aliasing been a optimization problem when using pointers, but I have not clue what this mean. My program is not multithread so I am sure there is only single access to variables, but I know the compiler does not know that and it does not optimize this kind of access the same way as it would ve a parameter variable of the function.

    I admit I am completely stupid in how the compiler works, about all when working with pointer, so my question is what must I have take into account in order to point the compiler to optimize in the best way the access to the struct thought pointers?, is there guidelines on how can I take the best optimization of them? must I instruct the compiler (via options or via programming) to best optimize?

    thx

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    145
    Quote Originally Posted by quzah View Post
    Code:
    struct foo
    { } myarrayoffoo[ SOMEFOOSIZE ];
    ...
    
    void somefun( struct foo *yay )
    {
    }
    
    ...
    
    somefun( myarrayoffoo );
    Quzah.
    Dont understand what you are trying to say me.
    In my program, following your example I call a function like: somefun(&myarrayofffoo[48])
    I mean, I pass only a concrete pointer to a record, and the complete struct.
    What I like is to know how the compiler use pointer inside the function and if there are ways to optimize it as it would be a local var (or similar)

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I completely misread what you were trying to ask.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 09:28 AM
  2. Replies: 11
    Last Post: 12-30-2009, 04:04 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Passing a 2d Array of pointers to a Function
    By miclus in forum C Programming
    Replies: 6
    Last Post: 09-11-2004, 08:34 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21