[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

malloc C# Equal?

Posted on 2003-11-18
9
Medium Priority
?
5,056 Views
Last Modified: 2007-12-19
I've got a C++ 6.0 Statement...
TOCRResults = (TOCRRESULTS *)malloc(ResultsInf);

I'm needing to (A) figure out what in the world this is even doing ..I believe it is allocating the amount of bytes to TOCRResults that ResultsInf needs...
and ..(B) what would be the C# Equivalent? Thanks for your time.
0
Comment
Question by:derekthornton
[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
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 1

Accepted Solution

by:
stone123 earned 600 total points
ID: 9773745
Answer to (A):

"TOCRRESULTS" seems to be a user-defined class or type.

then "TOCRResults" is simply a pointer to an instance of that class or type.

the "malloc" statement is to allocate certain amount of bytes in the count of "ResultsInf". These bytes are the memory space to hold/contain the instance of "TOCRRESULTS". Most likely, the value of "ResultsInf" can be determined by using "sizeof(TOCRRESULTS)".

In result, the "malloc" is to create an instance of "TOCRRESULTS",  to which the pointer "TOCRResults" points.

Notes: the allocation takes place at the heap of memory. you must call "free" function after you done using it.


Answer to (B):

C# is 100% pure OO. And you are not encouraged to use pointers in C#. so you just define a class "TOCRRESULTS", and create an instance like you do normally. I assume you know what to do at this point.
0
 
LVL 22

Assisted Solution

by:_TAD_
_TAD_ earned 400 total points
ID: 9773748


You don't need to worry about allocating memory in C#.  C# has its own garbage collection and monitors memory usage for you.

However, in the case of your example, it appears as if you are allocating a specific address in memory to store pointer information.

While C# does support pointers, I personally believe that pointers cause more problems than they solve.  Pointers and pointer arithmetic is fantasic and VERY fast if used properly, but can cause some serious memory leaks if used inproperly.

Having said all that, you do not need to worry about allocating a specific memory block in C# like you had to in C++.  The only thing you need to worry about is the scope of the object.  Which functions/classes will need access rights to the data in your object.

Pointers allowed for programmers to ignore scope if they needed to and could create global variables.  If you need a global var in C# then use the static reserved word.

0
 
LVL 22

Expert Comment

by:_TAD_
ID: 9773768


Bummer!  Beaten by mere seconds (with more or less the same repsponse, although stone's is a bit more concise).

oh well...
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Author Comment

by:derekthornton
ID: 9773787
I know C# Doesn't like pointers, and frankly, neither do I, however the first post was correct, it is a commercial library I have purchased and am trying to port to C#. It is an OCR Engine. This TOCRResults structure is supposed to hold the Results of an OCR Job, but it's just plain not working and I'm getting VERY frustrated. I've already gotten help with other parts of this project, and am starting to wonder if a full port to C# is even possible....
0
 
LVL 2

Author Comment

by:derekthornton
ID: 9773803
Question Answered - Problem still there, but I did get the answer to the malloc question. If anyone else wants to take a crack at this feel free to ask, It's driving me crazy.
0
 
LVL 1

Expert Comment

by:stone123
ID: 9774508
Thanks for the points, derekthornton.

When you say OCR, do you mean Optical Character Recognition? Just for curiosity.

why do you want to port it to C#? Is there any particular reason? Personally, I suggest you just use your Visual Studio.NET to create a Managed C++ class library project. And you shouldn't make much change to port it to Managed C++ project, which is also based and runs on .NET framework.

Then there are a few ways to easily call functions and create instances of library classes from a separate C# program. If you are interested, i will write to you more.

Good luck
0
 
LVL 2

Author Comment

by:derekthornton
ID: 9774529
I want to write the whole thing in C#. I don't own Visual Studio .NET either, just the individual "Standard" Products. Furthermore, the code I have is pretty unflexible, and I don't like the style the library I bought was coded in. I cannot make it a managed C++ Class Library, because I cannot access the actual 'Source' just the API, so I am stuck with what I have, and calling a C++ .dll from C# is just not something I like to do, it is Very sloppy, in my opinion, and it just never works how I like it to, so I want to rewrite it entirely. And yes, It is an Optical Character Recognition program. I can send you the Solution if you want to see it.
0
 
LVL 2

Author Comment

by:derekthornton
ID: 9774593
But if I can just make it an easy to use .dll file, that would work. The problem is that it never wants to load correctly in C# when I try to make a .dll in C++.
0
 
LVL 4

Expert Comment

by:meet_zorrer
ID: 9808690
well!
Windows system 32 bit dlls or COM dlls or any kind of dlls, all can be called in .NET framework's any language, just use the interopabilities of the .Net framework, Search in the MSDN

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

649 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