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

Strange output problem

Hi Everyone,

I am having a few issues while attempting to use Kevin Easton's base64
encryption algorythm. (See
http://groups.google.com/groups?selm...to.pcug.org.au)

here is a sample program I am using to test the algorythm.

----- start main.c -----

#include <stdio.h>
#include "base64.h"

int main(void)
{
char string[5] = "hello";
int len;
char string2[10];
len = strlen(string);
printf("%s\n", string);
b64_encode(string2, string, len);
printf("%s\n", string2);

return 0;
}
----- end main.c -----

I compile the program using:

gcc base64.c main.c -o base

And when I run it I get the following output.
mick@codegurus $ ./base
hello?(????
aGVsbG/wBCjG7L+/AQ==??????(

Does anyone know where its going wrong?
Nov 14 '05 #1
20 1853


Materialised wrote:
Hi Everyone,

I am having a few issues while attempting to use Kevin Easton's base64
encryption algorythm. (See <snip> char string[5] = "hello";


Needs to be size "6" to hold the null character.

Ed.

Nov 14 '05 #2
I'll have a go! NEWBIE ANSWER -- probably full of errors too :)

Materialised wrote:
#include <stdio.h>
#include "base64.h"

int main(void)
{
char string[5] = "hello";
string[0] = 'h'; /* this */
string[1] = 'e'; /* makes */
string[2] = 'l'; /* string */
string[3] = 'l'; /* 5 chars */
string[4] = 'o'; /* long! */
/* "hello" needs at least 6 chars: 'h', 'e', 'l', 'l', 'o', '\0' */

no more chars have been allocated

--> UB (undefined behaviour) ???

you are trying to copy '\0' to a place in memory you do not own.
You may want to let the compiler decide on the array size:
char string[] = "hello";

or specify enough size for your constants
char string[32] = "hello";
int len;
char string2[10];
len = strlen(string);
I think strlen() will go on after the final 'o' checking for the null
terminator which can be _anywhere_
printf("%s\n", string);
same thing here
b64_encode(string2, string, len);
and here
printf("%s\n", string2);

return 0;
}
----- end main.c -----

I compile the program using:

gcc base64.c main.c -o base

And when I run it I get the following output.
mick@codegurus $ ./base
hello?(????


Lucky you got a '\0' not very far away :)

--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Nov 14 '05 #3
Pedro Graca <he****@hotpop.com> wrote:
Materialised wrote:
#include <stdio.h>
#include "base64.h"

int main(void)
{
char string[5] = "hello";
string[0] = 'h'; /* this */
string[1] = 'e'; /* makes */
string[2] = 'l'; /* string */
string[3] = 'l'; /* 5 chars */
string[4] = 'o'; /* long! */
/* "hello" needs at least 6 chars: 'h', 'e', 'l', 'l', 'o', '\0' */

no more chars have been allocated

--> UB (undefined behaviour) ???


Not undefined behaviour.
you are trying to copy '\0' to a place in memory you do not own.


It's actually a special case of character array
initialisation -- if there is not enough space for the
terminating null, it will not be assigned.

--
Michael
Nov 14 '05 #4
Mac
On Fri, 02 Apr 2004 05:09:07 +0000, Michael Fyles wrote:
Pedro Graca <he****@hotpop.com> wrote:
Materialised wrote:
#include <stdio.h>
#include "base64.h"

int main(void)
{
char string[5] = "hello";
string[0] = 'h'; /* this */
string[1] = 'e'; /* makes */
string[2] = 'l'; /* string */
string[3] = 'l'; /* 5 chars */
string[4] = 'o'; /* long! */
/* "hello" needs at least 6 chars: 'h', 'e', 'l', 'l', 'o', '\0' */

no more chars have been allocated

--> UB (undefined behaviour) ???


Not undefined behaviour.


Until you call a function which expects a C string. Without the
terminating NUL, it is not a string.
you are trying to copy '\0' to a place in memory you do not own.


It's actually a special case of character array
initialisation -- if there is not enough space for the
terminating null, it will not be assigned.


--Mac

Nov 14 '05 #5
Michael Fyles <mf@dcs.warwick.ac.uk> spoke thus:
Not undefined behaviour.


But OP's attempt to print it was, correct?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #6


Materialised dropped $0.03 in the slot and wrote:
I am having a few issues while attempting to use Kevin Easton's base64
encryption algorythm. <snip> here is a sample program I am using to test the algorythm.

----- start main.c -----

#include <stdio.h>
#include "base64.h"

int main(void)
{
char string[5] = "hello"; Bzzzt-----------------^^
Here is your problem. "hello" takes six chars (when you include the
terminating 0)
make that:
char string[] = "hello";
and let the compiler count the number of chars needed. int len;
char string2[10];
len = strlen(string);
printf("%s\n", string);
b64_encode(string2, string, len);
printf("%s\n", string2);

return 0;
}
----- end main.c -----

I compile the program using:

gcc base64.c main.c -o base

And when I run it I get the following output.
mick@codegurus $ ./base
hello?(????
aGVsbG/wBCjG7L+/AQ==??????(

Does anyone know where its going wrong?

Yep you thought that "hello" was five chars long :-)

Roger
Nov 14 '05 #7
Materialised wrote:
here is a sample program I am using to test the algorythm.

^^^^^^^^^

I like this word :) Yeah, I got good algorythm.

--
Thomas.

Nov 14 '05 #8
Thomas Stegen wrote:

Materialised wrote:
here is a sample program I am using to test the algorythm.

^^^^^^^^^

I like this word :) Yeah, I got good algorythm.


Algorhythm,
Algomusic,
Algorhythm,
Who could ask for anything more?

--
Er*********@sun.com
Nov 14 '05 #9
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Eric Sosman wrote:

| Thomas Stegen wrote:
|
|>Materialised wrote:
|>
|>>here is a sample program I am using to test the algorythm.
|>
|> ^^^^^^^^^
|>
|>I like this word :) Yeah, I got good algorythm.
|
|
| Algorhythm,
| Algomusic,
| Algorhythm,
| Who could ask for anything more?

"Algorythm" sounds like a method of birth control for programmers ;-)
- --

Lew Pitcher, IT Consultant, Enterprise Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFAbb6LagVFX4UWr64RAqT6AKCXg0DmOffJRQ2PAvPyKz ySzkTKSACeLz2U
zwnH0ydnwt66a8BMj+Tjrx0=
=6A5R
-----END PGP SIGNATURE-----
Nov 14 '05 #10
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote in
news:c4**********@chessie.cirr.com:
Michael Fyles <mf@dcs.warwick.ac.uk> spoke thus:
Not undefined behaviour.


But OP's attempt to print it was, correct?


Indeed. printf() (and all other string-expecting C functions, whether or
not the Standard mentions them) expects a null-terminated string.
Nov 14 '05 #11
In <0L********************@onewest.net> August Derleth <se*@sig.now> writes:
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote in
news:c4**********@chessie.cirr.com:
Michael Fyles <mf@dcs.warwick.ac.uk> spoke thus:
Not undefined behaviour.


But OP's attempt to print it was, correct?


Indeed. printf() (and all other string-expecting C functions, whether or
not the Standard mentions them) expects a null-terminated string.


Wrong!

fangorn:~/tmp 38> cat test.c
#include <stdio.h>

int main(void)
{
char word[5] = "hello";

printf("%.5s\n", word);
return 0;
}
fangorn:~/tmp 39> gcc test.c
fangorn:~/tmp 40> ./a.out
hello

And, to prove that it worked by design and not by accident:

s If no l length modifier is present, the argument shall
be a pointer to the initial element of an array of
character type. Characters from the array are
written up to (but not including) the terminating null
character. If the precision is specified, no more than
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
that many bytes are written. If the precision is not
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^
specified or is greater than the size of the array,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^
the array shall contain a null character.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The standard is usually very precise WRT when a null-terminated string
is actually expected and when any ordinary character array will do.
No need for bogus rules of thumb.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #12
In <0L********************@onewest.net> August Derleth <se*@sig.now> writes:
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote in
news:c4**********@chessie.cirr.com:
Michael Fyles <mf@dcs.warwick.ac.uk> spoke thus:
Not undefined behaviour.


But OP's attempt to print it was, correct?


Indeed. printf() (and all other string-expecting C functions, whether or
not the Standard mentions them) expects a null-terminated string.


Wrong!

fangorn:~/tmp 38> cat test.c
#include <stdio.h>

int main(void)
{
char word[5] = "hello";

printf("%.5s\n", word);
return 0;
}
fangorn:~/tmp 39> gcc test.c
fangorn:~/tmp 40> ./a.out
hello

And, to prove that it worked by design and not by accident:

s If no l length modifier is present, the argument shall
be a pointer to the initial element of an array of
character type. Characters from the array are
written up to (but not including) the terminating null
character. If the precision is specified, no more than
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
that many bytes are written. If the precision is not
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^
specified or is greater than the size of the array,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^
the array shall contain a null character.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The standard is usually very precise WRT when a null-terminated string
is actually expected and when any ordinary character array will do.
No need for bogus rules of thumb.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #13
August Derleth wrote:

Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote in
news:c4**********@chessie.cirr.com:
Michael Fyles <mf@dcs.warwick.ac.uk> spoke thus:
Not undefined behaviour.


But OP's attempt to print it was, correct?


Indeed. printf() (and all other string-expecting C functions, whether or
not the Standard mentions them) expects a null-terminated string.


Nit-pick: If the "%s" conversion is used with a precision
(e.g. "%.10s" or "%.*s") the corresponding argument need only
point to the start of a character array, not necessarily zero-
terminated.

char hello[5] = "hello"; /* no terminator */
printf ("%s\n", hello); /* undefined behavior */
printf ("%.4s\n", hello); /* well-defined; prints "hell\n" */

--
Er*********@sun.com
Nov 14 '05 #14
August Derleth wrote:

Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote in
news:c4**********@chessie.cirr.com:
Michael Fyles <mf@dcs.warwick.ac.uk> spoke thus:
Not undefined behaviour.


But OP's attempt to print it was, correct?


Indeed. printf() (and all other string-expecting C functions, whether or
not the Standard mentions them) expects a null-terminated string.


Nit-pick: If the "%s" conversion is used with a precision
(e.g. "%.10s" or "%.*s") the corresponding argument need only
point to the start of a character array, not necessarily zero-
terminated.

char hello[5] = "hello"; /* no terminator */
printf ("%s\n", hello); /* undefined behavior */
printf ("%.4s\n", hello); /* well-defined; prints "hell\n" */

--
Er*********@sun.com
Nov 14 '05 #15
Dan Pop <Da*****@cern.ch> spoke thus:
char word[5] = "hello";
printf("%.5s\n", word);


Or, better yet,

printf( "%.*s\n", sizeof word, word );

Right?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #16
Dan Pop <Da*****@cern.ch> spoke thus:
char word[5] = "hello";
printf("%.5s\n", word);


Or, better yet,

printf( "%.*s\n", sizeof word, word );

Right?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #17
Christopher Benson-Manica wrote:

Dan Pop <Da*****@cern.ch> spoke thus:
char word[5] = "hello";
printf("%.5s\n", word);


Or, better yet,

printf( "%.*s\n", sizeof word, word );

Right?


printf ("%.*s\n", (int)(sizeof word), word);

--
Er*********@sun.com
Nov 14 '05 #18
Christopher Benson-Manica wrote:

Dan Pop <Da*****@cern.ch> spoke thus:
char word[5] = "hello";
printf("%.5s\n", word);


Or, better yet,

printf( "%.*s\n", sizeof word, word );

Right?


printf ("%.*s\n", (int)(sizeof word), word);

--
Er*********@sun.com
Nov 14 '05 #19
In <c4**********@chessie.cirr.com> Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
Dan Pop <Da*****@cern.ch> spoke thus:
char word[5] = "hello";
printf("%.5s\n", word);


Or, better yet,

printf( "%.*s\n", sizeof word, word );

Right?


Wrong. Undefined behaviour is never better than well defined behaviour.

Even if correctly done, your variant is ludicrous for my trivial example
and unlikely to be of much use in real life code, where the actual value
is seldom dictated by the size of the array (whose definition may not
even be in scope).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #20
In <c4**********@chessie.cirr.com> Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
Dan Pop <Da*****@cern.ch> spoke thus:
char word[5] = "hello";
printf("%.5s\n", word);


Or, better yet,

printf( "%.*s\n", sizeof word, word );

Right?


Wrong. Undefined behaviour is never better than well defined behaviour.

Even if correctly done, your variant is ludicrous for my trivial example
and unlikely to be of much use in real life code, where the actual value
is seldom dictated by the size of the array (whose definition may not
even be in scope).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #21

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

Similar topics

4
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
24
by: LineVoltageHalogen | last post by:
Greetings All, I was hoping that someone out there has run into this issue before and can shed some light on it for me. I have a stored procedure that essentially does a mini ETL from a source...
8
by: grundmann | last post by:
Hello, i got a strange compiler error. When compiling the following: // forward declarations typedef AvlTree<LineSegment,LineSegmentComperator> LSTree; void handleEventPoint (const...
6
by: leonecla | last post by:
Hi everybody, I'm facing a very very strange problem with a very very simple C program... My goal should be to write to a binary file some numbers (integers), each one represented as a sequence...
1
by: Martin Feuersteiner | last post by:
Dear Group I'm having a very weird problem. Any hints are greatly appreciated. I'm returning two values from a MS SQL Server 2000 stored procedure to my Webapplication and store them in...
5
by: soeren | last post by:
Hello, two days ago I stumbled across a very strange problem that came up when we were printing tiny double numbers as strings and trying to read them on another place. This is part of an object...
5
by: Ian | last post by:
Hi everyone, I have found some bizarre (to me...!) behaviour of the Form_Activate function. I have a form which has a button control used to close the form and a subform with a datasheet view...
1
by: Paul | last post by:
Hi all, I've hit a strange problem with my ASP .NET site - occasionally when I go to a page, it will have part of another request's output mixed in with my output. This breaks the HTML on the...
4
by: ZuLuuuuuu | last post by:
Hello, I have a strange problem or the problem is very obvious and I can't see it :) I'm kind of a new C programmer so the chance of the latter is more :) Anyway, I have a structure like this: ...
2
by: danep2 | last post by:
Hello all This is a really strange problem. I have code that performs a few calculations based on input from a joystick, and writes these values to a file using basically the following code: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.