Allocating memory for function argument 
Author Message
 Allocating memory for function argument

Hello,
this function

int getPackage (const char *id, char *buffer, istream fp);

should, depending on the value of id, put different strings with different
sizes in buffer. How and where do I allocate memory for the pointer? I'd
like to use it like this:

main {
    // don't know how much memory to allocate, therefore we can't do it
    char *buffer = NULL;
    istream fp("file.pak", ios::binary);
    getPackage("image4", buffer, fp);
    cout << buffer << endl;
    fp.close();
    return 0;
    // don't want to delete[] anything either

Quote:
}

How should getPackage allocate the memory? From what I understand, functions
like "strcpy" works like this; no allocating and no deleting needed.

--
Magnus Hult



Wed, 03 Jul 2002 03:00:00 GMT  
 Allocating memory for function argument

Quote:

> Hello,
> this function

> int getPackage (const char *id, char *buffer, istream fp);

> should, depending on the value of id, put different strings with different
> sizes in buffer. How and where do I allocate memory for the pointer? I'd
> like to use it like this:

> main {
>     // don't know how much memory to allocate, therefore we can't do it
>     char *buffer = NULL;
>     istream fp("file.pak", ios::binary);
>     getPackage("image4", buffer, fp);
>     cout << buffer << endl;
>     fp.close();
>     return 0;
>     // don't want to delete[] anything either
> }

> How should getPackage allocate the memory? From what I understand, functions
> like "strcpy" works like this; no allocating and no deleting needed.

> --
> Magnus Hult


Hi Functions like strcpy work strictly on memory allocated by the caller.  In
this case you are the caller and must provide allocated memory for buffer.  When
you do not need the memory after your function returns use automatic allocation:

char buffer[256];
getPackage(  buffer, );

That buffer is deallocated when you return.  As for the size, you will have to
make it big enough for what you expect.  Most functions return a count of how
many chars were returned.

--
Scott McPhillips [VC++ MVP]



Wed, 03 Jul 2002 03:00:00 GMT  
 Allocating memory for function argument
Problem is, buffer can contain anything from a couple of bytes to several
megs. I could of course make another function checking how many bytes needed

char *buffer = new char[getPackageSize("id", fp)];
getPackage("id", buffer, fp);

Isn't there a more conveinient (excuse the spelling on that last word, I
really have no idea of how it's spelled) way?

(Also, what is an MVP?)

/ Magnus



Quote:

> > Hello,
> > this function

> > int getPackage (const char *id, char *buffer, istream fp);

> > should, depending on the value of id, put different strings with
different
> > sizes in buffer. How and where do I allocate memory for the pointer? I'd
> > like to use it like this:

> > main {
> >     // don't know how much memory to allocate, therefore we can't do it
> >     char *buffer = NULL;
> >     istream fp("file.pak", ios::binary);
> >     getPackage("image4", buffer, fp);
> >     cout << buffer << endl;
> >     fp.close();
> >     return 0;
> >     // don't want to delete[] anything either
> > }

> > How should getPackage allocate the memory? From what I understand,
functions
> > like "strcpy" works like this; no allocating and no deleting needed.

> > --
> > Magnus Hult

> Hi Functions like strcpy work strictly on memory allocated by the caller.
In
> this case you are the caller and must provide allocated memory for buffer.
When
> you do not need the memory after your function returns use automatic
allocation:

> char buffer[256];
> getPackage(  buffer, );

> That buffer is deallocated when you return.  As for the size, you will
have to
> make it big enough for what you expect.  Most functions return a count of
how
> many chars were returned.

> --
> Scott McPhillips [VC++ MVP]



Wed, 03 Jul 2002 03:00:00 GMT  
 Allocating memory for function argument
If you are writing the getPackage() function yourself, you can calculate the
size of the string based on the id and then allocate the memory with new and
then fill it. The advantage to this technique is that if you do something
like loading this from a resource on a Mac (it sounds like your getPackage()
function does some analog of this) you can replace the new call with the
function loading, because the pointer will just be assigned to point to
whatever block the resource management calls load.

Quote:
> Hello,
> this function

> int getPackage (const char *id, char *buffer, istream fp);

> should, depending on the value of id, put different strings with different
> sizes in buffer. How and where do I allocate memory for the pointer? I'd
> like to use it like this:

> main {
>     // don't know how much memory to allocate, therefore we can't do it
>     char *buffer = NULL;
>     istream fp("file.pak", ios::binary);
>     getPackage("image4", buffer, fp);
>     cout << buffer << endl;
>     fp.close();
>     return 0;
>     // don't want to delete[] anything either
> }

> How should getPackage allocate the memory? From what I understand,
functions
> like "strcpy" works like this; no allocating and no deleting needed.

> --
> Magnus Hult




Wed, 03 Jul 2002 03:00:00 GMT  
 Allocating memory for function argument

Quote:

> Problem is, buffer can contain anything from a couple of bytes to several
> megs. I could of course make another function checking how many bytes needed

> char *buffer = new char[getPackageSize("id", fp)];
> getPackage("id", buffer, fp);

> Isn't there a more conveinient (excuse the spelling on that last word, I
> really have no idea of how it's spelled) way?

> (Also, what is an MVP?)

> / Magnus

No, there is not a more convenient way in this situation.  A computer program is
a primitive thing that must manage the details.  There are come classes
available (CString, string) that expand their memory as necessary because they
already contain such code, but your getPackageSize doesn't accept one of those.

MVP is Most Valuable Professional, an award from Microsoft for helping out
around here.  http://support.microsoft.com/support/mvp/
--
Scott McPhillips [VC++ MVP]



Wed, 03 Jul 2002 03:00:00 GMT  
 Allocating memory for function argument

Quote:

> int getPackage (const char *id, char *buffer, istream fp);

Beside your question:                                  \|/
  int getPackage (const char *id, char *buffer, istream & fp);

Don't pass streams by value.

rdu



Thu, 04 Jul 2002 03:00:00 GMT  
 Allocating memory for function argument
    Since it's not (only) the contents of the buffer that will be changed by
getPackage, but the char* itself, then it's the char* that must passed by
pointer:

    int getPackage(const char*id, char** pptr, istream& fp)
    {
        char* ptr = new char[ whatever_you_need];
        :
        :
        *pptr= ptr;
    }

    then:

    char*    buffer;
    getPackage(id, &buffer, fp)
    // buffer now set right,

    Alternately, you can return the char*, and pass the int back via a int*
parameter.
--
Truth,
James Curran
http://www.NJTheater.com
http://www.NJTheater.com/JamesCurran


Quote:
> Hello,
> this function

> int getPackage (const char *id, char *buffer, istream fp);

> should, depending on the value of id, put different strings with different
> sizes in buffer. How and where do I allocate memory for the pointer? I'd
> like to use it like this:

> main {
>     // don't know how much memory to allocate, therefore we can't do it
>     char *buffer = NULL;
>     istream fp("file.pak", ios::binary);
>     getPackage("image4", buffer, fp);
>     cout << buffer << endl;
>     fp.close();
>     return 0;
>     // don't want to delete[] anything either
> }

> How should getPackage allocate the memory? From what I understand,
functions
> like "strcpy" works like this; no allocating and no deleting needed.

> --
> Magnus Hult




Sat, 06 Jul 2002 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. allocating memory in one function and freeing it with another function

2. allocating memory with functions

3. Allocating memory inside a function

4. allocating memory and processing thru function.

5. Do the string functions allocate memory?

6. standard variadic functions allocate memory?

7. getline function that allocates memory on-the-fly

8. allocate memory for function

9. allocate memory in a function

10. C functions that allocate memory

11. Freeing memory allocated inside a function

12. How to access memory allocated in function

 

 
Powered by phpBB® Forum Software