Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Easy questions about C++ [modified]

Easy questions about C++ [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
questioncsharpc++visual-studioperformance
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    akirilov
    wrote on last edited by
    #1

    Hi guys, I'm using Visual Studio 2005 and I have few questions: 1. If I have function without parameters, what to do to skip () when invoking the function? 2. I have:

    int Mem;
    ...
    Mem = LocalAlloc(LHND, ...);
    ...
    WriteFile(f, Mem,...)

    Mem keeps address of memory block. What expression to write in WriteFile(f, ???Mem,...) to write the memory block pointed by Mem in file? 3. Is there a way a class member field to be accessible just for reading without writing? P.S. If you know the answer of at least 1 question, please share it.

    modified on Friday, November 21, 2008 6:29 AM

    C C 2 Replies Last reply
    0
    • A akirilov

      Hi guys, I'm using Visual Studio 2005 and I have few questions: 1. If I have function without parameters, what to do to skip () when invoking the function? 2. I have:

      int Mem;
      ...
      Mem = LocalAlloc(LHND, ...);
      ...
      WriteFile(f, Mem,...)

      Mem keeps address of memory block. What expression to write in WriteFile(f, ???Mem,...) to write the memory block pointed by Mem in file? 3. Is there a way a class member field to be accessible just for reading without writing? P.S. If you know the answer of at least 1 question, please share it.

      modified on Friday, November 21, 2008 6:29 AM

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      akirilov wrote:

      1. If I have function without parameters, what to do to skip () when invoking the function?

      You can't. Why do you want to do such a thing ?

      akirilov wrote:

      int Mem; - it keep address of memory block.

      Why are you working with integers to store addresses ? Why not manipulate pointers directly, it will make your life much easier.

      akirilov wrote:

      3. Is there a way a class member field to be accessible just for reading without writing?

      Make it private and provide a getter method and no setter method in the class.

      Cédric Moonen Software developer
      Charting control [v1.5] OpenGL game tutorial in C++

      A 1 Reply Last reply
      0
      • A akirilov

        Hi guys, I'm using Visual Studio 2005 and I have few questions: 1. If I have function without parameters, what to do to skip () when invoking the function? 2. I have:

        int Mem;
        ...
        Mem = LocalAlloc(LHND, ...);
        ...
        WriteFile(f, Mem,...)

        Mem keeps address of memory block. What expression to write in WriteFile(f, ???Mem,...) to write the memory block pointed by Mem in file? 3. Is there a way a class member field to be accessible just for reading without writing? P.S. If you know the answer of at least 1 question, please share it.

        modified on Friday, November 21, 2008 6:29 AM

        C Offline
        C Offline
        Code o mat
        wrote on last edited by
        #3

        1. As far as i know there's no standard way to do that. 2. What do you mean? If you want the address of the Mem variable, then use &, so &Mem. If you store a memory location in Mem, then use a pointer type, like int *Mem, but if you want to convert int to a pointer, then you can use for example (void *)Mem. 3. You can declare const "variables" to which you specify a value and then it cannot be modified anymore. For example const int m_Constant;, you would give a value to this in the contstructor of yor class. If you want to modify a variable "inside" the containing class but not outside then declare it as protected or private amd supply either a Get method for it or a const reference to it. so if you have int m_variable; then you can do either const int &GetVariable() { return m_variable; } or you can do const int &m_constVariable; and initialize this in the constructor using m_variable.

        A 1 Reply Last reply
        0
        • C Cedric Moonen

          akirilov wrote:

          1. If I have function without parameters, what to do to skip () when invoking the function?

          You can't. Why do you want to do such a thing ?

          akirilov wrote:

          int Mem; - it keep address of memory block.

          Why are you working with integers to store addresses ? Why not manipulate pointers directly, it will make your life much easier.

          akirilov wrote:

          3. Is there a way a class member field to be accessible just for reading without writing?

          Make it private and provide a getter method and no setter method in the class.

          Cédric Moonen Software developer
          Charting control [v1.5] OpenGL game tutorial in C++

          A Offline
          A Offline
          akirilov
          wrote on last edited by
          #4

          Make it private and provide a getter method and no setter method in the class. Invoking methods is slow. Is there any other way (Something like in Delphi - you can give read/write access to a member, without involving methods)? Why not manipulate pointers directly, it will make your life much easier. Mainly, because I'don't know how :) and also I had problems with pointer arithmetics. Anyway could you share a code fragment that will do the trick (with pointers) (if possible with int)?

          M C 2 Replies Last reply
          0
          • C Code o mat

            1. As far as i know there's no standard way to do that. 2. What do you mean? If you want the address of the Mem variable, then use &, so &Mem. If you store a memory location in Mem, then use a pointer type, like int *Mem, but if you want to convert int to a pointer, then you can use for example (void *)Mem. 3. You can declare const "variables" to which you specify a value and then it cannot be modified anymore. For example const int m_Constant;, you would give a value to this in the contstructor of yor class. If you want to modify a variable "inside" the containing class but not outside then declare it as protected or private amd supply either a Get method for it or a const reference to it. so if you have int m_variable; then you can do either const int &GetVariable() { return m_variable; } or you can do const int &m_constVariable; and initialize this in the constructor using m_variable.

            A Offline
            A Offline
            akirilov
            wrote on last edited by
            #5

            2. With some code will be easier to explain:

            int Mem;
            ...
            Mem = LocalAlloc(LHND, ...);
            ...
            WriteFile(f, (void *) Mem,...)

            I receive exception during runtime, so my question is what expression to use so it will work properly (writes the pointed by Mem block to file)

            C 1 Reply Last reply
            0
            • A akirilov

              2. With some code will be easier to explain:

              int Mem;
              ...
              Mem = LocalAlloc(LHND, ...);
              ...
              WriteFile(f, (void *) Mem,...)

              I receive exception during runtime, so my question is what expression to use so it will work properly (writes the pointed by Mem block to file)

              C Offline
              C Offline
              Code o mat
              wrote on last edited by
              #6

              Ok, new edit, i misread you there the first time. LocalAlloc gives you a handle to a memory location, not the memory location itself. Declare your variable as HANDLE, not int, since LocalALloc, unless specified differently, will give you a HANDLE. You have to use LocalLock and LocalUnlock to actually access the memory. But may i ask why you need to use LocalAlloc instead of new or malloc? So basicly, if you have to use LocalAlloc you have to LocalLock(handle), this will give you a void * pointer that points at the memory you allocated, you can feed this to WriteFile (didn't go into WriteFile itself so i assume you know what you are doing with that) and then use LocalUnlock to unlock the memory, also use LocalFree to free it.

              modified on Friday, November 21, 2008 6:36 AM

              A 1 Reply Last reply
              0
              • A akirilov

                Make it private and provide a getter method and no setter method in the class. Invoking methods is slow. Is there any other way (Something like in Delphi - you can give read/write access to a member, without involving methods)? Why not manipulate pointers directly, it will make your life much easier. Mainly, because I'don't know how :) and also I had problems with pointer arithmetics. Anyway could you share a code fragment that will do the trick (with pointers) (if possible with int)?

                M Offline
                M Offline
                Maximilien
                wrote on last edited by
                #7

                akirilov wrote:

                Invoking methods is slow

                no; using inline might help a little bit, and probably the compiler will optimize this also.

                akirilov wrote:

                because I'don't know how

                what a good occasion to learn.

                This signature was proudly tested on animals.

                1 Reply Last reply
                0
                • A akirilov

                  Make it private and provide a getter method and no setter method in the class. Invoking methods is slow. Is there any other way (Something like in Delphi - you can give read/write access to a member, without involving methods)? Why not manipulate pointers directly, it will make your life much easier. Mainly, because I'don't know how :) and also I had problems with pointer arithmetics. Anyway could you share a code fragment that will do the trick (with pointers) (if possible with int)?

                  C Offline
                  C Offline
                  Cedric Moonen
                  wrote on last edited by
                  #8

                  akirilov wrote:

                  Invoking methods is slow.

                  As a famous guy already said: "Premature optimization is the root of all evil". What it means here is that you try to have only optimization in mind even when it makes your code less readable or maintainable. Unless your function has to be called very often and is in a time critical part of your code, I think you can forget about the performances (and you can also rely on your compiler for a bit of optimizations). If I take your way of thinking to the extreme, you can end up with one gigantic main function because you never wanted to make function calls...

                  akirilov wrote:

                  Anyway could you share a code fragment that will do the trick (with pointers) (if possible with int)?

                  I think you should learn that because it provides type safety which is not the case when working with ints containing an address. What kind of data are you manipulating ? An array of ints ?

                  Cédric Moonen Software developer
                  Charting control [v1.5] OpenGL game tutorial in C++

                  1 Reply Last reply
                  0
                  • C Code o mat

                    Ok, new edit, i misread you there the first time. LocalAlloc gives you a handle to a memory location, not the memory location itself. Declare your variable as HANDLE, not int, since LocalALloc, unless specified differently, will give you a HANDLE. You have to use LocalLock and LocalUnlock to actually access the memory. But may i ask why you need to use LocalAlloc instead of new or malloc? So basicly, if you have to use LocalAlloc you have to LocalLock(handle), this will give you a void * pointer that points at the memory you allocated, you can feed this to WriteFile (didn't go into WriteFile itself so i assume you know what you are doing with that) and then use LocalUnlock to unlock the memory, also use LocalFree to free it.

                    modified on Friday, November 21, 2008 6:36 AM

                    A Offline
                    A Offline
                    akirilov
                    wrote on last edited by
                    #9

                    LocalAlloc gives you a handle to a memory location, not the memory location itself Actually, that depends on the first parameter. In my case (for Windows Mobile) it always return the actual address, so it is OK.

                    C 1 Reply Last reply
                    0
                    • A akirilov

                      LocalAlloc gives you a handle to a memory location, not the memory location itself Actually, that depends on the first parameter. In my case (for Windows Mobile) it always return the actual address, so it is OK.

                      C Offline
                      C Offline
                      Code o mat
                      wrote on last edited by
                      #10

                      As i said, "unless specified differently". So if you get a pointer at the memory location why do you store it in an int?

                      B 1 Reply Last reply
                      0
                      • C Code o mat

                        As i said, "unless specified differently". So if you get a pointer at the memory location why do you store it in an int?

                        B Offline
                        B Offline
                        bulg
                        wrote on last edited by
                        #11

                        I agree. But if you insist, then you should just 'cast' it as a pointer when you Write - WriteFile(... (long*)Mem, // mem is an address ...

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups