Thread: Strings and Dynamic Memory

  1. #1
    Registered User
    Join Date
    Oct 2008
    Location
    Turkey
    Posts
    1

    Strings and Dynamic Memory

    Hi, I'm in trouble with strings and pointers. It's a long message, but if anyone could read and say something, I'd be pleased.

    1)
    Code:
    main() {
    char *a;
    printf("%d", strlen(a));
    }
    This code prints "0". But if I declare another char b[10] next to it, it begins printing 11. In this case, if I declare a as char *a="", again it prints "0". I used Codeblocs with Mingw here. And if I move the char line with or without b out of main(), it crashes in the beginning of runtime.

    It acts different in DevC++. If "char *a;" is in main, it simply crashes in runtime. If I add b[10], it prints "0". If I move the char line with or without b out of main(), it still crashes. The problem is solved if a is initialized as ="".

    Why do these compilers act different? And what causes this problem? I think it's because of the lack of initialization; but I saw somebody saying "You don't need". Which is true, and can i initialize a string or a "pointer to string" as a=""? Does it cost 0 byte?

    2) A second problem, with dynamic memory allocation. I want to get some text from the user, and assign it to an array of characters. It has to be done dynamically. I want to get all the characters one by one, until it is '\n', and allocate place for one more char every time and assign. Here's a code I tried to do this, but I think it's far away from doing the job.

    Code:
    main() {
    char *a = "\0", *b = "\0";
    char c;
    int i = 0;
    while ((c = getchar()) != '\n') {
    b = (char *)malloc((strlen(a) + 1) * sizeof(char));
    strcpy(b, a);
    b[strlen(a)] = c;
    b[strlen(b)] = '\0';
    free(a);
    a = (char *)malloc(strlen(b) * sizeof(char));
    strcpy(a, b);
    free(b);
    }
    a[strlen(a)] = '\0';
    printf("%s, %d, %d", a, strlen(a), strlen(b));
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. "You don't need" initialization IF the first time you use *a you assign something meaningful to it. Since that's not true in your case, you need initialization.

    In case you were wondering, Dev-C++ also uses MinGW, so they're both using the same compiler. It would have to come down to the libraries -- I guess you have Dev-C++ set to use the "debug runtime" which silently initializes things to 0 so that if you try to follow a pointer that you haven't defined, it crashes; while you have Code::Blocks set to use "release runtime", which just uses whatever was in that piece of memory when the program started.

    Now, when you make a variable global, it is automatically initialized to 0 (that's what C requires), so then your pointer really is NULL, and following a NULL pointer is guaranteed to crash.

    You can initialize a pointer with a string literal, yes, BUT you have to remember that you don't own the memory that is pointed to. So this won't work:
    Code:
    char *a = "bob";
    a[1] = 'b'; //crash! a does not point to writable memory
    To resize allocated memory, you need to use realloc.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's undefined behavior, simply put. It means anything can happen.
    And you need to indent your code. You can't possibly expect us to read that code mess!
    You also need to drop the implicit main: http://cpwiki.sourceforge.net/Implicit_main
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    I didn't manage to read all of the replies - it's late and my eyes are tired.

    However, you can use getchar, as you are doing, with a pointer and realloc, and reallocate memory for each char.

    However, this would be very inefficient. You should really start by allocating some set size, say.. 128 bytes or some such. You then use a counter to keep track of the number of characters that have been input, and if it reaches something like SET_SIZE-1, you reallocate and increase the size by another, say, 128 bytes. The increment can be smaller, if you want to preserve memory, but a few hundred bytes is nothing now that computers are hardly ever seen with less than two gigabytes of memory.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. beginner: dynamic array of strings
    By pc2-brazil in forum C++ Programming
    Replies: 10
    Last Post: 04-29-2008, 04:29 PM
  2. dynamic memory allocation on demand
    By oilrg in forum C Programming
    Replies: 5
    Last Post: 12-02-2007, 02:59 AM
  3. Concatenating strings (dynamic array using pointers)
    By Tankndozer in forum C Programming
    Replies: 8
    Last Post: 07-01-2004, 07:27 AM
  4. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM
  5. Inputing strings into a dynamic array
    By Nakeerb in forum C++ Programming
    Replies: 1
    Last Post: 10-12-2002, 02:38 AM

Tags for this Thread