473,466 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problems with VC++

2 New Member
I have the following code in VC++:

In TestView.h :

/***********************************/
......
.....
typedef struct t_test
{
int a, b;
struct t_test *next;
} Obj;

class CTestView : public CView
{
public :
Obj *OO;
Obj Make(int aa, int bb);
void Read();
void foo();
int N;
.......
}


In TestView.cpp the functions are as follows :

Obj CTestView::Make(int aa, int bb)
{
Obj *tmp;

tmp = (Obj *)malloc(sizeof(Obj));
tmp->a = aa;
tmp->b = bb;
}

void CTestView::Read()
{
Obj *new_o;

new_o = &Make(1, 2);
new_o->next = OO;
OO = new_o;

new_o = &Make(3, 4);
new_o->next = OO;
OO = new_o;
}

void CTestView::OnLButtonDown(.....) // or any other function
}
Read();
foo();
}

/************************************/

Thats is the essence of the code. My problem is :

In Read() the values for OO are correctly assigned. But after the function Read() is left and enters in foo() (OnLButtonDown) the values of OO are completely change (other values for a and b and next is not accessible).

What's wrong ? How can you correct ?

Thank you. The Curious 08
Oct 23 '08 #1
4 1244
r035198x
13,262 MVP
Please don't flood the forum with the same question. Read the posting guidelines before posting further.
Oct 23 '08 #2
Banfa
9,065 Recognized Expert Moderator Expert
CTestView::Make does not return anything.

I assume in your compiling code it returns something like *tmp. This is a mistake the compiler now creates a temporary object to return from the function (and the memory you allocated in malloc is leaked as it is never freed). You store a pointer to the temporary object in CTestView::Read but the object is temporary and is destroyed at the end of the assignment so then the pointer is pointing an invalid memory location.

You access this in foo and get undefined behaviour.

You should return Obj * from CTestView::Make and pass the pointer allocated by malloc back to the calling function.

And finally this is C++, you should prefer using new over malloc unless you have a valid reason that you can write down on paper why you need to use malloc (not being familiar with new is not a valid reason).
Oct 23 '08 #3
TheCurious08
2 New Member
CTestView::Make does not return anything.

I assume in your compiling code it returns something like *tmp. This is a mistake the compiler now creates a temporary object to return from the function (and the memory you allocated in malloc is leaked as it is never freed). You store a pointer to the temporary object in CTestView::Read but the object is temporary and is destroyed at the end of the assignment so then the pointer is pointing an invalid memory location.

You access this in foo and get undefined behaviour.

You should return Obj * from CTestView::Make and pass the pointer allocated by malloc back to the calling function.

And finally this is C++, you should prefer using new over malloc unless you have a valid reason that you can write down on paper why you need to use malloc (not being familiar with new is not a valid reason).


Hi

Thank for your reply. I have missed to add "return (*tmp);"in the message but in the program it appears. Bad habit with malloc(). I really don't know how to solve the problem.

The Curious
Oct 23 '08 #4
JosAH
11,448 Recognized Expert MVP
Thank for your reply. I have missed to add "return (*tmp);"in the message but in the program it appears. Bad habit with malloc(). I really don't know how to solve the problem.
A bad habit? It is plain wrong. Return the pointer to the malloc'd struct, keep it
somewhere and work with what the pointer points to and free() it afterwards.

kind regards,

Jos
Oct 23 '08 #5

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

Similar topics

6
by: Ryan D. Lucey | last post by:
ok I'm trying out a small program using operator overloading from the book C++ How To Program by Deitel Deitel. In their code they use the old style headers and the program compiles and runs as...
7
by: ree | last post by:
I need to use a function from a wininet library, it requires the use of const TCHAR. The problem is the value needs to be calculated, so at the end I have this string but I am having troubles...
1
by: manish | last post by:
Hi, I am a fresher in the programming field i.e although I have done programming at the basic level but at professional level I am very new and I am facing many problems. These probllems are...
2
by: Pep | last post by:
The following code from the Loki TypeManip.h header file is causing problems with MSVC version 6.0 template <unsigned i> struct TypeTag { struct Inner {char c;}; typedef Inner X;...
5
by: Hendrik Schober | last post by:
Hi, we just run into the problem, that "default" alignment in the project properies dialog seem to be different. We have a project that's a DLL, which is linked with a couple of LIBs. All are...
3
by: John Petersen | last post by:
Hi there I really hope someone had this problem before me. I have moved all our c++ projects from VC6 to VS.NET2003 and i have problems with nearly all dialogs containing either DateTimePicker...
6
by: Alex | last post by:
Hello people, We have a large application (about 5000 source files) consisting of multiple executables, DLLs and COM components written in C++. We develop and build it in Visual Studio 2003. ...
9
by: =?Utf-8?B?SG93YXJkIFNtaXRo?= | last post by:
I am using VC++ 6.0 (with SP5 installed). When using WinXP this is with SP2 installed. I am developing an instrumentation system comprising a set of networked PCs connected using TCP/IP TCP links....
0
by: chandan agarwal | last post by:
hi I have been given the task of building vc++ programs at the command line using make software. i have installed gnu make-3.81 version. i was able to build and run makefiles for turbo c++. but...
3
by: Russ | last post by:
I have a Web Service that was originally created with .NET VC 2003, and subsequently converted to the 2005 version. It works fine when built as a debug version, and run on the workstation it was...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.