Valhalla Legends Forums Archive | General Programming | [C] Dynamically Allocated Arrays

AuthorMessageTime
MrRaza
Here is a tid-bit of code i found.

[code]
/* Allocate space for a string dynamically, request user input, and then print the string backwards. */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void main(void)
{
char *s;
register int t;
s = malloc(80);

if(!s) {
printf("Memory request failed.\n");
exit(1);
}

fget(s);
for(t=strlen(s)-1; t>=0; t--) putchar(s[t]);
free(s);
}
[/code]

April 22, 2003, 1:01 PM
Skywing
[quote author=MrRaza link=board=5;threadid=1123;start=0#msg8204 date=1051016479]
Here is a tid-bit of code i found.

[code]
/* Allocate space for a string dynamically, request user input, and then print the string backwards. */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void main(void)
{
char *s;
register int t;
s = malloc(80)

if(!s) {
printf("Memory request failed.\n");
exit(1);
}

get(s);
for(t=strlen(s)-1; t>=0; t--+) putchar(s[t]);
free(s)
}
[/code]


[/quote]That's got quite a few errors: t--+? No semicolon after free(s) or malloc(80)? get instead of fgets (using gets is a open invitation for buffer overflows)? void main?

Suggestion: You might check that examples you post are valid code and emphasize good programming practices.
April 22, 2003, 4:57 PM
MrRaza
i had it in my notes, i must of had some typo's, sorry about that...


btw, i forget what the standard is for the void main() function...

And you still get the point of what the example was trying to show. Besides a minor amount of errors on my part, it was a good example.
April 22, 2003, 5:07 PM
iago
Even if you fixed sky's errors, that still wouldn't compile because malloc returns a void* pointer, not a char*. This would be better:
s = (char*)malloc(80);

But that is still bad practice, this would actually be best:
s = (char*)malloc(sizeof(char) * 80);
And don't worry about that taking longer to run, because it won't, the compiler will hardcode in the result of that multiplication for you.
April 22, 2003, 5:22 PM
Arta
that may be the case, "sizeof(char)*80" is still silly though :P
April 22, 2003, 5:49 PM
Etheran
might as well use
s = (T_CHAR*)malloc(sizeof(T_CHAR)*80);
if you're going to do all that.
April 22, 2003, 7:52 PM
Eibro
register int? What a rare beast.
Nowadays, register is pretty much deprecated. Modern compilers will be able to optimize your C/C++ code much better than you will, these dinky little hints now serve no purpose. Optimizers know much more about generated code, so let them choose what would be best served as a register variable.
April 22, 2003, 9:13 PM
Yoni
Not to mention accessing a word (or byte, dword, qword, whatever) on the stack, which probably spends more time in your CPU's cache than in the RAM, is almost as fast as accessing a register, especially with modern CPUs.
April 22, 2003, 11:39 PM
iago
[quote author=Arta[vL] link=board=5;threadid=1123;start=0#msg8224 date=1051033777]
that may be the case, "sizeof(char)*80" is still silly though :P
[/quote]

It's true, but I like doing that anyway, just in case I later change it to an int or something silly like that :P
April 23, 2003, 2:47 AM
MrRaza
doing that ensures portability
April 23, 2003, 1:42 PM
St0rm.iD
Is there unicode C?
April 23, 2003, 11:11 PM
Yoni
Erm... Yes, ANSI C supports wide strings natively.
April 24, 2003, 12:32 AM

Search