Thread: guide me to make this code better or efficient

  1. #1
    Registered User
    Join Date
    Jul 2008
    Location
    Barcelona
    Posts
    41

    guide me to make this code better or efficient

    given this prototype code how could I improve this?
    Code:
    #include "geocoding.h"
    int main()
    {
    	vector<vector<vector<SURFACEMODEL> > > radar = invgeocoding();
    	dirgeocoding(radar);
    	return 0;
    }
    
    #include "geocoding.h"
    vector<vector<vector<SURFACEMODEL> > > invgeocoding()
    {
    	// read input parameters
    
        // memory allocations
        vector<vector<SURFACEMODEL> > dtmpixel(ny_dtm, vector<SURFACEMODEL>(nx_dtm));
        vector<vector<vector<SURFACEMODEL> > > radarpixel(ny_radar, vector<vector<SURFACEMODEL> >(nx_radar));
    
    	// read SURFACEMODEL data binary file
    
    	// copy relevant surface data to radar dimension matrix (smaller than surface)
    
    	return radarpixel;
    }
    
    
    #include "geocoding.h"
    void dirgeocoding(vector<vector<vector<SURFACEMODEL> > > radarpixel)
    {
    	//  interpolation of height data for each radarpixel
    }
    I am learning C++ by doing, I am used to prototyping / calculating with matlab but don't really have the grip on references and pointers in C. Considering my surface model could be hundreds of MB's should I free memory somewhere? Typically surface 2D vector (dtmpixel) should be much larger than radar 3D vector (radarpixel). In function dirgeocoding I need to calculate some values of class SURFACEMODEL. I would like to advance a little bit and don't waste too much unnecessary memory.
    Best regards

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You probably do not want to make a copy of a potentially huge vector of vectors of vectors, so change this:
    Code:
    void dirgeocoding(vector<vector<vector<SURFACEMODEL> > > radarpixel)
    to:
    Code:
    void dirgeocoding(const vector<vector<vector<SURFACEMODEL> > >& radarpixel)
    Note that invgeocoding() returns by value, but return value optimisation would alleviate this unnecessary copying problem.
    Last edited by laserlight; 01-18-2009 at 02:28 PM. Reason: bector -> vector
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    You can use a reference. It functions as an alias, and often doesn't have to use a pointer.
    Code:
    typedef vector<vector<vector<SURFACEMODEL> > > big_t;
    void initialize(big_t & ref);
    int main()
    {
        big_t radar;
        initialize(radar); //radar is not copied
    }
    
    void initialize(big_t & ref)
    {
        //Do stuff to ref, which refers to the variable in main()
    }
    This way if you have one big object, you only need that one.

    Is this what you were asking?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Registered User
    Join Date
    Jul 2008
    Location
    Barcelona
    Posts
    41
    I tried that but I doesn't compile after I change that. I get:

    D:\IG\program\geocoding\090117\dirgeocoding.cpp:14 : error: passing `const PIXEL' as `this' argument of `void PIXEL::printdata()' discards qualifiers

    In function dirgeocoding I need to change some values of SURFACEMODEL and at the moment I just call radarpixel[y][x][n].printdata(); for testing which earlier worked to print the variables of the sought instance.
    Any idea?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In that case you should remove the const qualifier. It also implies that your original code has a bug.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jul 2008
    Location
    Barcelona
    Posts
    41
    when I remove const I get:
    Release\main.o:main.cpp:(.text+0x5ea): undefined reference to `dirgeocoding(std::vector<std::vector<std::vector< SURFACEMODEL , std::allocator<SURFACEMODEL > >, std::allocator<std::vector<SURFACEMODEL , std::allocator<SURFACEMODEL > > > >, std::allocator<std::vector<std::vector<SURFACEMODE L , std::allocator<SURFACEMODEL > >, std::allocator<std::vector<PIXEL, std::allocator<SURFACEMODEL

    and sorry for the confusion, but I changed the name of SURFACEMODEL to PIXEL in previous post.
    cheers

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Rebuild the code and make sure that the function prototype is correct.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jul 2008
    Location
    Barcelona
    Posts
    41
    I tried that and thanks, but no go

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then show the current code?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by larne View Post
    I tried that but I doesn't compile after I change that. I get:

    D:\IG\program\geocoding\090117\dirgeocoding.cpp:14 : error: passing `const PIXEL' as `this' argument of `void PIXEL:rintdata()' discards qualifiers

    In function dirgeocoding I need to change some values of SURFACEMODEL and at the moment I just call radarpixel[y][x][n].printdata(); for testing which earlier worked to print the variables of the sought instance.
    Any idea?
    Does printdata() actually modify the pixel data? If not, then it should be declared const in the first place.
    If it's is intended that printdata() should modify the data, then as Laserlight says, you should not call it with const reference. There are now two possibly correct options:
    • If you intend for it to modify the actual data in the original vector of vectors, then you will need to pass it by reference, not copy it by calling by value as you are in the original code.
    • If you actually modify the data in the vector of vector, and you do not want to modify the original data to be modified, you should make a copy, and call by value is one way to achieve that.


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Jul 2008
    Location
    Barcelona
    Posts
    41
    I changed my function dirgeocoding to
    Code:
    void dirgeocoding(const vector<vector<vector<SURFACEMODEL> > >& radarpixel)
    as suggested by laserlight. I skipped const since I will want to calculate a value of SURFACEMODEL in function dirgeocoding. However i get a strange builderror like this
    Code:
    --------------------Configuration: main - Win32 Debug--------------------
    Linking...
    main.obj : error LNK2001: unresolved external symbol "void __cdecl dirgeocoding(class std::vector<class std::vector<class std::vector<class SURFACEMODEL,class std::allocator<class SURFACEMODEL> >,class std::allocator<class std::vector<class SURFACEMODEL,class std::alloc
    ator<class SURFACEMODEL> > > >,class std::allocator<class std::vector<class std::vector<class SURFACEMODEL,class std::allocator<class SURFACEMODEL> >,class std::allocator<class std::vector<class SURFACEMODEL,class std::allocator<class SURFACEMODEL> > > > > >)" (?dirgeocoding@@YAXV?$v
    ector@V?$vector@V?$vector@VSURFACEMODEL@@V?$allocator@VSURFACEMODEL@@@std@@@std@@V?$allocator@V?$vector@VSURFACEMODEL@@V?$allocator@VSURFACEMODEL@@@std@@@std@@@2@@std@@V?$allocator@V?$vector@V?$vector@VSURFACEMODEL@@V?$allocator@VSURFACEMODEL@@@std@@@std@@V?$allocator@V?$vector@VSURFACEMODEL@@V?$a
    llocator@VSURFACEMODEL@@@std@@@std@@@2@@std@@@2@@std@@@Z)
    Debug/main.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.
    
    main.exe - 2 error(s), 0 warning(s)

  12. #12
    Registered User
    Join Date
    Jul 2008
    Location
    Barcelona
    Posts
    41
    I found the error, I had not changed the declaration of the function in header file. Thanks for all your help, and sorry for wasting your time with this last one =/
    Cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 07-30-2005, 11:28 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. How efficient is this code?
    By Morgan in forum C Programming
    Replies: 6
    Last Post: 05-23-2003, 04:47 AM
  4. the definitive guide to code commenting
    By Aran in forum C++ Programming
    Replies: 5
    Last Post: 09-16-2002, 01:43 PM
  5. How do I write more efficient code?
    By minesweeper in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 11:08 AM