Solved

Directing a pointer value to a file.

Posted on 2003-11-11
2
280 Views
Last Modified: 2010-04-01
Hi,

I have a pointer variable which I declared as U8 * buffer.

I am populating this variable with data and now I want to transfer the contents to a file.
When I tried to do that, I am getting error saying:

"This Program has performed an Illegal operation and needs to be shut down".

I have my code somethig like this:

U8 *buffer;
// Code for populating buffer

char *byteVals = new char[dmaBytes];
//dmaBytes is of the order of 100,000.

for(int a = 0; a < dmaBytes ; a++)
{
      outfile<<(char)*(buffer + a); // I am getting error in this line.
}

In my program I had to use an array according to parameters supplied at run time and so I cannot use a predefined array length and I thought pointers can solve my problem and I shifted to pointers.

I am new to C++.

I shall be very happy if someone can help me in this.

Thanks,
Dinkar.
0
Comment
Question by:dinkarece
2 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 9723103
As far as I can tell from your code, "buffer" is an uninitialized pointer.  Always a BAD thing and certainly should cause your program to crash...
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 40 total points
ID: 9723333
I suppose U8 is defined as  unsigned char  (8 bits from 0 .. 255).

Then, your code might be changed like this:

char *byteVals = new char[dmaBytes];
//dmaBytes is of the order of 100,000.

U8 *buffer = (U8*)byteVals ;
// Code for populating buffer

for(int a = 0; a < dmaBytes ; a++)
{
     outfile<<(char)(buffer[a]);    
    // Much more simple is:  outfile << byteVals[a];
    // You will not get an error, but the data isn't initialized and filled or did I miss some code?
}

Regards, Alex

0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question