memory allocate thing

[b][red]This message was edited by bluj91 at 2006-9-7 19:11:55[/red][/b][hr]
Dodgey subject line ay.

okay I was just wondering if the following is a valid way of doing this

[code]
#include
#include

struct linkedList
{
struct linkedList *node;
char cell;
};

struct base
{
unsigned i : 32;
struct linkedList start, *finish;
};

int main(void)
{
char buffer;
struct base keyList = {0,{0,''},&(keyList.start)};
do
{
buffer = getchar();
if (buffer != '
' && buffer != '
')
{
keyList.finish->node = malloc(sizeof(struct linkedList));
keyList.finish->cell = buffer;
keyList.finish = keyList.finish->node;
keyList.i ++;
}
keyList.finish->node = 0;
keyList.finish->cell = 0;

}
while (buffer != '
' && buffer != '
');
char *String = malloc(sizeof(char)*keyList.i);
unsigned long long j = 0;
struct linkedList *Agent = &(keyList.start);
for (j = 0; j <= keyList.i; ++ j)
{
String[j] = Agent->cell;
Agent = Agent->node;
}
printf("


You entered:> %s
", String);
system("pause");
return 0;
}
[/code]

basically I am just trying to work out a way to dynamically read in a string rather than using a static amount of memory. I am just wondering if there is a simpler way of doing this. Also note that this method should only contain expressions from ANSI C, not C++ or visual C.

Cheers.


Comments

  • Also note that this method should only contain expressions from ANSI C, not C++ or visual C.
    :
    : Cheers.
    :
    :
    :
    Pull all your local declarations to the top then. The implementation is OK otherwise, but you may want to lose the linked list if it is not needed.
  • : Also note that this method should only contain expressions from ANSI C, not C++ or visual C.
    : :
    : : Cheers.
    : :
    : :
    : :
    : Pull all your local declarations to the top then. The implementation is OK otherwise, but you may want to lose the linked list if it is not needed.
    :


    Yep. Further issues:

    unsigned long long is C99 which is still a draft standard, and therefore maybe shouldn't be counted as "ANSI C" yet, since most compilers haven't implemented it.

    Also, bitfields containing other types than int is undefined behaviour in ANSI C. Mixing an int with two pointers in the same struct will likely give you strange problems when porting to another compiler. I don't see why you would want a single variable of 32 bits in a bitfield though, so maybe you could just skip it.
  • [blue]Linked list is an overkill here - just use the good old realloc(). This function is not tested, but try it - it may work. Also, you need to free() the returned value after using your input string. And no backspacing here - can you add it?..[/blue]
    [code]
    char* InputDynamicString ()
    {
    int room, ch, col=0;
    char* s = malloc (room=32);

    do
    {
    ch = getch ();
    if (ch == '
    ')
    {
    [green][b]// ENTER pressed, so finish the string[/b][/green]
    s [col] = '';
    break;
    }

    s [col++] = ch; [green][b]// Store symbol into buffer[/b][/green]
    putch (ch); [green][b]// echo symbol to screen[/b][/green]

    if (col == room-1)
    {
    [green][b]// time to get more memory[/b][/green]
    s = realloc (s, room+32);
    if (! s)
    {
    [green][b]// no more memory, so finish the string[/b][/green]
    s [col] = '';
    break;
    }

    [green][b]// we have more, continue![/b][/green]
    room += 32;
    }
    }
    return s;
    }
    [/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories