Solved

Directing a pointer value to a file.

Posted on 2003-11-11
2
294 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

688 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