Re: right padding



On Tue, 28 Oct 2008 13:48:05 +0000, Ben Bacarisse <ben.usenet@xxxxxxxxx>
wrote:

Using gcc in mingw on a windows machine.. All these varying 'standards'
have me dizzy.

If you pick two sets of strict options, one for C90 and one for C99
the compiler will tell you if you have used C99 features when you
don't mean to, or (in the second case) when you use implementation
specific features when you don't want to. This is one of the most
helpful things a compiler can do if you are writing library code like
this that should compile "almost everywhere". (For gcc and
derivatives -std=c89 -pedantic -Wall -Wextra is a good starting
point).

Thank you.


char buf[sizeof(int) * CHAR_BIT + 5) / 3];
I assume that you meant
char buf[(sizeof(int) * CHAR_BIT +) /3];
but I fail to understand the significance of the value between the
square brackets. It evaluates to 12 on my system.

==============================

Ok, Mr H & Mr B. I have my current code for the function below. And I
can now call it like this:
---------------------------------------------
char *str;
int num = 123;
str = pad(num, 20, '*', PAD_RIGHT);
puts(str);
str = pad(INT_MAX - 5000, 20, '=', PAD_LEFT);
puts(str);
---------------------------------------------

and I think I understand that, if I now say free(str), since the pointer
*str points to the same area as the ptr *s in the subroutine, that the
area of memory freed is the area that was allocated in the subroutine.
No?

But what if I said
--------------------------------------------
printf("%s",pad(INT_MAX - 5000, 20, '=', PAD_LEFT));
--------------------------------------------
which I have found to work, how would I free the memory allocated in the
function?

And, Mr H, your <http://www.cpax.org.uk/prg/writings/fgetdata.php> was
what I had in mind, but I was incapable of using it.. I'll try again.
"We live to learn".

Oh, here's the current code. As you see, I have taken your comments to
heart, both of you, and I thank you both,
-----------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>

#define PAD_RIGHT 0
#define PAD_LEFT 1

int digits(int n) /* how many digits in an integer */
{
if(n==0) return 1;
int sign = n;
n = abs(n);
int ct = 0;
while(n > 0) {
n /= 10;
ct++;
}
if(sign < 0) ct++;
return ct;
}

char *pad( /* func returns ptr to start of string */
int n, /* number to print */
size_t width, /* pad to this width */
int padch, /* character to pad with */
int rl) /* pad on right (0) or left (1) */
{
char *s; /* ptr to output string */
int count = 0; /* count of characters */
int dig = digits(n); /* space required for the number */
if(dig > width) {
fputs("Field not big enough for data");
exit (EXIT_FAILURE); /* maybe I should truncate? */
}

free(s);
s = malloc(width+1); /* get necessary memory */
if(s == NULL){
fputs("Inadequate memory!", stderr);
exit (EXIT_FAILURE);
}

char *t = s; /* save ptr */
if (rl == PAD_RIGHT) {
count = sprintf(t, "%d", n); /* insert num */
t += count; /* bump ptr */
while(count++ < width) /* pad on right */
*t++ = padch;
*t = '\0'; /* finish string */
}else{
while(count++ < width-dig) /* pad on left */
*t++ = padch;
sprintf(t, "%d", n); /* insert num */
}
return s; /* return ptr to start of string */
}
-----------------------------------------------------------------------

.



Relevant Pages

  • RE: getting memory errors when natively bulding native gnu gcc 3. 3.2 compiler on PQ2FADS-VR with 32
    ... Gnu gcc area: ... and/or decrease memory consumption ... getting memory errors when natively bulding native gnu gcc ... compiler on PQ2FADS-VR with 32 MB DRAM memory ...
    (Linux-Kernel)
  • Re: Deteriorating performance
    ... The process required switching to the C++ compiler; ... implementing the XML feature; there was no performance issue after the ... The problem appears to be in the GCC memory manager. ...
    (comp.os.os2.programmer.misc)
  • Re: data types
    ... Usually short is smaller than an int. ... Your compiler is either *really* old or broken or both. ... recommend either a version of gcc,, or Visual Studio ... Microsoft's Visual Studio Express has an onerous EULA). ...
    (comp.lang.c)
  • Re: data types
    ... Usually short is smaller than an int. ... memory per dms structure if they are 32-bit integers. ... current compiler system. ... recommend either a version of gcc,, or Visual Studio ...
    (comp.lang.c)
  • Re: Third party compiler middle and back-end
    ... GCC is a huge pile of nasty IMO. ... compiler technology is not to sit around trying to be a language designer. ... I wanted a real assembler and linker: ... old function is that this would effectively prevent the memory used by this ...
    (comp.compilers)