473,804 Members | 2,225 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

malloc + 4??

http://www.yep-mm.com/res/soCrypt.c

I have 2 malloc's in my program, and when I write the contents of them to
the screen or to a file, there aren addition 4 characters.

As far as I can tell, both the code to register the malloc and to write
information into the malloc is solid. Why then ismy program returning an
additional 4 characters?

register malloc 1:
line 192

register malloc 2:
line 214

write to malloc 1:
line 200 - 205

write to malloc 2:
line 221 - 225

display malloc 2:
line 157

write malloc 2:
line 251

Here's how you execute the program:

socrypt.exe /e :i input.txt :o output.txt :A keya.txt :B keyb.txt :k
keyout.txt

**note that the input, keya, and keyb files must exist or the program will
return an error code.

If you write a text string into the input.txt file, it will write the same
string into the output.txt file plus an addition 4 characters.

The 1024 char random 'masterkey' is also written out to the keyout.txt file
with an addition 4 characters.

Why is this happening? I'm totally baffled and have spent days trying to
figure this out.
Nov 14 '05
144 5428
Martin Ambuhl wrote:

John Tsiombikas (Nuclear / the Lab) wrote:
Kevin Torr wrote:
So when do I need not cast mallocs?


Never.

Actually he *always* need not cast mallocs.
What he need never do is cast mallocs.


Hm, it seems I missed the *not* in there, for some obscure reason I
thought that he asked "So when do I need to cast mallocs?". :)))

--
John Tsiombikas (Nuclear / the Lab)
nu*****@siggrap h.org
http://thelab.demoscene.gr/nuclear/
Nov 14 '05 #11
Alberto Giménez <al****@telelin e.es> scribbled the following:
El 8 Apr 2004 08:33:51 GMT, Joona I Palaste escribió:
People here might know next to nothing about Finnish, but like it's
been said, Finnish is pronounced pretty much like it's written. I have
studied (at least cursorily) many languages, and I truly believe Finnish
gets the closest to a 1-1 correspondence between written glyphs and
spoken sounds.
I'm spanish, and I have to say that spanish is *exactly* pronounced as
it is written, except for "h" letter, that is not pronounced at all.


Close, but no cigar. Some minor points: Why is the 'u' in "qu"
pronounced differently than the normal 'u'? (For example "una
quilogramme".) Why do 'l' by itself and "ll" have separate
pronunciations? (I don't know how the "ll" is pronounced correctly,
but I think I know it's *not* pronounced as two 'l' sounds.)
Why can 'y' be both a consonant (like in "yo") and a vowel (like in
"hay")?
I suppose 'j' in Spanish is always pronounced like 'h' in English.
Fair enough, but seeing as it's pronounced in Finnish like the
consonant 'y' in English and Spanish, it strikes me as a little weird.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"It was, er, quite bookish."
- Horace Boothroyd
Nov 14 '05 #12
Ben Pfaff wrote:
when I include a header? which header?


You have to include stdlib.h when you use malloc (or provide the
prototype of malloc() yourself, but i can't imagine why you would
prefer to do that)


Providing a prototype of malloc() yourself is arguably not valid
practice based on this sentence from the standard, section 7.1.4:

2 Provided that a library function can be declared without
reference to any type defined in a header, it is also
permissible to declare the function and use it without
including its associated header.

You can certainly declare malloc() without a type defined in a
header, but giving a prototype requires using size_t. It's
better just to use the header.


So are you saying that you can't do the following?

#include <stddef.h> /* for size_t */
void *malloc(size_t) ;

this is providing a prototype for malloc, without including stdlib.h and
yes ofcourse it's better to just use the header, I just added that
comment for completeness, if I may quote myself :)
"or provide the prototype of malloc() yourself, but i can't imagine why
you would prefer to do that"

--
John Tsiombikas (Nuclear / the Lab)
nu*****@siggrap h.org
http://thelab.demoscene.gr/nuclear/
Nov 14 '05 #13
"John Tsiombikas (Nuclear / the Lab)" <nu*****@siggra ph.org> writes:
Ben Pfaff wrote:
when I include a header? which header?

You have to include stdlib.h when you use malloc (or provide the
prototype of malloc() yourself, but i can't imagine why you would
prefer to do that)

Providing a prototype of malloc() yourself is arguably not valid
practice based on this sentence from the standard, section 7.1.4:
2 Provided that a library function can be declared without
reference to any type defined in a header, it is also
permissible to declare the function and use it without
including its associated header.
You can certainly declare malloc() without a type defined in a
header, but giving a prototype requires using size_t. It's
better just to use the header.


So are you saying that you can't do the following?

#include <stddef.h> /* for size_t */
void *malloc(size_t) ;

this is providing a prototype for malloc, without including stdlib.h


You got size_t from a header, which seems to fall afoul of the
spirit of the provision above. Whether it is actually undefined
behavior would be better judged in comp.std.c. But it is better
in any case to simply include <stdlib.h>.
--
"We put [the best] Assembler programmers in a little glass case in the hallway
near the Exit sign. The sign on the case says, `In case of optimization
problem, break glass.' Meanwhile, the problem solvers are busy doing their
work in languages most appropriate to the job at hand." --Richard Riehle
Nov 14 '05 #14

On Tue, 13 Apr 2004, Joona I Palaste wrote:

Alberto Giménez <al****@telelin e.es> scribbled the following:
El 8 Apr 2004 08:33:51 GMT, Joona I Palaste escribió:
People here might know next to nothing about Finnish, but like it's
been said, Finnish is pronounced pretty much like it's written. I have
studied (at least cursorily) many languages, and I truly believe Finnish
gets the closest to a 1-1 correspondence between written glyphs and
spoken sounds.
I'm spanish, and I have to say that spanish is *exactly* pronounced as
it is written, except for "h" letter, that is not pronounced at all.


Close, but no cigar. Some minor points: Why is the 'u' in "qu"
pronounced differently than the normal 'u'? (For example "una
quilogramme".)


Is this correct in some Spanish dialect with which I'm unfamiliar?
I thought the word for "kilogram" in Spanish was... well.. "kilogramo. "
Certainly the "gramme" ending in Joona's word isn't Spanish; Spanish
doesn't double consonants. Looks like a weird Ibero-British hybrid
to me. :)
(After Googling: is this something like Catalan?)
Why do 'l' by itself and "ll" have separate
pronunciations? (I don't know how the "ll" is pronounced correctly,
but I think I know it's *not* pronounced as two 'l' sounds.)
The two-'l' letter is the "elle" (pronounced roughly like the
English letter "A": "A-yay"). In words, it's pronounced like the
English 'y': "me llamo" -> "may yamo". And perfectly regularly so.

Spanish used to consider both the 'll' and the 'ch' to be letters
in their own right, along with the enye (n+tilde; sorry, not in my
encoding). But IIRC recently the Spanish people in charge of the
"official" language decided to give up the separate letters for 'ch'
and 'll', and now you'll find "llama" in between "liviano" and "local"
in the dictionary.
Why can 'y' be both a consonant (like in "yo") and a vowel (like in
"hay")?
I'd say, because Spanish doesn't consider 'y' either a consonant or
a vowel, just as in English. The 'y' sound is kind of in-between.
In any event, the 'y' in "yo" isn't really acting like a consonant:
it's just adding the extra "ee" sort of sound. Just like it's doing
in "hay," which without the 'y' would be pronounced "ahh." With the
'y', it's pronounced "ahh-ee," but run together into "ai."

[It's weird trying to write down phonetic descriptions in "English"
syllables, when we're talking about a *more* phonetic language in the
first place, and I know English isn't your first language in the second
place. ;) ]
I suppose 'j' in Spanish is always pronounced like 'h' in English.
Correct, AFAIK.
Fair enough, but seeing as it's pronounced in Finnish like the
consonant 'y' in English and Spanish, it strikes me as a little weird.


Sounds to me like *Finnish* is the weird one. ;-))

-Arthur

Nov 14 '05 #15
"Joona I Palaste" <pa*****@cc.hel sinki.fi> wrote in message
news:c5******** **@oravannahka. helsinki.fi...
Alberto Giménez <al****@telelin e.es> scribbled the following:
I'm spanish, and I have to say that spanish is *exactly* pronounced as
it is written, except for "h" letter, that is not pronounced at all.
Close, but no cigar. Some minor points: Why is the 'u' in "qu"
pronounced differently than the normal 'u'? (For example "una
quilogramme".) Why do 'l' by itself and "ll" have separate
pronunciations?


'ch', 'll', and 'rr' are considered separate letters in the Spanish alphabet
with their own pronunciation, not combinations of two letters. For example,
the word "churro" has four letters and should come _after_ "cuba" in sort
order. Arguably, 'qu' should be another (composite) letter as well,
especially since 'q' isn't used on its own.

"kilogramo" or just "kilo" are the words I know in Spanish, by the way.
However, since 'k' is not in the Spanish alphabet, I presume the correct
spelling is "quilogramo ". The -mme ending in your spelling looks French,
not Spanish.
(I don't know how the "ll" is pronounced correctly, but I think I know it's *not* pronounced as two 'l' sounds.)
The exact pronunciation of 'll' depends on dialect; most pronounce it as the
French do, with a 'y' sound.
Why can 'y' be both a consonant (like in "yo") and a vowel (like in
"hay")?
For every word I can think of, you get the correct pronunciation (for most
dialects) if you replace 'y' with 'i' -- in fact the name of 'y' is "i
griega", or "greek i". Of course, there are dialects where the consonant
sound of 'y' is very different, but that's an aberration.
I suppose 'j' in Spanish is always pronounced like 'h' in English.
Fair enough, but seeing as it's pronounced in Finnish like the
consonant 'y' in English and Spanish, it strikes me as a little weird.


When switching between English, Spanish, and French, the 'j' probably trips
me up more than anything else since it has totally different sounds in each.
Most other letters sound the same in at least two of them.

S

--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Aaron Sorkin

Nov 14 '05 #16
Alberto Giménez wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

El 8 Apr 2004 08:33:51 GMT, Joona I Palaste escribió:
People here might know next to nothing about Finnish, but like it's
been said, Finnish is pronounced pretty much like it's written. I have
studied (at least cursorily) many languages, and I truly believe Finnish
gets the closest to a 1-1 correspondence between written glyphs and
spoken sounds.

I'm spanish, and I have to say that spanish is *exactly* pronounced as
it is written, except for "h" letter, that is not pronounced at all.

With all due respect, Spain (Iberia) has four (more?) regions with
their own languages. Castille, Andalusia, Catalonia, Basque, etc.
Which of these are you talking about? Then there is Central and most
of South America. Do you suppose these people write and pronounce
Spanish the same, or even like you do? How do you pronounce
'tortilla' where you live, and what would you get if you ordered it?
How would you pronounce it in Mexico and what would you get?

Phonetic spelling is tedious so bear with me if you can.
In Spain it is 'tor-TILL-ya' and is very much like an omelette.
In Mexico it is 'tor-TEE-ya' and is a dry corn pancake.

--
Joe Wright mailto:jo****** **@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #17
"Kevin Torr" <ke*******@hotm ail.com> writes:
"Chris Torek" <no****@torek.n et> wrote in message
news:c4******** *@news2.newsguy .com... [...] Wow, thanks for all that. I will have to get my head around it all.

So when do I need not cast mallocs? when I include a header? which header?
Or are you saying that I don't need to cast a malloc if I've already defined
the pointer as a data type?
What would be a possible bad thing if I did cast a malloc when I didn't need
to? Is there something that could go wrong or is it just redundant?


Speaking of redundant, you just re-posted over 230 lines of Chris
Torek's text. Please limit quoted material to what's relevant to your
reply.

To answer your question, if you're going to use malloc() you should
always include <stdlib.h>, and you should never cast the result.

Casting the result masks possible errors. It can eliminate the
warning message you'll get if you've forgotten to include <stdlib.h>,
but only because you're lying to the compiler. It's like eliminating
a warning light on your dashboard by unscrewing the light bulb rather
than by fixing the problem it's trying to tell you about.

--
Keith Thompson (The_Other_Keit h) 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 #18
Joona I Palaste <pa*****@cc.hel sinki.fi> wrote:
Alberto Giménez <al****@telelin e.es> scribbled:
Joona I Palaste escribió:
People here might know next to nothing about Finnish, but like it's
been said, Finnish is pronounced pretty much like it's written. I have
studied (at least cursorily) many languages, and I truly believe Finnish
gets the closest to a 1-1 correspondence between written glyphs and
spoken sounds.

I must agree with you here. One can make a fairly good guess at a
Finnish word's pronunciation by interpreting its letters as letters
in the International Phonetic Association's alphabet
(http://www.arts.gla.ac.uk/IPA/ipa.html)
I'm spanish, and I have to say that spanish is *exactly* pronounced as
it is written, except for "h" letter, that is not pronounced at all.
Close, but no cigar. Some minor points: Why is the 'u' in "qu"
pronounced differently than the normal 'u'? (For example "una
quilogramme".) Why do 'l' by itself and "ll" have separate
pronunciations? (I don't know how the "ll" is pronounced correctly,
but I think I know it's *not* pronounced as two 'l' sounds.)
Why can 'y' be both a consonant (like in "yo") and a vowel (like in
"hay")?


Another minor point that comes to mind: 'c' in "capitan" vs. 'c'
in "Barcelona" . I'm no authority on Spanish pronunciation but I
believe these two are different.
I suppose 'j' in Spanish is always pronounced like 'h' in English.


In fact 'j' in Spanish is a consonant that does not occur in English
(it does occur in German, and most other european languages).
English speakers say it as 'h' because it is the closest-matching
English sound. In fact it is common for allophones (sounds which differ
audibly but are interchangeable in pronunciation without affecting
the word) to be "heard" as only one sound, by people who have not put
effort into discerning the difference. Another example is English 'th'
in "thick" vs. "then".

At any rate this isn't a "problem" , I interpret "pronounced as it
is written" to mean "the same letter has the same pronunciation,
regardless of its context".
Nov 14 '05 #19
"Chris Torek" <no****@torek.n et> wrote in message
(although most systems, today,
have an even wider range, about +/- 2 billion).


Only, however, if you live in the US. (Or France).
Nov 14 '05 #20

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

Similar topics

19
683
by: john smith | last post by:
Can someone please explain to me what is happening when I do a malloc(0). This is what I did. int* p = (int*)malloc(0); Then I printed the value of p and of course it was non-null. But has it allocated memory or what?
34
6449
by: Richard Hunt | last post by:
I'm sorry for asking such a silly question, but I can't quite get my head around malloc. Using gcc I have always programmed in a lax C/C++ hybrid (which I suppose is actually c++). But I have started messing around in Plan 9, and that sort of thing is totally no go there :). Is this correct to allocate memory for my struct? It works on my computer, but I'm suspicious that I'm doing it wrong. --
231
23256
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought (perhaps mistakenly) that the purpose of a void pointer was to cast into a legitimate date type. Is this wrong? Why, and what is considered to be correct form?
7
2219
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ http://www.eskimo.com/~scs/C-faq/faq.html but it doesn't seem to answer my questions... So, I've made an example behind, with some included questions...
20
10765
by: spasmous | last post by:
main() { float * f; initialize_f(f); // ...use f for processing free(f); }
15
2593
by: Martin Jørgensen | last post by:
Hi, I have a (bigger) program with about 15-30 malloc's in it (too big to post it here)... The last thing I tried today was to add yet another malloc **two_dimensional_data. But I found out that malloc always returned null at this moment and the program exited (even though if I malloc'ed only 20 bytes or something)... Then I googled for this problem and found something about a memory pool??? Is that standard C? I didn't understand it,...
68
15722
by: James Dow Allen | last post by:
The gcc compiler treats malloc() specially! I have no particular question, but it might be fun to hear from anyone who knows about gcc's special behavior. Some may find this post interesting; some may find it off-topic or confusing. Disclaimers at end. The code samples are intended to be nearly minimal demonstrations. They are *not* related to any actual application code.
40
2612
by: Why Tea | last post by:
What happens to the pointer below? SomeStruct *p; p = malloc(100*sizeof(SomeStruct)); /* without a cast */ return((void *)(p+1)); /* will the returned pointer point to the 2nd struct? */ Seems to me there is no guarantee it will. /Why Tea
71
19145
by: desktop | last post by:
I have read in Bjarne Stroustrup that using malloc and free should be avoided in C++ because they deal with uninitialized memory and one should instead use new and delete. But why is that a problem? I cannot see why using malloc instead of new does not give the same result.
23
2736
by: raphfrk | last post by:
I am having an issue with malloc and gcc. Is there something wrong with my code or is this a compiler bug ? I am running this program: #include <stdio.h> #include <stdlib.h> typedef struct pxl { double lon, lat;
0
9715
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10600
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10097
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3835
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.