Problem with malloc

T

Tom St Denis

Knady said:
Hi,
I have the following problem, I must to do my assignment, but I really do
not know how to use the malloc.
I need create a program that will be used to do some algebrical
computation on the matrix.
How I can create dynamical the matrices with the name in order to be able
to recall them.

step 1. "man malloc"
step 2. Pay attention in class when they teach you how to use pointers.
step 3. Don't ask for homework solutions in usenet.

Tom
 
N

Nils Petter Vaskinn

Do you know how to do it without dynamic memory (for a fixed sice
matrix?), start out with that and modify it to use dynamic memory.
step 1. "man malloc"
step 2. Pay attention in class when they teach you how to use pointers.
step 3. Don't ask for homework solutions in usenet.

Atleat he was honest about it beeing homework.

Step 4. Try to code something up yourself
Step 5. If you can't get it to work post the code that you have and
someone will probably point out your errors.
 
T

Tom St Denis

Joona said:
What makes you so sure Knady is working on a UNIX system?

Who cares if he isn't? Stupid windows users can trap themselves in
ignorance for all I care. As for me and my Gentoo'd box I'll go hug my
manual pages now... ;-)

Tom
 
K

Knady

Hi,
I have the following problem, I must to do my assignment, but I really do
not know how to use the malloc.
I need create a program that will be used to do some algebrical computation
on the matrix.
How I can create dynamical the matrices with the name in order to be able to
recall them.
Thx
 
M

Malcolm

Knady said:
How I can create dynamical the matrices with the name in order to > be able to recall them.

To create a matrix

double *mtx = malloc(width * height * sizeof(double));
if(mtx == 0)
/* out of memory */

To access an element

double element = mtx[y * width + x];

Once you've finished

free(mtx);
 
A

Al Bowers

Knady said:
Hi,
I have the following problem, I must to do my assignment, but I really do
not know how to use the malloc.
I need create a program that will be used to do some algebrical computation
on the matrix.
How I can create dynamical the matrices with the name in order to be able to
recall them.
Thx

The faq is a good source for help on multidimensional array dynamic
allocations. The link:

http://www.eskimo.com/~scs/C-faq/q6.16.html
 
C

Chris Fogelklou

Anyone know why engineers get such a bad reputation for being antisocial?
Instead of wasting my time berating you for being a "stupid windows user"
I'll answer the question...

It's actually quite simple... Malloc returns a pointer to a chunk of memory
of a certain size in bytes. You specify the number of bytes in the
parameter you pass to malloc. The prototype for malloc is
void *malloc(int size);

So, you get a void pointer in return... you need to cast it to something
useable, like your matrix, for instance...

typedef int my_matrix_t[20][20];//Hope I did this typedef right!

//Allocate memory for matrix and store in pointer. Cast so compiler doesn't
puke.
my_matrix_t *pmatrix = (my_matrix_t *)malloc(sizeof(my_matrix_t));
 
T

Tom St Denis

Chris said:
Anyone know why engineers get such a bad reputation for being antisocial?
Instead of wasting my time berating you for being a "stupid windows user"
I'll answer the question...

And even at that you failed.
It's actually quite simple... Malloc returns a pointer to a chunk of
memory
of a certain size in bytes. You specify the number of bytes in the
parameter you pass to malloc. The prototype for malloc is
void *malloc(int size);

That's not the prototype for malloc. It's

void *malloc(size_t size);
So, you get a void pointer in return... you need to cast it to something
useable, like your matrix, for instance...

Um, no. In C you need not cast void pointers.
typedef int my_matrix_t[20][20];//Hope I did this typedef right!

This isn't dynamic though [hint: read the OPs question] it's always 20x20
//Allocate memory for matrix and store in pointer. Cast so compiler
doesn't puke.
my_matrix_t *pmatrix = (my_matrix_t *)malloc(sizeof(my_matrix_t));

If your compiler pukes at that (without the cast) then you need a new
compiler.

Tom
 
J

Joona I Palaste

Who cares if he isn't? Stupid windows users can trap themselves in
ignorance for all I care. As for me and my Gentoo'd box I'll go hug my
manual pages now... ;-)

And what about stupid UNIX users? Oh, right. I forgot. To be able to so
much as boot up a UNIX machine you have to be both a hardware and a
software high wizard. How silly of me. =)
Anyway, I hope you are kidding about your attitude towards Windows
users. I don't like Microsoft any more than you do, but that's no
excuse to prejudice against their entire userbase.
 
T

Tom St Denis

Joona said:
And what about stupid UNIX users? Oh, right. I forgot. To be able to so
much as boot up a UNIX machine you have to be both a hardware and a
software high wizard. How silly of me. =)
Anyway, I hope you are kidding about your attitude towards Windows
users. I don't like Microsoft any more than you do, but that's no
excuse to prejudice against their entire userbase.

Perhaps but honestly there aren't many "good" reasons to use Windows. For
the most part Windows lowers that standards for users.

But even if the dude is a Windows user he could search MSDN for malloc ;-)
or search for manpages online, etc, etc...The fact that the guy doesn't
know what malloc does shows he has zero research skills.

I mean it's one thing to ask standards questions [e.g. "is this in ISO C",
etc] but honestly I'd expect any half competent developer to at least
memorize the vast majority of C functions and be able to research the rest.

Tom
 
C

Chris Fogelklou

Anyone know why engineers get such a bad reputation for being
antisocial?
And even at that you failed.

Hmmm... thanks.
That's not the prototype for malloc. It's

void *malloc(size_t size);

It's irrelevant to the answer, and normally size_t is an int anyway. I
could have said it's a size_t, but it might have created more problems than
it solved for the question-asker.
Um, no. In C you need not cast void pointers.

Um, yes you do, if you want truly portable code.
If your compiler pukes at that (without the cast) then you need a new
compiler.

Alright... Do you want to write one up for our embedded system? There have
two compilers, one based on GCC, and neither of them like it if you equate
two pointer types to each other without casting.
This isn't dynamic though [hint: read the OPs question] it's always 20x20
This is true, but I got the impression that he had no idea how to use
malloc. I was trying to solve his confusion relating to malloc itself.
Hopefully with this basic understanding, he can solve the dynamic part of
the problem himself. (I should have also mentioned how to use free, but
someone else did a pretty good job of that.)

The link provided by Al Bowers is an excellent source for info on the
dynamic memory problem.

Cheers,

Chris
 
M

Martin Dickopp

Chris Fogelklou said:
It's irrelevant to the answer, and normally size_t is an int anyway.

No. According to 7.17#2, `size_t' is required to be an unsigned integer
type, so it cannot be `int'.
Um, yes you do, if you want truly portable code.

Chapter and verse, please?

Martin
 
T

Tom St Denis

Chris said:
Hmmm... thanks.

You are most certainly welcome.
It's irrelevant to the answer, and normally size_t is an int anyway. I
could have said it's a size_t, but it might have created more problems
than it solved for the question-asker.

Um first off, no "size_t" isn't int. I'd think if anything it would be
"unsigned" not just an int. Anyways the type is size_t. Why bother
replying with "seemingly useful facts" if you're just going to mess with
them?
Um, yes you do, if you want truly portable code.

No, no you don't. The C standards [C90, C99] don't require you to cast a
void pointer to any other pointer type. If you're code is supposed to work
in C++ as well then maybe. However, putting the cast in can mask a more
serious problem that you forget to include stdlib.h.
Alright... Do you want to write one up for our embedded system? There
have two compilers, one based on GCC, and neither of them like it if you
equate two pointer types to each other without casting.

Um dude I've coded for embedded platforms before [from 8085, AVR, 68xx
series to the ARM based boards] and in all cases I've never casted a void
pointer to something.

In particular for the ARM boards you can use GCC and it will accept the
non-cast code just fine.

Me thinks you might want to preface your responses with "I used casts cuz my
non-standard compiler asked me to". This is afterall comp.lang.c and we're
not talking about "vendor lang c"
This isn't dynamic though [hint: read the OPs question] it's always 20x20
This is true, but I got the impression that he had no idea how to use
malloc. I was trying to solve his confusion relating to malloc itself.

Point him to the manual pages. Why bother answering his question with code?
How the hell do you think all these "coders" get out there? They take an
ounce of knowledge and spread it so thin to get a job....

If the dude can't learn from a manual page [or googling on the net] then too
F'ing bad.

Tom
 
C

Chris Fogelklou

whatever...
Martin Dickopp said:
No. According to 7.17#2, `size_t' is required to be an unsigned integer
type, so it cannot be `int'.


Chapter and verse, please?

Martin


--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
 
C

Chris Fogelklou

I used casts cuz my non-standard compiler asked me to :)

Anyway... I regret answering now. Please refer to man malloc for more
assistance.

Done.

Tom St Denis said:
Chris said:
Hmmm... thanks.

You are most certainly welcome.
It's irrelevant to the answer, and normally size_t is an int anyway. I
could have said it's a size_t, but it might have created more problems
than it solved for the question-asker.

Um first off, no "size_t" isn't int. I'd think if anything it would be
"unsigned" not just an int. Anyways the type is size_t. Why bother
replying with "seemingly useful facts" if you're just going to mess with
them?
Um, yes you do, if you want truly portable code.

No, no you don't. The C standards [C90, C99] don't require you to cast a
void pointer to any other pointer type. If you're code is supposed to work
in C++ as well then maybe. However, putting the cast in can mask a more
serious problem that you forget to include stdlib.h.
Alright... Do you want to write one up for our embedded system? There
have two compilers, one based on GCC, and neither of them like it if you
equate two pointer types to each other without casting.

Um dude I've coded for embedded platforms before [from 8085, AVR, 68xx
series to the ARM based boards] and in all cases I've never casted a void
pointer to something.

In particular for the ARM boards you can use GCC and it will accept the
non-cast code just fine.

Me thinks you might want to preface your responses with "I used casts cuz my
non-standard compiler asked me to". This is afterall comp.lang.c and we're
not talking about "vendor lang c"
This isn't dynamic though [hint: read the OPs question] it's always
20x20
This is true, but I got the impression that he had no idea how to use
malloc. I was trying to solve his confusion relating to malloc itself.

Point him to the manual pages. Why bother answering his question with code?
How the hell do you think all these "coders" get out there? They take an
ounce of knowledge and spread it so thin to get a job....

If the dude can't learn from a manual page [or googling on the net] then too
F'ing bad.

Tom
 
T

Thomas Stegen

Chris said:
It's irrelevant to the answer, and normally size_t is an int anyway.

size_t can be many things. But it is never int. Never.
I
could have said it's a size_t, but it might have created more problems than
it solved for the question-asker.

You could have said that size_t is just an unsigned integer.
Um, yes you do, if you want truly portable code.

Let me quote Tom St Denis on this one:

"Um, no. In C you need not cast void pointers." Before you even attempt
to contradict me go look up the vast amount of discussion on this
topic right here in this newsgroup. If you can come up with an argument
not already proffered please do so.

Alright... Do you want to write one up for our embedded system?

If your compiler requires a cast from pointer to void to a pointer to
any other object type then it is broken. Why, because it will not
accept all strictly conforming C programs (for stand alone
implementations).
There have
two compilers, one based on GCC, and neither of them like it if you equate
two pointer types to each other without casting.

In the following T and S can not be void.

If you have a pointer to S and a pointer to T then you need the cast
to assign one to the other AND you need to know that they are
compatable. A pointer to void can be implicitly converted to any
pointer type, no cast required.

For more information read section 6.3.2.3 of the C99 standard.
 
C

Chris Fogelklou

Hi Thomas,

Yes, I was wrong about the int. It should definitely an unsigned int. I
wrote my response very quickly and obviously didn't pull up my handy C
library help file when I did it. I just took offense to the "higher than
thou" attitude to a question that was out-and-out stated to be a homework
assignment by a beginner.

I was trying to express how to use malloc. Thinking that malloc takes an
int would probably not affect the application code in any way (and yes I
know that ints have a smaller maximum absolute value do to their signedness,
but think about what would really happen... hint... compiler casting from
positive int to unsigned int... before you catch me on this one, although I
do expect someone to respond in any case.)

As long as he isn't writing his own malloc function, don't you think I still
might have provided some handy, if perhaps a bit imprecise, advice?

That said, apparently the embedded compilers I've been using are broken. I
have a cross platform environment going here, and when building for win32,
casting a void * to anything else is OK. But when I move to the embedded
version, it breaks. I am referring only to my firsthand experience.

Cheers,
Chris

PS. Next time I write anything in this NG, I'll ensure that I have a
complete bibliography appended to the bottom and that I triple check every
fact stated with the latest ANSI standards and their addendums.
 
M

Martin Dickopp

Chris Fogelklou said:
Yes, I was wrong about the int. It should definitely an unsigned int.

Actually, the standard only requires `size_t' to be an unsigned integer
type. Sometimes it is `unsigned int', sometimes it is another unsigned
integer type.
That said, apparently the embedded compilers I've been using are broken. I
have a cross platform environment going here, and when building for win32,
casting a void * to anything else is OK. But when I move to the embedded
version, it breaks. I am referring only to my firsthand experience.

When `malloc' works on some systems and doesn't on others, that is often
an indication of incorrect code. A typical error of this kind is to use
`malloc' without a proper declaration in scope (which one usually gets
by including <stdlib.h>). Usually, the compiler would warn you if use
`malloc' and haven't included <stdlib.h>, but by casting the return
value, the warning is suppressed. Despite the lack of a warning, the
code is still incorrect and will break on some systems.
PS. Next time I write anything in this NG, I'll ensure that I have a
complete bibliography appended to the bottom and that I triple check
every fact stated with the latest ANSI standards and their addendums.

The latter is certainly a good idea. That's what I do unless I'm very
sure. (I still make mistakes sometimes, but fortunately, someone else
will reply and point them out very quickly, so it's not a problem.)

Martin
 

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

Similar Threads

MALLOC problem 25
Malloc question 9
Problem with code 6
Problem with KMKfw libraries 1
Java matrix problem 3
array-size/malloc limit and strlen() failure 26
Help with if and variables 1
malloc rare error (malloc bug??) 18

Members online

No members online now.

Forum statistics

Threads
474,142
Messages
2,570,818
Members
47,362
Latest member
eitamoro

Latest Threads

Top