Solved

Directing a pointer value to a file.

Posted on 2003-11-11
2
288 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

740 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