473,320 Members | 1,799 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.

.mat file, malloc: Having some problems

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/help...al/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

May 24 '06 #1
6 2032
an****@gmail.com 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/help...al/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.
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.


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
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 24 '06 #2
Richard Heathfield wrote:
an****@gmail.com said:

Hi

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

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.

--
Eric Sosman
es*****@acm-dot-org.invalid
May 24 '06 #3
Eric Sosman said:
Richard Heathfield wrote:
an****@gmail.com said:

Hi

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

Better:

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


Better, maybe, but still not valid C.


Oops! Sorry, Eric - I did indeed miss that.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 24 '06 #4
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

May 25 '06 #5
Me wrote:

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/>

May 25 '06 #6
"Me" <an****@gmail.com> writes:
Thanks for answering,
Thanks for answering what?

Please read <http://cfaj.freeshell.org/google/>. See also
<http://www.clc-wiki.net/wiki/Introduction_to_comp.lang.c>,
particularly the section on Google Groups.
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 25 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Axel | last post by:
Hiho, here my Newbie question (Win32 GUI): I'm reading a file in binary mode to a char* named buffer. I used malloc(filesize) to make the needed space avaiable. The filedata in the buffer...
13
by: Richard | last post by:
vector<char*> m_Text; m_Text.resize(1); char* foo = "FOO"; char* bar = "BAR"; char* foobar = (char*)malloc(strlen(foo) + strlen(bar) + 1); if (foobar) { strcpy(foobar, foo); strcat(foobar,...
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
19
by: Hal Styli | last post by:
Hello, Can someone please help. I need to use a large array and I'm not sure when one should use (A) and when one should use (B) below:- #define MAX 10000000 (A) int x;
58
by: Jorge Peixoto de Morais Neto | last post by:
I was reading the code of FFmpeg and it seems that they use malloc just too much. The problems and dangers of malloc are widely known. Malloc also has some overhead (although I don't know what is...
6
by: Kinbote | last post by:
Hi, I'm trying to make a function that opens a file, reads it in line by line, puts each line into an malloc'd array, and returns the array. I suspect I'm going about it in an atypical fashion, as...
1
by: sfa | last post by:
I have a little problem... many solutions but maybe someone will give me a better hint. It's about the possibility of assigning the return value of a malloc assignment to a variable of array type....
71
by: desktop | last post by:
I have read in Bjarne Stroustrup that using malloc and free should be avoided in C++ because they deal with uninitialized memory and one should instead use new and delete. But why is that a...
5
by: paragon.john | last post by:
Hello all, I am trying to read a file into some allocated memory as part of a program and I am running into a problem. The program gets a segmentation fault during fread. I have previously...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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.