string comparison

C

Christian Kandeler

C strings have trailing zeros, but those are only important if you wish
to count out the length of the string or perform other string functions
after the assignment. You do not need to have the trailing zero.

If it does not have the trailing zero, it is not a C string.
The allocation of memory occurs either in .bss or in the heap.
The address of the string sits on the stack.

This may or may not be true, depending on your platform. And it has nothing
to do with the topic.
What I do not understand is why you think I created a memory leak.

You wrote this:

    string = malloc(5);
    string = "hello\n";

How could it be any more obvious? You are allocating five bytes of memory
and then you immediately throw away the information about where it is
stored. There now is no way for you to free it, which means you have
created a memory leak. I suspect, though, that you believe the second line
stores the "hello\n" string in the allocated memory. In that case you
definitely need to read a book on C. Not to mention that if your assumption
was true, the whole thing would be even worse as you'd cause undefined
behavior.


Christian
 
L

Lawrence Kirby

When you write
if (a_var == "a string") { /* ... */ }
you're really writing "is the value of a_var equals to the value of the
address that points to the constant string 'a string' ?" So, yes, there is
a conversion, but maybe not the one you thought...

Rather the address of the first element of the array of char defined by
the string literal.

Lawrence
 
B

bwaichu

Keith said:
The second statement causes string to point to the beginning of the
string literal "hello\n" (which occupies 7 bytes). You've now lost
your only pointer to the memory allocated by malloc(), and you have no
way to release it (there's nothing you can pass to free()). That's a
memory leak.

You're right. I'm overwriting the address of the memory I had
allocated.

movq %rax, -8(%rbp)
movq $.LC0, -8(%rbp)

I shouldn't code past a certain hour.

But exit() frees memory allocated with malloc(). Is there any reason
to use free() anymore?

And yes, in practice, you should null terminate C strings or else you
will have off by one bugs.

Brian
 
C

Chris Dollin

But exit() frees memory allocated with malloc(). Is there any reason
to use free() anymore?

Yes.

(a) I don't see any guarantee that exit() frees memory that's been
mallocated.

(b) Programs become program components.

(c) Sometimes, the total amount of memory turned over by a program
in its lifetime exceeds the amount of available memory.

(d) You'll likely end up with fragmented virtual memory and your
program will thrash.

(e) If you want a garbage-collected language, you have several to
choose from.
And yes, in practice, you should null terminate C strings or else you
will have off by one bugs.

If it's not null-terminated, it isn't a C string.
 
K

Keith Thompson

You're right. I'm overwriting the address of the memory I had
allocated.

movq %rax, -8(%rbp)
movq $.LC0, -8(%rbp)

Assembly listings are rarely useful here. I don't even know (or
particularly care) which assembly language you're using.

[...]
And yes, in practice, you should null terminate C strings or else you
will have off by one bugs.

You understate the importance of the null terminator. With the '\0'
terminator, you simply don't have a string. If you have a
non-terminated character array and you try to treat it as a string,
you'll very likely get undefined behavior, which can be arbitrarily
bad.
 
B

bwaichu

The null termination is a trademark of the string library of functions
in C. A lot of those functions are horribly written.

You can write your own string functions to replace those that are
broken like strcpy and strncpy, or you can spin your own type of
string. You will have to include object files to have any portability.

And the reason why I look at the assembly is to see how my compiler
treats the C code I write. I don't care what the standard says if all
the behaviors are not properly implemented. I know I am coding for a
gcc compiler, so I need to know how that compiler follows the
standards.

If I strictly followed the CSS2 standard, mosts pages viewed in IE
would have problems. You cannot all ways depend upon standards.

Brian
 
R

Robert Gamble

(e-mail address removed) wrote:

This is the third time in this thread you have posted without providing
any context and you have already been warned about this by Keith
elsethread. I know that you know how to do this because your last post
quoted context. Continual blatant disregard for basic usenet etiquette
is likely to get you plonked.
The null termination is a trademark of the string library of functions
in C.

Null termination is part of the definition of a string. The Standard
defines a string as "a contiguous sequence of characters terminated by
and including the first null character".
A lot of those functions are horribly written.

If they are "horribly written" in your implementation, that is no fault
of the Standard, or did you mean horribly designed?
You can write your own string functions to replace those that are
broken like strcpy and strncpy,

Please explain how these functions are "broken", on second thought,
don't.
or you can spin your own type of string.

Your own type of string? You can create anything you like and call it
a string but that doesn't make it one.
You will have to include object files to have any portability.

This makes no sense at all. Object files are inherently not portable.
And the reason why I look at the assembly is to see how my compiler
treats the C code I write. I don't care what the standard says if all
the behaviors are not properly implemented. I know I am coding for a
gcc compiler, so I need to know how that compiler follows the
standards.

This is all very well discussed in the gcc documentation, I would think
it would be a little easier to consult that than to try to pick through
the machine generated assembly code. If you don't trust the
documentation, then why trust that the assembler will generate the
machine code properly? Do you check the machine code to see if the
assembler is doing what you expect?
If I strictly followed the CSS2 standard, mosts pages viewed in IE
would have problems. You cannot all ways depend upon standards.

You cannot depend on applications that intentionally deviate from
standards, there is a difference here.

Robert Gamble
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,918
Members
47,458
Latest member
Chris#

Latest Threads

Top