guide me to make this code better or efficient

This is a discussion on guide me to make this code better or efficient within the C++ Programming forums, part of the General Programming Boards category; given this prototype code how could I improve this? Code: #include "geocoding.h" int main() { vector<vector<vector<SURFACEMODEL> > > radar = ...

  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
    22,112
    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
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    907
    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
    22,112
    In that case you should remove the const qualifier. It also implies that your original code has a bug.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,112
    Rebuild the code and make sure that the function prototype is correct.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

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

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