-5

this is the code which i am trying to run.The compiler shows some warning that the variable 'p' may be possible to be used uninitialized. And ..on running it has a problem ..i will show..below. please correct me and explain . Thank you. execution of code * code that error is that..the first two elements of the character array being output are somewhat messed up!!!*

#include<bits/stdc++.h>
using namespace std;
class file{
public:int a;
char* name;
public:
file(int x,char* b):a(x){name=b;}
void printfile(){cout<<a<<" "<<name<<endl;}
};
int main(){
char *p;
int x=10;
cout<<"enter a name"<<endl;
cin>>p;
file k(x,p);
ofstream f("file",ios::out|ios::binary);
f.write((char*)&k,sizeof(class file));
f.close();
ifstream of("file",ios::in|ios::binary);
file o(0,'\0');
of.read((char*)&o,sizeof(class file));
o.printfile();
of.close();
return 1;
}
  • 1
    Please include your code in the question now a link to pictures of the code. – Dave Dec 06 '17 at 19:30
  • 1
    Welcome to StackOverflow. Please read [ask] and provide a [mcve]. – Barry Dec 06 '17 at 19:30
  • Format your post correctly rather than including the code in imgur – BooleanCheese Dec 06 '17 at 19:31
  • 1
    But after you format your post correctly as @BooleanCheese said, please format your actual code correctly. Please don't format it like that. Please. – Jose Fernando Lopez Fernandez Dec 06 '17 at 19:32
  • I have entered the code as required. Please review the question. Thank you. – naksmoester luetarpa Dec 06 '17 at 19:41
  • p is a pointer to a char. You have not allocated space for the char but you attempt input a value into it. I assume you are not permitted to use a `std::string`. – drescherjm Dec 06 '17 at 19:43
  • `sizeof(class file))` we leave out the class here. Note that sizeof() is a compile time constant. and name is a pointer to a character array that is outside the class. Your binary read / write will not store the data that name points to. – drescherjm Dec 06 '17 at 19:45
  • yep! that worked @drescherjim .!! Thanks !! i FEEL like an ass right now ! – naksmoester luetarpa Dec 06 '17 at 19:47
  • but it's working fine with the keyword class being added there . :-/ – naksmoester luetarpa Dec 06 '17 at 19:48
  • If it is working that is bad luck. It should not work. Sometimes undefined behavior appears to work. If you separated this into 2 programs it will fail. What I mean by that is you are not saving the text to the binary file at all. – drescherjm Dec 06 '17 at 19:49
  • @naksmoesterluetarpa Here's [why you shouldn't use raw pointers with c++ at all](https://stackoverflow.com/questions/46991224/are-there-any-valid-use-cases-to-use-new-and-delete-raw-pointers-or-c-style-arr) – user0042 Dec 06 '17 at 20:00

2 Answers2

0

You have p as some pointer. Who is going to allocate the memory that pointer points to?
In C it is almost always the responsibility of the caller to allocate any buffers before the call.
If you don't want to, then use a std::string instead.

Gem Taylor
  • 5,381
  • 1
  • 9
  • 27
0

First of all I would advise you to read some basic manual about the c++ pointers and memory handling so you will better understand the source of the problem.

There are two major problems in your code. The first is that you are creating a pointer which is not connected to any allocated memory. In simple words you are asking to access a memory address without asking the system to reserve it for you.

Additionally, in that memory location there can already be stored any bit configuration. The initialization of a variable is the task of giving a chunk of memory some data which have a meaningful interpretation. I'm not even sure what the in-stream operator of char* is supposed to do in this particular case. He is probably appending your characters after the last one which is not a 0 or an end of line.

Triskeldeian
  • 590
  • 3
  • 18