 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Kalle Olavi Niemitalo Guest
|
Posted: Thu Aug 10, 2006 9:11 am Post subject: Re: Reading PGM file into structure |
|
|
"SP" <polifemo (AT) suscom (DOT) net> writes:
| Quote: | unsigned char **data; // Pixel values
|
[...]
| Quote: | pgm_in.data = (unsigned char *)malloc( pgm_in.nc * pgm_in.nr *
sizeof(unsigned char));
|
That tries to store an unsigned char * to a member declared as
unsigned char **.
If you want to access the pixels like pgm_in.data[y][x], then
you will have to allocate and initialize an array of pointers.
For instance:
pgm_in.data = malloc(pgm_in.nr * sizeof(pgm_in.data[0])
+ pgm_in.nr * pgm_in.nc * sizeof(pgm_in.data[0][0]));
if (pgm_in.data != NULL)
for (y = 0; y < pgm_in.nr; ++y)
pgm_in.data[y] = (unsigned char *) (pgm_in.data + pgm_in.nr)
+ y * pgm_in.nc * sizeof(pgm_in.data[0][0]);
This allocates just one block of memory, which contains both the
array of pointers and the data area into which the pointers point.
A single free(pgm_in.data) call will then suffice to get rid of
the whole thing.
Using pointer arithmetic to place objects of different types
adjacent to each other in memory typically risks violating
alignment requirements; but here it is safe because the objects
placed at the computed address are unsigned chars and so cannot
have such requirements.
If you were using some other type, such as int, then it would be
safer to either allocate the data area separately, or add padding
to ensure that the offset from the beginning of the malloc block
(which is suitably aligned for any type) is a multiple of
sizeof(int). (alignof(int) might be more efficient but is
nonstandard.)
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
jacob navia Guest
|
Posted: Thu Aug 10, 2006 9:11 am Post subject: Re: A concern about mixing C and C++ |
|
|
ziman137 wrote:
| Quote: | Hello all,
I have a question and am seeking for some advice.
I am currently working to implement an algorithmic library.
|
Very interesting project. Is there a web site/documentation for what you
are trying to do?
What your question is concerned, mixing C with C++ will not affect the
performance of the C part at all.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Hans-Bernhard Broeker Guest
|
Posted: Thu Aug 10, 2006 9:11 am Post subject: Re: Standard Library of useful utilities in C... |
|
|
Sunburned Surveyor <redefined.horizons (AT) gmail (DOT) com> wrote:
| Quote: | Does such a library exist?
|
Not really. The closest approximation may well be GTK's glib.
| Quote: | Does every C programmer implement this stuff from scratch?
|
Not every one, but lots of them do, for various reasons.
--
Hans-Bernhard Broeker (broeker (AT) physik (DOT) rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Ivan Novick Guest
|
Posted: Thu Aug 10, 2006 9:11 am Post subject: Re: Standard Library of useful utilities in C... |
|
|
I think its a reasonable question. The GNU c library is a really big
library with lots of useful stuff used by C++ programmers as well.
Remember though there are no classes, inheritance, or templates in C,
so how do you make a reusable container, i am not sure?
Sunburned Surveyor wrote:
| Quote: | I'm new to c programming, and I come to it from higher-level, object
oriented languages. I was hoping to find a standard library that
contained common structures and functions that would be used in many C
programs. As an example, I am looking for a standard implementation of
a hash table.
Does such a library exist? I checked out the GNU C Library, but it
didn't seem to have what I was looking for.
Does every C programmer implement this stuff from scratch?
Thanks,
Scott Huey
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
-- |
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Barry Schwarz Guest
|
Posted: Thu Aug 10, 2006 9:11 am Post subject: Re: Reading PGM file into structure |
|
|
On 09 Aug 2006 16:54:13 GMT, "SP" <polifemo (AT) suscom (DOT) net> wrote:
| Quote: | I am writing a program to read a PGM ASCII file into a structure.
The following code works up to the point were the fclose() call is
made, when I add the malloc() lines the compiler gives me the
following warning: "warning: assignment from incompatible pointer
type"
I think its the structute that is the problem, but I'm not sure.
The next part of the application is to read the unsigned char values
and put them im the memory pointed to by pgm_in.data so that I can then
access it as a 2 dimensional array.
Thanks for any help,
#include <stdio.h
#include <stdlib.h
#include <string.h
/* The IMAGE data structure */
typedef struct image {
char magic_num[4]; // File identifier
int nr, nc; // Rows and columns in the image
int max_val; // Max value of data
int oi, oj; // Origin
unsigned char **data; // Pixel values
}PGMimage;
int main ( int argc, char *argv[])
{
FILE *fp;
char line_in[3000];
int count = 0;
PGMimage pgm_in;
int arg_assigned;
fp = fopen( argv[1], "r");
//read the first three "NON COMMENT" lines of the file
while( fgets( line_in, 2999, fp) != NULL) {
|
fgets automatically reserves the last position for the terminating
'\0' so you can use 3000 (or sizeof line_in) here.
| Quote: | if (line_in[0] != '#'){
count = count + 1;
if (count == 1){
arg_assigned = sscanf(line_in, "%s", &pgm_in.magic_num);
|
How ironclad is the guarantee that the input string has at most 3
characters. magic_num is an array of char. Using the & here causes
undefined behavior since the %s promises a char* but you are actually
providing a char(*)[4]. On most systems where all object pointers
have the same size and representation, this is not a problem but you
should get used to doing it right.
| Quote: | }
if (count == 2){
arg_assigned = sscanf(line_in, "%d %d",&pgm_in.nc, &pgm_in.nr);
}
if (count == 3){
arg_assigned = sscanf(line_in, "%d",&pgm_in.max_val);
break;
}
}
}
fclose (fp);
|
If your string overflowed magic_num, you could easily wipe out fp.
| Quote: |
printf("PGM Header: Magic # %s\n", pgm_in.magic_num);
printf("PGM Header: Columns # %d\n", pgm_in.nc);
printf("PGM Header: Rows # %d\n", pgm_in.nr);
printf("PGM Header: Max Value # %d\n", pgm_in.max_val);
// allocate memory for array to hold image
pgm_in.data = (unsigned char *)malloc( pgm_in.nc * pgm_in.nr *
sizeof(unsigned char));
|
Don't cast the return from malloc. It never helps but can cause the
compiler to suppress a diagnostic you would really want to see.
This is almost guaranteed to be the wrong size. data is a char**.
That means that whatever is points to is a char*. Your size is
predicated on char, not char*.
| Quote: | if(!pgm_in.data)
{ printf("Memory allocation failed\n"); }
return 0;
}
|
Remove del for email
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Barry Schwarz Guest
|
Posted: Thu Aug 10, 2006 9:11 am Post subject: Re: Binary Form |
|
|
On 09 Aug 2006 16:54:25 GMT, "neha" <neham.email (AT) gmail (DOT) com> wrote:
| Quote: | hi
How floats are stored in binary form.
|
Varies depending on hardware. Check the documentation for your
system.
Remove del for email
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Barry Schwarz Guest
|
Posted: Thu Aug 10, 2006 9:11 am Post subject: Re: malloc help |
|
|
On 09 Aug 2006 16:53:11 GMT, mdrons (AT) yahoo (DOT) com wrote:
| Quote: | I have a structure defined as:
struct channel {
int chanid;
int channum;
char callsign[20];
char name[64];
};
I then have a global variable defined as:
static struct channel *chan=NULL;
I then malloc the space when I know the total number of rows (dynamic
variable Total_rows) I have:
if ( (chan=(struct channel*)malloc(sizeof(struct channel)*Total_rows))
|
Don't cast the return from malloc. It does not help and cause the
compiler to hide a diagnostic you really want to see.
| Quote: | =
= NULL) {
snprintf(buf, sizeof(buf),"Mallor Error.\n" );
free (chan);
|
If chan is NULL, why bother freeing it? However, you are allowed to
pass a NULL pointer to free().
| Quote: | goto out;
}
My issue as I fill the structure the program seg faults. If I change
the line to
if ( (chan=(struct channel*)malloc(sizeof(struct
channel)*Total_rows*sizeof(struct channel))) =
= NULL) {
It works, but I now have sizeof twice.... or if I make the Total_rows
variable 3 times as large.
|
This allocates at least 86 times as much memory as you need, probably
more. You need to show the code that fills the array of struct. You
probably go beyond the end of allocated memory.
| Quote: |
Can anyone tell me what I am doing wrong?
Thanks Mike
|
Remove del for email
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Zara Guest
|
Posted: Thu Aug 10, 2006 9:11 am Post subject: Re: A concern about mixing C and C++ |
|
|
On 09 Aug 2006 16:53:25 GMT, "ziman137" <gangxu_csu (AT) yahoo (DOT) com> wrote:
Hi
| Quote: |
I have a question and am seeking for some advice.
I am currently working to implement an algorithmic library. Because
the performance is the most important factor in later applications, I
decide to write it in C instead of C++.
|
You may be wrong, you may be right...
| Quote: | However, I thought it might be
convenient to use some C++ code at some misc places.
|
You may be wrong, you may be right...
| Quote: | I'm aware that, I
could always use the C++ compiler to get it work.
|
You may be wrong, you may be right...
| Quote: |
My concerns are:
1) Would the way I mix C and C++ code have any potential drawbacks to
the performance?
|
It depends on you. How good are your programming abilities in both
languages?
| Quote: | 2) Would the way I mix C and C++ code have any potential drawbacks for
the future users to use the library?
|
It depends on you. How good are your programming abilities in both
languages?
| Quote: |
My intention for choosing C interface instead of C++ OOD is to gain the
maximum performance as possible, yet I still like to use some C++
coding features (e.g., "const", reference instead of pointers, ...).
|
You probably *are* wrong.
You are not trying to mix C++ and C, you are trying to write C with a
C++ compiler.
Zara
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Douglas A. Gwyn Guest
|
Posted: Thu Aug 10, 2006 9:11 am Post subject: Re: A concern about mixing C and C++ |
|
|
"ziman137" <gangxu_csu (AT) yahoo (DOT) com> wrote in message
news:clcm-20060809-0035 (AT) plethora (DOT) net...
| Quote: | I am currently working to implement an algorithmic library. Because
the performance is the most important factor in later applications, I
decide to write it in C instead of C++. However, I thought it might be
convenient to use some C++ code at some misc places. I'm aware that, I
could always use the C++ compiler to get it work.
|
Not every environment that has a C compiler also has a C++ compiler.
| Quote: | 1) Would the way I mix C and C++ code have any potential drawbacks to
the performance?
|
Simply using the C++ compiler would trade away whatever performance
gain you might have achieved by choosing C to start with. There shouldn't
be a significant speed difference anyway, for "algorithmic" applications.
(maybe 5% penalty, depending on various factors).
| Quote: | 2) Would the way I mix C and C++ code have any potential drawbacks for
the future users to use the library?
|
If you code in the "intersection" of the two languages, then your
library could be compiled either way, which is certainly more
convenient than limiting users to one or the other language.
| Quote: | My intention for choosing C interface instead of C++ OOD is to gain the
maximum performance as possible, yet I still like to use some C++
coding features (e.g., "const", reference instead of pointers, ...).
|
Standard C has "const" (slightly different in detail from C++'s
notion of const", but still advantageous to exploit in coding).
Reference isn't a substitute for pointer, and if you have a
legitimate use for it in the library you should go ahead and
use the full power of C++, rather than imposing useless
limitations upon yourself.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Douglas A. Gwyn Guest
|
Posted: Thu Aug 10, 2006 9:11 am Post subject: Re: Binary Form |
|
|
"neha" <neham.email (AT) gmail (DOT) com> wrote in message
news:clcm-20060809-0040 (AT) plethora (DOT) net...
| Quote: | How floats are stored in binary form.
|
with up so many floating bells down
The answer is, of course, that it depends on the specific platform.
Most modern systems use a representation specified by the IEEE
binary floating-point standard, but even then there are variations
in byte order.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Douglas A. Gwyn Guest
|
Posted: Thu Aug 10, 2006 9:11 am Post subject: Re: sum(2,4,3,5,-1,...) |
|
|
"Allan Adler" <ara (AT) nestle (DOT) csail.mit.edu> wrote in message
news:clcm-20060809-0041 (AT) plethora (DOT) net...
| Quote: | The function sum is supposed to take a variable number of arguments
of type int and add them up. Thus, sum(0) returns 0 and sum(3,5,2,-7)
returns 3.
|
No, you can't do that in Standard C. The implementation of the
function needs to know how many arguments to use. Usually that
is passed as a separate argument, can be deduced from some of
the earlier arguments (such as a format string), or is detected as a
special terminating value while fetching successive arguments.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
John Dallman Guest
|
Posted: Thu Aug 10, 2006 9:11 am Post subject: Re: A concern about mixing C and C++ |
|
|
In article <clcm-20060809-0035 (AT) plethora (DOT) net>, gangxu_csu (AT) yahoo (DOT) com
(ziman137) wrote:
| Quote: | 1) Would the way I mix C and C++ code have any potential drawbacks to
the performance?
|
Probably not seriously.
| Quote: | 2) Would the way I mix C and C++ code have any potential drawbacks for
the future users to use the library?
|
Potential? Yes, loads. Practical? Maybe. Will all the users of your
library have source and compile it themselves, or will it be distributed
as binary-only?
If it's source, that's often easier provided that you can build it
yourself on a few platforms to catch the differences. Flags for strict
ISO are your friends here; beware of platforms-specific features,
particularly GCC and MS extensions. Consider carefully if templates add
radically to your functionality; don't use them just because they're
cool, of because you're used to them. The Standard Template Library
isn't all that standard.
If it is binary-only, then you're requiring your customers to match the
same C++ ABI, because the C++ code /will/ need support functions from a
C++ run-time library. This may seriously restrict their choice of
compilers on occasion. It will also require you to develop some
expertise in the quirks of individual compilers and their run-time
libraries. A few examples that I happen to remember:
Microsoft Visual C++ 6.0, 7.0, 7.1 and 8.0 have compatible ABIs, but use
different run-time libraries. A mixture can often be got to work if all
the interfaces are C, but it can be complicated and confusing.
Sun compilers since Sun Workshop 5.0 are all compatible. This is rather
good.
IBM Visual Age 6, XLC7 and XLC8 are all supposed to be compatible if
they are using ISO name mangling. This was introduced in Visual Age
6.0.0.3 or thereabouts; earlier versions can't manage it.
HP's C++ compiler v6 on Itanium is OK; the v5 one is no longer supported
by HP. The PA-RISC C++ compiler had an unfortunate bug that changed the
ABI at about 3.30, which was fixed at about 3.50. C++ compiled code from
that period isn't compatible with compiled code from compilers before or
after it, although the before and after compilers are compatible with
each other.
GCC changed its C++ ABI at 3.40, and there is no compatibility of C++
compiled code in either direction across that boundary.
The binary-distributed algorithm library I work on is written in C. We
have some add-ons for it written in C++, but the issues I describe have
caused us to decide, very firmly, that any further add-ons will be C.
---
John Dallman, jgd (AT) cix (DOT) co.uk, HTML mail is treated as probable spam.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Jack Klein Guest
|
Posted: Thu Aug 10, 2006 9:11 am Post subject: Re: Binary Form |
|
|
On 09 Aug 2006 16:54:25 GMT, "neha" <neham.email (AT) gmail (DOT) com> wrote in
comp.lang.c.moderated:
| Quote: | hi
How floats are stored in binary form.
|
The C standard does not define the bit-by-bit representation of the
floating point types, it is completely up to the compiler, which
usually bases its choice on either hardware formats supported by the
processor, or by the IEEE 758 or 858 floating point standard.
If you want to know how your compiler represents floating point types,
consult your compiler's documentation.
If you want to know what floating point formats your hardware
supports, consult the manufacturer's documentation for the processor.
If you are talking about x86 processors, you can download the
documentation for free from http://developer.intel.com, for example.
Note that not all compilers use all floating point formats that the
hardware supports. This is particularly true of Microsoft's Visual
C++ and some others on Windows, which deliberately do not support the
80-bit extended precision format of the x87 hardware.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Douglas A. Gwyn Guest
|
Posted: Thu Aug 10, 2006 9:11 am Post subject: Re: tolower macro |
|
|
"Hans-Bernhard Broeker" <broeker (AT) physik (DOT) rwth-aachen.de> wrote in message
news:clcm-20060809-0030 (AT) plethora (DOT) net...
| Quote: | #define _tolower(_c) ((_c)-'A'+'a')
That implementation is much worse than just "historic". It's
completely and utterly wrong. If you actually found that in any
current toolchain's library, complain to its maker. Loudly.
|
What is supposed to be wrong about it? Since _tolower is invoked
only when the argument is known to be an upper-case code, and
presumably the "toolchain" is installed on a host where ASCII or
EBDCIC (or some compatible extension) is used for the single-
byte codeset, it should work just fine.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Keith Thompson Guest
|
Posted: Thu Aug 10, 2006 9:11 am Post subject: Re: tolower macro |
|
|
whiteflags99 (AT) hotmail (DOT) com writes:
| Quote: | doskix (AT) gmail (DOT) com wrote:
Hey guys, what's up?
I was looking at <CTYPES.H> after chatting with some friends about
binary math. What I saw, and researched a little is that historically,
tolower is a macro defined like this.
#define _tolower(_c) ((_c)-'A'+'a')
[snip]
What do you guys think? How do macros like these get expanded and would
this macro be faster than the tradition tolower?
It's not portable when I think it should be.
|
There's no need for an implementation of tolower() to be portable. It
just has to work correctly for a given implementation. The above
macro doesn't satisfy this requirement, since it fails for (at least)
characters other than upper case letters, but it could conceivably be
used by an implementation of tolower().
--
Keith Thompson (The_Other_Keith) kst-u (AT) mib (DOT) 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.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Powered by phpBB © 2001, 2006 phpBB Group
|