473,378 Members | 1,492 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,378 software developers and data experts.

Problem with malloc

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
Nov 14 '05
66 3165
Joona I Palaste wrote:
Tom St Denis <to*@securescience.net> scribbled the following:
Joona I Palaste wrote:
.... snip ...

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... ;-)


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.

^^^^^^^^^^^^^^^^^^^^

You, and Knady, should be aware that Tom is a pubescent
opinionated Ottawa teen-ager with all the diplomatic skills of Dan
Pop and very little of Dans knowledge nor Dans capability at the
English language. Toms invective level appears to be improving,
so he escapes replonking since he changed his 'from' description.

BTW, the above phrase could be either "for prejudice against" or
"to prejudge". As it stands it is highly confusing.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #51
Chris Fogelklou wrote:
.... snip ...
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.


Um, yes you do, if you want truly portable code.
//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.


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.


You are misusing it, especially if one is based on gcc. You NEVER
should cast a void pointer being assigned to another pointer
type. Errors from such statements usually indicate a failure to
have proper prototypes for malloc, caused by failure to #include
stdlib.h, or failure to use a C compiler, often caused by naming
source files with a .C or .cpp extension. The best statement for
the malloc call above is:

my_matrix_t *pmatrix = malloc(sizeof *pmatrix);

Casts are automatically suspect code. (double)95.34152718% of
them are unnecessary and serve only to conceal errors.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #52
Chris Fogelklou wrote:
.... snip ...
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.


Um, yes you do, if you want truly portable code.
//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.


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.


You are misusing it, especially if one is based on gcc. You NEVER
should cast a void pointer being assigned to another pointer
type. Errors from such statements usually indicate a failure to
have proper prototypes for malloc, caused by failure to #include
stdlib.h, or failure to use a C compiler, often caused by naming
source files with a .C or .cpp extension. The best statement for
the malloc call above is:

my_matrix_t *pmatrix = malloc(sizeof *pmatrix);

Casts are automatically suspect code. (double)95.34152718% of
them are unnecessary and serve only to conceal errors.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #53
Chris Fogelklou wrote:
.... snip ...
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.


A wise decision around here. Getting a copy of the C standard (or
N869) is also advisable. Otherwise you will soon die the death of
a thousand pecks. Remember, the vultures are circling :-)

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #54
Chris Fogelklou wrote:
.... snip ...
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.


A wise decision around here. Getting a copy of the C standard (or
N869) is also advisable. Otherwise you will soon die the death of
a thousand pecks. Remember, the vultures are circling :-)

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #55
In 'comp.lang.c', "Chris Fogelklou" <ch*************@comhem.se> wrote:
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.


You've got it. This NG has a reputation of excellence. Trust me, you'll learn
*a lot* by reading it.

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=cpp
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #56
In 'comp.lang.c', "Chris Fogelklou" <ch*************@comhem.se> wrote:
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.


You've got it. This NG has a reputation of excellence. Trust me, you'll learn
*a lot* by reading it.

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=cpp
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #57
CBFalconer wrote:
You, and Knady, should be aware that Tom is a pubescent
opinionated Ottawa teen-ager with all the diplomatic skills of Dan
Pop and very little of Dans knowledge nor Dans capability at the
English language.


This is, quite simply, not true.

IIRC, Tom is 21 years old.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #58
CBFalconer wrote:
You, and Knady, should be aware that Tom is a pubescent
opinionated Ottawa teen-ager with all the diplomatic skills of Dan
Pop and very little of Dans knowledge nor Dans capability at the
English language.


This is, quite simply, not true.

IIRC, Tom is 21 years old.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #59
"Chris Fogelklou" <ch*************@comhem.se> writes:
Hi Richard,
It doesn't. If the code works with the cast, it'll work without it, too.


I concede... I just checked and I don't actually have a compiler now that
craps out without the cast. I could swear that I have used one that would
fail without the cast, so I have just always casted... and never turned
back.


Turn back now. 8-)}

Perhaps you were using a C++ compiler. C++, unlike C, doesn't
implicitly convert void* to other pointer types, and C++ is close
enough to being a superset of C that you can probably compile most C
code using a C++ compiler with only a few tweaks.

But it almost always makes more sense to compile your C code with a C
compiler, and your C++ code with a C++ compiler. (Most C++ compilers
either ship with a C compiler, or can be invoked as a C compiler.)
<OT>It seldom makes sense to use malloc() in C++; use the "new"
operator instead.</OT>

In rare circumstances, it can make sense to write code that's valid C
and valid C++; such code will probably need to cast the result of
malloc(). If you don't already know why you'd need to do this, you
almost certainly shouldn't try.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #60
"Chris Fogelklou" <ch*************@comhem.se> writes:
Hi Richard,
It doesn't. If the code works with the cast, it'll work without it, too.


I concede... I just checked and I don't actually have a compiler now that
craps out without the cast. I could swear that I have used one that would
fail without the cast, so I have just always casted... and never turned
back.


Turn back now. 8-)}

Perhaps you were using a C++ compiler. C++, unlike C, doesn't
implicitly convert void* to other pointer types, and C++ is close
enough to being a superset of C that you can probably compile most C
code using a C++ compiler with only a few tweaks.

But it almost always makes more sense to compile your C code with a C
compiler, and your C++ code with a C++ compiler. (Most C++ compilers
either ship with a C compiler, or can be invoked as a C compiler.)
<OT>It seldom makes sense to use malloc() in C++; use the "new"
operator instead.</OT>

In rare circumstances, it can make sense to write code that's valid C
and valid C++; such code will probably need to cast the result of
malloc(). If you don't already know why you'd need to do this, you
almost certainly shouldn't try.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #61
"Chris Fogelklou" <ch*************@comhem.se> writes:
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.


The argument to malloc is of type size_t, the same type as the result
of the sizeof operator. size_t is guaranteed to be an unsigned
integer type, but it needn't be unsigned int; in many implementations,
it's unsigned long.

That's exactly why "size_t" exists, so you don't have to know which of
the predefined unsigned integer types to use for a given implementation.

One more thing: please don't top-post. The convention here in
comp.lang.c is for your response to *follow* any quoted text from the
previous article, which should be limited to what's directly relevant
to your response (as I've done here). It may not seem like a big
deal, but it really does make it easier for your readers.

A: Because it makes your article more difficult to read.
Q: Why is top-posting bad?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #62
"Chris Fogelklou" <ch*************@comhem.se> writes:
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.


The argument to malloc is of type size_t, the same type as the result
of the sizeof operator. size_t is guaranteed to be an unsigned
integer type, but it needn't be unsigned int; in many implementations,
it's unsigned long.

That's exactly why "size_t" exists, so you don't have to know which of
the predefined unsigned integer types to use for a given implementation.

One more thing: please don't top-post. The convention here in
comp.lang.c is for your response to *follow* any quoted text from the
previous article, which should be limited to what's directly relevant
to your response (as I've done here). It may not seem like a big
deal, but it really does make it easier for your readers.

A: Because it makes your article more difficult to read.
Q: Why is top-posting bad?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #63
In <c4*********@news1.newsguy.com> Chris Torek <no****@torek.net> writes:
A lot of embedded-system "C compilers" have, historically, been
quite awful. The widespread use of GCC has, I think, succeeded at
"raising the bar" here: if you have a lousy embedded-system compiler
suite for target CPU "X", you need only to port the GNU Compiler
Collection to target X, and you now have a half-decent compiler
suite. (Not that porting GCC is easy, but it is a lot easier than
writing an entire compiler from scratch.)


I'm not sure how feasible this approach is when target X is a small
microprocessor or microcontroller. IIRC, GCC was designed with the 68000
CPU as the minimal target processor.

Furthermore, many embedded control CPUs have specific features that need
C extensions in order to be used, e.g. the bit addressing of the 8051
family or the on chip vs extern RAM or ROM.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #64
In <c4*********@news1.newsguy.com> Chris Torek <no****@torek.net> writes:
A lot of embedded-system "C compilers" have, historically, been
quite awful. The widespread use of GCC has, I think, succeeded at
"raising the bar" here: if you have a lousy embedded-system compiler
suite for target CPU "X", you need only to port the GNU Compiler
Collection to target X, and you now have a half-decent compiler
suite. (Not that porting GCC is easy, but it is a lot easier than
writing an entire compiler from scratch.)


I'm not sure how feasible this approach is when target X is a small
microprocessor or microcontroller. IIRC, GCC was designed with the 68000
CPU as the minimal target processor.

Furthermore, many embedded control CPUs have specific features that need
C extensions in order to be used, e.g. the bit addressing of the 8051
family or the on chip vs extern RAM or ROM.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #65
[though i've not set follow-up's this is off-topic for clc and i hope the
next round will move to e-mail, comp.arch.embedded or the gcc mailing-list]

in comp.lang.c i read:
I'm not sure how feasible this approach is when target X is a small
microprocessor or microcontroller. IIRC, GCC was designed with the 68000
CPU as the minimal target processor.
gcc currently supports arc, arm, avr, cris, ip2022, iq2000, m32r, mc68hc1[12],
embedded ppc and xtensa mcu's and embedded platforms, and even dsp's, e.g.,
ti's tms320c[34]x series.
Furthermore, many embedded control CPUs have specific features that need
C extensions in order to be used, e.g. the bit addressing of the 8051
family or the on chip vs extern RAM or ROM.


indeed, that would be part of the challenge. perhaps it would be too hard
for some platforms, but it can be a solution for some.

--
a signature
Nov 14 '05 #66
[though i've not set follow-up's this is off-topic for clc and i hope the
next round will move to e-mail, comp.arch.embedded or the gcc mailing-list]

in comp.lang.c i read:
I'm not sure how feasible this approach is when target X is a small
microprocessor or microcontroller. IIRC, GCC was designed with the 68000
CPU as the minimal target processor.
gcc currently supports arc, arm, avr, cris, ip2022, iq2000, m32r, mc68hc1[12],
embedded ppc and xtensa mcu's and embedded platforms, and even dsp's, e.g.,
ti's tms320c[34]x series.
Furthermore, many embedded control CPUs have specific features that need
C extensions in order to be used, e.g. the bit addressing of the 8051
family or the on chip vs extern RAM or ROM.


indeed, that would be part of the challenge. perhaps it would be too hard
for some platforms, but it can be a solution for some.

--
a signature
Nov 14 '05 #67

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

Similar topics

33
by: Knady | last post by:
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...
37
by: Patrik Huber | last post by:
Hello! I got the following Code in Assembler (NASM), which prints out "5" in realmode: mov ax, 0xB800 mov es, ax mov byte , '5' I want to do the same in gcc now, but I'm stuck. GCC...
8
by: Snis Pilbor | last post by:
First, let me announce that this is very possibly off-topic because malloc is a specific third party accessory to c, etc. I spent about an hour trying to find a more appropriate newsgroup and...
3
by: ferbar | last post by:
Hi all, I'm working on a program that tests a random number generator -rand()- in this case. Runs and Chi square test have to be applied to see if the numbers generated are random.. anyway, I'm...
3
by: Sean Shanny | last post by:
To all, We are running postgresql 7.4.1 on an G5 with dual procs, OSX 10.3.3 server, 8GB mem, attached to a fully configured 3.5TB XRaid box via fibre channel. I think we have run into this...
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
9
by: Me | last post by:
Hi, I ran into a malloc problem but I can't find the solution. I try to read a file into a variable with malloc like this: BYTE *lcdata; lcdata = malloc(fsize*sizeof(BYTE));
21
by: softwindow | last post by:
#include "stdio.h" #include "malloc.h" struct student{ int age; char *nms; struct student *next; }; struct student *create(){ int ags=0,size=sizeof(struct student); char *nms=" ";
87
by: pereges | last post by:
I have a C program which I created on Windows machine. I have compiled and executed the program on windows machine and it gives me the consistent output every time i run it. for eg. input a = 2,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.