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. Passing a pointer to a function/dynamic memory allocation

Passing a pointer to a function/dynamic memory allocation

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresperformancehelpquestion
7 Posts 6 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.
  • H Offline
    H Offline
    hpjchobbes
    wrote on last edited by
    #1

    I am trying to create a function that takes a pointer as a parameter that will end up being an array. The function returns the number of elements that will be in the array. I'm having a problem with the allocating part in the function. Here is what I have: struct FooStruct { int aFoo, bFoo; } int GetFooList(FooStruct *pFoos) { // Find out the max foos we're going to have pFoos = new FooStruct[MaxFoos]; return MaxFoos; } My function works, but the problem comes from the calling function. If I create a pointer to a FooStruct and pass that, it does not get updated with the address that the new comes back with. I still don't fully grasp pointers (just the basics) so I may be going about this the wrong way. I would imagine that this would be a standard situation with programming (calling a function to get an unknown number of items returned), but I don't know what this would be called so my searches have not come up with anything useful. Is this a good way to achieve what I am looking for, or am I wandering down the wrong path? Any suggestions or pointers (sorry for the pun) on ways to accomplish this would be greatly appreciated!!

    C C A Z D 6 Replies Last reply
    0
    • H hpjchobbes

      I am trying to create a function that takes a pointer as a parameter that will end up being an array. The function returns the number of elements that will be in the array. I'm having a problem with the allocating part in the function. Here is what I have: struct FooStruct { int aFoo, bFoo; } int GetFooList(FooStruct *pFoos) { // Find out the max foos we're going to have pFoos = new FooStruct[MaxFoos]; return MaxFoos; } My function works, but the problem comes from the calling function. If I create a pointer to a FooStruct and pass that, it does not get updated with the address that the new comes back with. I still don't fully grasp pointers (just the basics) so I may be going about this the wrong way. I would imagine that this would be a standard situation with programming (calling a function to get an unknown number of items returned), but I don't know what this would be called so my searches have not come up with anything useful. Is this a good way to achieve what I am looking for, or am I wandering down the wrong path? Any suggestions or pointers (sorry for the pun) on ways to accomplish this would be greatly appreciated!!

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

      hpjchobbes wrote:

      My function works, but the problem comes from the calling function. If I create a pointer to a FooStruct and pass that, it does not get updated with the address that the new comes back with. I still don't fully grasp pointers (just the basics) so I may be going about this the wrong way.

      That is the standard behavior. To make things easier, see pointers as being a value (the value is the address of a certain memory area). Thus you are in fact passing a value to a function, and the function will make a copy of this value for itself. You create a new array and you store this new value in the copy. Thus, your calling function will never be updated. To solve this 'problem', pass the pointer by reference. This instruct your function that it must work on the variable and not make a copy of it: int GetFooList(FooStruct* &pFoos)


      Cédric Moonen Software developer
      Charting control [v1.2]

      1 Reply Last reply
      0
      • H hpjchobbes

        I am trying to create a function that takes a pointer as a parameter that will end up being an array. The function returns the number of elements that will be in the array. I'm having a problem with the allocating part in the function. Here is what I have: struct FooStruct { int aFoo, bFoo; } int GetFooList(FooStruct *pFoos) { // Find out the max foos we're going to have pFoos = new FooStruct[MaxFoos]; return MaxFoos; } My function works, but the problem comes from the calling function. If I create a pointer to a FooStruct and pass that, it does not get updated with the address that the new comes back with. I still don't fully grasp pointers (just the basics) so I may be going about this the wrong way. I would imagine that this would be a standard situation with programming (calling a function to get an unknown number of items returned), but I don't know what this would be called so my searches have not come up with anything useful. Is this a good way to achieve what I am looking for, or am I wandering down the wrong path? Any suggestions or pointers (sorry for the pun) on ways to accomplish this would be greatly appreciated!!

        C Offline
        C Offline
        cmk
        wrote on last edited by
        #3

        In short:

        int GetFooList( FooStruct **ppFoos ) {
        ...
        *ppFoos = new FooStruct[MaxFoos];
        return(MaxFoos);
        }
        FooStruct *pFoos = NULL;
        int nFoos = GetFooList(&pFoos);

        In long: A pointer is just another data type like an integer (char, short, int, long, long long) or real (float, double). So, turn the function around, what if you wanted to return the pointer and update the size as a parameter:

        FooStruct* GetFooList( int *nFoos ) {
        ...
        FooStruct *pfs = new FooStruct[MaxFoos];
        *nFoos = MaxFoos;
        return(pfs);
        }
        int nFoos = 0;
        FoosStuct *pFoos = GetFooList(&nFoos);

        Don't get wrapped up by the fact that a pointer data type is defined with a *. [EDIT] Or, as Cedric pointed out, use references. I was trying to stay with a pointer based explaination. [/EDIT]

        ...cmk Save the whales - collect the whole set

        1 Reply Last reply
        0
        • H hpjchobbes

          I am trying to create a function that takes a pointer as a parameter that will end up being an array. The function returns the number of elements that will be in the array. I'm having a problem with the allocating part in the function. Here is what I have: struct FooStruct { int aFoo, bFoo; } int GetFooList(FooStruct *pFoos) { // Find out the max foos we're going to have pFoos = new FooStruct[MaxFoos]; return MaxFoos; } My function works, but the problem comes from the calling function. If I create a pointer to a FooStruct and pass that, it does not get updated with the address that the new comes back with. I still don't fully grasp pointers (just the basics) so I may be going about this the wrong way. I would imagine that this would be a standard situation with programming (calling a function to get an unknown number of items returned), but I don't know what this would be called so my searches have not come up with anything useful. Is this a good way to achieve what I am looking for, or am I wandering down the wrong path? Any suggestions or pointers (sorry for the pun) on ways to accomplish this would be greatly appreciated!!

          A Offline
          A Offline
          Arman S
          wrote on last edited by
          #4

          Well, the reason of failure is; "I still don't fully grasp pointers (just the basics)" :) The problem is that you pass the pointer by value; IOW the actual pointer gets copied and the very copy is passed to the function. Inside of the function, you assign the address [returned by new] to the copy of the original pointer and thus the original pointer will not be affected. To make it work as you expect, make the following changes in the function prototype;

          int GetFooList(FooStruct *& pFoos) {
          ...
          }

          Noticed? Now you pass the pointer by its reference.

          -- ===== Arman

          1 Reply Last reply
          0
          • H hpjchobbes

            I am trying to create a function that takes a pointer as a parameter that will end up being an array. The function returns the number of elements that will be in the array. I'm having a problem with the allocating part in the function. Here is what I have: struct FooStruct { int aFoo, bFoo; } int GetFooList(FooStruct *pFoos) { // Find out the max foos we're going to have pFoos = new FooStruct[MaxFoos]; return MaxFoos; } My function works, but the problem comes from the calling function. If I create a pointer to a FooStruct and pass that, it does not get updated with the address that the new comes back with. I still don't fully grasp pointers (just the basics) so I may be going about this the wrong way. I would imagine that this would be a standard situation with programming (calling a function to get an unknown number of items returned), but I don't know what this would be called so my searches have not come up with anything useful. Is this a good way to achieve what I am looking for, or am I wandering down the wrong path? Any suggestions or pointers (sorry for the pun) on ways to accomplish this would be greatly appreciated!!

            Z Offline
            Z Offline
            zhang800605
            wrote on last edited by
            #5

            struct FooStruct { int aFoo, bFoo; }; int GetFooList(FooStruct * & pFoos) { // Find out the max foos we're going to have pFoos = new FooStruct[MaxFoos]; return MaxFoos; }

            1 Reply Last reply
            0
            • H hpjchobbes

              I am trying to create a function that takes a pointer as a parameter that will end up being an array. The function returns the number of elements that will be in the array. I'm having a problem with the allocating part in the function. Here is what I have: struct FooStruct { int aFoo, bFoo; } int GetFooList(FooStruct *pFoos) { // Find out the max foos we're going to have pFoos = new FooStruct[MaxFoos]; return MaxFoos; } My function works, but the problem comes from the calling function. If I create a pointer to a FooStruct and pass that, it does not get updated with the address that the new comes back with. I still don't fully grasp pointers (just the basics) so I may be going about this the wrong way. I would imagine that this would be a standard situation with programming (calling a function to get an unknown number of items returned), but I don't know what this would be called so my searches have not come up with anything useful. Is this a good way to achieve what I am looking for, or am I wandering down the wrong path? Any suggestions or pointers (sorry for the pun) on ways to accomplish this would be greatly appreciated!!

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              See this thread.


              "A good athlete is the result of a good and worthy opponent." - David Crow

              "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

              1 Reply Last reply
              0
              • H hpjchobbes

                I am trying to create a function that takes a pointer as a parameter that will end up being an array. The function returns the number of elements that will be in the array. I'm having a problem with the allocating part in the function. Here is what I have: struct FooStruct { int aFoo, bFoo; } int GetFooList(FooStruct *pFoos) { // Find out the max foos we're going to have pFoos = new FooStruct[MaxFoos]; return MaxFoos; } My function works, but the problem comes from the calling function. If I create a pointer to a FooStruct and pass that, it does not get updated with the address that the new comes back with. I still don't fully grasp pointers (just the basics) so I may be going about this the wrong way. I would imagine that this would be a standard situation with programming (calling a function to get an unknown number of items returned), but I don't know what this would be called so my searches have not come up with anything useful. Is this a good way to achieve what I am looking for, or am I wandering down the wrong path? Any suggestions or pointers (sorry for the pun) on ways to accomplish this would be greatly appreciated!!

                H Offline
                H Offline
                hpjchobbes
                wrote on last edited by
                #7

                I see where my mistake was. I was not thinking of the pointer being passed as a value. Thanks everyone for the replies. I'm going to need to play around with this a lot more so I get a full understanding of it! I also like the idea of swapping the parameter and the return value. This is a lot easier to understand. I should have thought of that myself. I'll read up on the link that was provided. I did some searches on 'array pointer', but everything I found was about having a known array size at compile time. Thanks for the link, it looks like it has a lot of good information!

                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