Solved

Directing a pointer value to a file.

Posted on 2003-11-11
2
272 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

777 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