473,320 Members | 1,948 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

memory problem

93
Hi,
I want to intialise values in my program by reading from a binary file. I thought of doing it in the fllowing way. But it produces a assertion failure when the program returns. I traced the error to the destructor of the Test structure. When I remove z from the structure it works fine. Can anyone tell me what's the problem here? And is there a better way of doing my initializations from a binary? Thanx.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include "stdafx.h"
  3. #include <iostream>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. struct Test
  9. {
  10.     int x;
  11.     char y;
  12.     char * z;
  13.  
  14.     Test() {z = new char[4]; z = "abc";}
  15.     ~Test() {delete [] z;}
  16. };
  17.  
  18. int main(int argc, char* argv[])
  19. {
  20.     ofstream out;
  21.     out.open("l.tst", ios::out | ios::binary);
  22.  
  23.     char * buffer = new char[256];
  24.  
  25.     Test t;
  26.     t.x = 80;
  27.     t.y = 's';
  28.  
  29.     memcpy(buffer, &t, sizeof(Test));
  30.  
  31.     out.write(buffer, sizeof(Test));
  32.     out.close();
  33.  
  34.     ifstream in;
  35.     in.open("l.tst", ios::in | ios::binary);
  36.  
  37.     char * inbuf = new char[256];
  38.  
  39.     in.read(inbuf, sizeof(Test));
  40.  
  41.     Test * t2 = new Test;
  42.  
  43.     memcpy(t2, inbuf, sizeof(Test));
  44.     in.close();
  45.  
  46.     //Use t2 for something.
  47.         cout << t2->x << " " << t2->y << " " << t2->z << endl; 
  48.  
  49.     return 0;
  50. }
  51.  
  52.  
  53.  
  54.  
Jun 2 '07 #1
1 918
Savage
1,764 Expert 1GB
This is not correct:

Test() {z = new char[4]; z = "abc";}

this is what causes a problem,change it to strcpy(z,"abc") and it shall work.

:D

Savage
Jun 2 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Andreas Suurkuusk | last post by:
Hi, I just noticed your post in the "C# memory problem: no end for our problem?" thread. In the post you implied that I do not how the garbage collector works and that I mislead people. Since...
4
by: Amadeus | last post by:
Hello Everybody! I have a problem with MySQL servers running RedHat 9 (smp kernel 2.4.20) on Intel and MySQL server 4.0.14 (problem also appears on binary distr 4.0.15 and on 4.0.15 I bilt myself...
32
by: John | last post by:
Hi all: When I run my code, I find that the memory that the code uses keeps increasing. I have a PC with 2G RAM running Debian linux. The code consumes 1.5G memory by the time it finishes...
17
by: José Joye | last post by:
Hi, I have implemented a Service that is responsible for getting messages from a MS MQ located on a remote machine. I'm getting memory leak from time to time (???). In some situation, it is...
16
by: JCauble | last post by:
We have a large Asp.net application that is currently crashing our production servers. What we are seeing is the aspnet_wp eat up a bunch of memory and then stop unexpectedly. Does not recycle. ...
7
by: Salvador | last post by:
Hi, I am using WMI to gather information about different computers (using win2K and win 2K3), checking common classes and also WMI load balance. My application runs every 1 minute and reports...
9
by: Bruno Barberi Gnecco | last post by:
I'm using PHP to run a CLI application. It's a script run by cron that parses some HTML files (with DOM XML), and I ended up using PHP to integrate with the rest of the code that already runs the...
9
by: jeungster | last post by:
Hello, I'm trying to track down a memory issue with a C++ application that I'm working on: In a nutshell, the resident memory usage of my program continues to grow as the program runs. It...
17
by: frederic.pica | last post by:
Greets, I've some troubles getting my memory freed by python, how can I force it to release the memory ? I've tried del and gc.collect() with no success. Here is a code sample, parsing an XML...
27
by: George2 | last post by:
Hello everyone, Should I delete memory pointed by pointer a if there is bad_alloc when allocating memory in memory pointed by pointer b? I am not sure whether there will be memory leak if I do...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.