473,395 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Passing Stream object by reference

I am trying to write a function that takes a reference to an object as its
arguement. The object is created in another function and I am trying to pass
the object to another function from within the function that created the
object. I have cut out some code as I dont think showing a for loop is
helpful.

void TMP3_Main_Form::LoadMP3()
{
TMemoryStream *MP3Stream = new TMemoryStream();
MP3Stream->LoadFromFile(FileListBox1->Items->Strings[i].c_str());
GetMP3Data(&MP3Stream);
....
....
....
}

the object is created within this function. I load a file into the stream. I
then want to pass that stream to another function to perform other tasks on
the stream.

My GetMP3Data function accepts a pointer to a stream as its arguement.
void TMP3_Main_Form::GetMP3Data(TMemoryStream *MP3)
{
char DATA[3]={0,0,0};
MP3.Position = MP3.Seek(-128,soFromEnd);
MP3.Read(DATA,3);

/* Have tried
MP3->Position = MP3->Seek(-128,soFromEnd);
MP3->Read(DATA,3);
also but this didnt seem to work either
*/
....
....
....
}

I am having trouble with the * and & operators. I can use and process
MP3Stream within the LoadTag() function but not with the GetMP3Data
function. I am trying to avoid having to reload the file from disk from
scratch in the GetMP3Data function to get the data into the function.
Ideally I want to be able to load my file once into a stream in memory, do
my processing with a couple of different functions on the same stream, and
then rewrite the file back to disk from my updated stream. At the moment I
have to keep reloading the file from disk as I cant pass by reference
properly.

Can anyone help?

Thanks in advance
Jul 22 '05 #1
6 3669
Ian Robertson wrote:
I am trying to write a function that takes a reference to an object as
its arguement. The object is created in another function and I am
trying to pass the object to another function from within the function
that created the object. I have cut out some code as I dont think
showing a for loop is helpful.

void TMP3_Main_Form::LoadMP3()
{
TMemoryStream *MP3Stream = new TMemoryStream();
MP3Stream->LoadFromFile(FileListBox1->Items->Strings[i].c_str());
GetMP3Data(&MP3Stream);
...
...
...
}

the object is created within this function. I load a file into the
stream. I then want to pass that stream to another function to perform
other tasks on the stream.

My GetMP3Data function accepts a pointer to a stream as its arguement.
But above, a pointer to a pointer to a TMemoryStream is passed to
GetMP3Data. MP3Stream is a pointer to TMemoryStream, and &MP3Stream is
the address of that pointer. You want to pass the pointer itself, so
leave out the &.
void TMP3_Main_Form::GetMP3Data(TMemoryStream *MP3)
{
char DATA[3]={0,0,0};
MP3.Position = MP3.Seek(-128,soFromEnd);
MP3.Read(DATA,3);

/* Have tried
MP3->Position = MP3->Seek(-128,soFromEnd);
MP3->Read(DATA,3);
also but this didnt seem to work either
*/
The latter is correct. Are you sure you got the error _within_ that
function? Unfortunately, you forgot to post the error messages you got.
...
...
...
}

I am having trouble with the * and & operators. I can use and process
MP3Stream within the LoadTag() function but not with the GetMP3Data
function. I am trying to avoid having to reload the file from disk
from scratch in the GetMP3Data function to get the data into the
function. Ideally I want to be able to load my file once into a stream
in memory, do my processing with a couple of different functions on
the same stream, and then rewrite the file back to disk from my
updated stream. At the moment I have to keep reloading the file from
disk as I cant pass by reference properly.

Can anyone help?


I wonder where the reference is that you have been talking about at the
beginning. I only see pointers, no references.

Jul 22 '05 #2

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:c7*************@news.t-online.com...
Ian Robertson wrote:
I am trying to write a function that takes a reference to an object as
its arguement. The object is created in another function and I am
trying to pass the object to another function from within the function
that created the object. I have cut out some code as I dont think
showing a for loop is helpful.

void TMP3_Main_Form::LoadMP3()
{
TMemoryStream *MP3Stream = new TMemoryStream();
MP3Stream->LoadFromFile(FileListBox1->Items->Strings[i].c_str());
GetMP3Data(&MP3Stream);
...
...
...
}

the object is created within this function. I load a file into the
stream. I then want to pass that stream to another function to perform
other tasks on the stream.

My GetMP3Data function accepts a pointer to a stream as its arguement.


But above, a pointer to a pointer to a TMemoryStream is passed to
GetMP3Data. MP3Stream is a pointer to TMemoryStream, and &MP3Stream is
the address of that pointer. You want to pass the pointer itself, so
leave out the &.


More likely is that the OP should not use new and instead leave in the &,
like this

void TMP3_Main_Form::LoadMP3()
{
TMemoryStream MP3Stream;
MP3Stream->LoadFromFile(FileListBox1->Items->Strings[i].c_str());
GetMP3Data(&MP3Stream);

Either way, if you use new you already have a pointer and so don't need &,
if you don't use new then you don't have a pointer and do need &.

The advantage of not using new is that you don't need to use delete and by
avoiding heap allocation it is likely to be more efficient.

john
Jul 22 '05 #3
>
void TMP3_Main_Form::LoadMP3()
{
TMemoryStream MP3Stream;
MP3Stream->LoadFromFile(FileListBox1->Items->Strings[i].c_str());
The above line should also change to

MP3Stream.LoadFromFile(FileListBox1->Items->Strings[i].c_str());
GetMP3Data(&MP3Stream);


Apologies.

john
Jul 22 '05 #4
John Harrison wrote:

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:c7*************@news.t-online.com...
Ian Robertson wrote:
> I am trying to write a function that takes a reference to an object
> as its arguement. The object is created in another function and I
> am trying to pass the object to another function from within the
> function that created the object. I have cut out some code as I
> dont think showing a for loop is helpful.
>
> void TMP3_Main_Form::LoadMP3()
> {
> TMemoryStream *MP3Stream = new TMemoryStream();
> MP3Stream->LoadFromFile(FileListBox1->Items->Strings[i].c_str());
> GetMP3Data(&MP3Stream);
> ...
> ...
> ...
> }
>
> the object is created within this function. I load a file into the
> stream. I then want to pass that stream to another function to
> perform other tasks on the stream.
>
> My GetMP3Data function accepts a pointer to a stream as its
> arguement.
But above, a pointer to a pointer to a TMemoryStream is passed to
GetMP3Data. MP3Stream is a pointer to TMemoryStream, and &MP3Stream
is the address of that pointer. You want to pass the pointer itself,
so leave out the &.


More likely is that the OP should not use new and instead leave in the
&, like this

void TMP3_Main_Form::LoadMP3()
{
TMemoryStream MP3Stream;
MP3Stream->LoadFromFile(FileListBox1->Items->Strings[i].c_str());
GetMP3Data(&MP3Stream);


I would have suggested that too, but I wasn't sure whether the OP wants
to use the stream after LoadMP3 (since he said he wanted to write to
the file later). But I guess you're right. After all, if he wanted to
use the stream later, it should have been a member variable, or at
least the function would have to return the pointer.
Either way, if you use new you already have a pointer and so don't
need &, if you don't use new then you don't have a pointer and do need
&.
Right. However, I'd suggest using a reference instead of a pointer as
parameter to GetMP3Data. Then, the & is again not needed.
The advantage of not using new is that you don't need to use delete
and by avoiding heap allocation it is likely to be more efficient.


Jul 22 '05 #5

"Rolf Magnus" <ra******@t-online.de> wrote in message news:c7*************@news.t-online.com...
I would have suggested that too, but I wasn't sure whether the OP wants
to use the stream after LoadMP3 (since he said he wanted to write to
the file later). But I guess you're right. After all, if he wanted to
use the stream later, it should have been a member variable, or at
least the function would have to return the pointer.
Either way, if you use new you already have a pointer and so don't
need &, if you don't use new then you don't have a pointer and do need
&.


TMemoryStream and TMemoryStream::LoadFromFile() look like
Borland VCL classes and functions. If so, the need to be created
on the heap and can't be instantiated without new. TMemoryStream
will fit nicely into an auto_ptr though.
Jul 22 '05 #6
Many thanks to John and Rolf. I am using borland cpp builder - which forces
the use of new with TMemoryStreams, and other VCL classes.
I have used your help to overcome my problem, and the performance gain over
having to reload the file from disk more than once is very noticable, since
my program goes through a batch of files at a time, only having to load a
file once makes a big difference when there are 30-100 files to process.
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:c7*************@news.t-online.com...
John Harrison wrote:

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:c7*************@news.t-online.com...
Ian Robertson wrote:

> I am trying to write a function that takes a reference to an object
> as its arguement. The object is created in another function and I
> am trying to pass the object to another function from within the
> function that created the object. I have cut out some code as I
> dont think showing a for loop is helpful.
>
> void TMP3_Main_Form::LoadMP3()
> {
> TMemoryStream *MP3Stream = new TMemoryStream();
> MP3Stream->LoadFromFile(FileListBox1->Items->Strings[i].c_str());
> GetMP3Data(&MP3Stream);
> ...
> ...
> ...
> }
>
> the object is created within this function. I load a file into the
> stream. I then want to pass that stream to another function to
> perform other tasks on the stream.
>
> My GetMP3Data function accepts a pointer to a stream as its
> arguement.

But above, a pointer to a pointer to a TMemoryStream is passed to
GetMP3Data. MP3Stream is a pointer to TMemoryStream, and &MP3Stream
is the address of that pointer. You want to pass the pointer itself,
so leave out the &.


More likely is that the OP should not use new and instead leave in the
&, like this

void TMP3_Main_Form::LoadMP3()
{
TMemoryStream MP3Stream;
MP3Stream->LoadFromFile(FileListBox1->Items->Strings[i].c_str());
GetMP3Data(&MP3Stream);


I would have suggested that too, but I wasn't sure whether the OP wants
to use the stream after LoadMP3 (since he said he wanted to write to
the file later). But I guess you're right. After all, if he wanted to
use the stream later, it should have been a member variable, or at
least the function would have to return the pointer.
Either way, if you use new you already have a pointer and so don't
need &, if you don't use new then you don't have a pointer and do need
&.


Right. However, I'd suggest using a reference instead of a pointer as
parameter to GetMP3Data. Then, the & is again not needed.
The advantage of not using new is that you don't need to use delete
and by avoiding heap allocation it is likely to be more efficient.

Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Sören | last post by:
Hi, I'd like advise on passing ownership of an iostream. The idea is that my factory class/function should open a file, read enough to detect file type (eg which soundfile format), then...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
6
by: csvka | last post by:
Hello, I wonder if I could pick your brains. I'm beginning to learn about C++. I have opened a file in my program and I want to read lines from it. I would like this to be done in a separate...
6
by: Arghknork | last post by:
Heres a dumb one. I know you can't actually pass reference types by value in C#, but I want to pass an object into a method and allow that method to manipulate a copy of that object, such that the...
8
by: Dennis Myrén | last post by:
I have these tiny classes, implementing an interface through which their method Render ( CosWriter writer ) ; is called. Given a specific context, there are potentially a lot of such objects,...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
6
by: MSDNAndi | last post by:
Hi, I get the following warning: "Possibly incorrect assignment to local 'oLockObject' which is the argument to a using or lock statement. The Dispose call or unlocking will happen on the...
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.