C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-19-2008, 04:39 PM   #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));
}
grooveordie is offline   Reply With Quote
Old 10-19-2008, 04:47 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 10-20-2008, 09:19 AM   #3
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
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
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 10-20-2008, 02:08 PM   #4
Ex scientia vera
 
Join Date: Sep 2007
Posts: 426
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."
IceDane is offline   Reply With Quote
Reply

Tags
initialization, malloc, string

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 06:41 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22