.mat file, malloc: Having some problems

A

an0011

Hi

static double *var = (double*) malloc(100000*sizeof(double));

is used among other memory allocations to store data that comes from a
..mat file

The declarations are done on the main .cpp file and then used by other
..cpp files one
of those files is similar to the following:
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/f32276.html

The code is ansi c, and .cpp files are used because MS VC is used to
compile the hole program.
From one time to the other the program began crashing and it was
noticed that removing
some static double global declarations and mallocs help to run it with
out crashing.

I´m not sure what is the problem, if memory reached limits or if my
programming is so bad
that I can´t allocate more memory space, or many globals are declared.

The questions are:
1.- Is the other way to access globaly the data of the .mat file using
ansi C and many other
..cpp files included in compilation?

2.- Declaring

static mxarray pa1
var[x] = (mxGetPr(pa1))[x];

where main() is declared would allow having global access to the .mat
data without the need
of storing every thing on allocated memory space?

As a beginner programming I'm sorry if the questions are very silly but
I don't know about a real good book where one can learn to give
structure to ansi c programms nor I didn't find good information on the
internet.

Best Regards and Thank you
 
R

Richard Heathfield

(e-mail address removed) said:
Hi

static double *var = (double*) malloc(100000*sizeof(double));

Better:

static double *var = malloc(100000 * sizeof *var);
is used among other memory allocations to store data that comes from a
.mat file

The declarations are done on the main .cpp file

C++ questions are off-topic in comp.lang.c - but you mention this later, so
keep reading.
and then used by other
.cpp files one
of those files is similar to the following:
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/f32276.html

The code is ansi c, and .cpp files are used because MS VC is used to
compile the hole program.

Then the code is not being compiled under C rules. MS VC is perfectly
capable of dealing with .c files. If it tries to save a file as a .cpp
file, you should give it a good kicking. Whilst it's busy picking up its
teeth, insist that it saves the file as a .c file. This is not difficult.
Just overtype its suggested foo.cpp with foo.c and you're done.
noticed that removing
some static double global declarations and mallocs help to run it with
out crashing.

I´m not sure what is the problem, if memory reached limits or if my
programming is so bad
that I can´t allocate more memory space, or many globals are declared.

It's easy to get carried away with memory, especially when you are defining
multiple-dimensioned arrays. You just do a quick double[17][282][4][915]
and don't immediately realise that you just asked for about 140MB! (On a
system with 8-octet doubles, it's 140368320 bytes.)

Dynamic allocation works better here, but you should *always* check that the
allocation succeeded, and take appropriate action if it didn't.

if(var != NULL)
{
all is well
}
else
{
all is most certainly not well
}
 
E

Eric Sosman

Richard said:
(e-mail address removed) said:




Better:

static double *var = malloc(100000 * sizeof *var);

Better, maybe, but still not valid C. The initializer
for a `static' variable must be a constant, and a function
result is not a constant.

Methinks the O.P.'s efforts to get his C++ compiler to
operate in "C mode" have not been entirely successful. It
would be a good idea to move firmly into either the C or the
C++ camp before trying to do much further diagnosis.
 
M

Me

Hi

Thanks for answering,

So, it is possible use malloc with as much memory the system has? and
it is possible too to declare as much static global variables as one
wish?

Regards
 
C

CBFalconer

Me said:
So, it is possible use malloc with as much memory the system has?
and it is possible too to declare as much static global variables
as one wish?

This is meaningless. In general on usenet you should realize that
readers may very well not have convenient access to previous
articles in a thread. That means that your reply articles should
include adequate context, so that they stand by themselves. Google
is NOT usenet, it is only a very poor interface to the real usenet
system. To include proper context when using google, see my sig.
below. Please be sure to read the referenced URLs.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
K

Keith Thompson

Me said:
Thanks for answering,

Thanks for answering what?

Please read <http://cfaj.freeshell.org/google/>. See also
So, it is possible use malloc with as much memory the system has? and
it is possible too to declare as much static global variables as one
wish?

Each system will impose some limits on how much memory you can
allocate. The standard doesn't say much about what those limits might
be.

If malloc() fails, it will report its failure by returning a null
pointer (you should always check for this). If you've declared to
many static variables, your program will probably fail on startup.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top