Solved

Directing a pointer value to a file.

Posted on 2003-11-11
2
253 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now