Hello all,
I am a newbie to C, so please bear with me
I need to create an array in a function (a local variable) with size
of 1MB. Since local variables get stored on the stack, at run-time, I
get an error.
I tried using mallac() function. This function only works when I am
allocating memory to a char* and not a char[].
I wish to manipulate the string in my function, but I've heard that
when using char*, the value is not meant to be altered. Where as if I
use char[], I can change the value as many times as I like (which I
want to).
Could someone please tell me how to properly allocate memory to a
local variable (string), so I can copy a string to this variable, and
manipulate it any way and how ever many times I wish.
Thanks in advance,
mike79
Use the following code as an example:
#include <stdlib.h>
#include <string.h>
int main (void)
{
char *a = "Hello";
char b[] = "Hello";
char c[8] = "Hello";
char d[8];
char *e;
strcpy (d, "Goodbye");
e = malloc (1024);
if (e)
{
strcpy (e, a);
strcat (e, d)
}
a = d;
}
Here's a hypothetical memory map to illustrate the concepts below. It
assumes that all instances of the string literal "Hello" map to the
same address; that may not be universally true, but we'll assume it
for this model. This is not meant to correspond to any real memory
model, just to demonstrate what's going on in the code:
Item Address 00 01 02 03 04 05 06 07
---- -------
"Hello" 0x00000100 'H' 'e' 'l' 'l' 'o' 0 .. ..
...
a 0x00400000 00 00 01 00 .. .. .. ..
b 0x00400008 'H' 'e' 'l' 'l' 'o' 0 .. ..
c 0x00400010 'H' 'e' 'l' 'l' 'o' 0 .. ..
d 0x00400018 .. .. .. .. .. .. .. ..
e 0x00400020 .. .. .. .. .. .. .. ..
This is what memory looks like right before the first strcpy().
The string literal "Hello" is stored as an array of 6 characters with
contents {'H', 'e', 'l', 'l', 'o', 0} with static extent, meaning the
memory for it is allocated at program startup, and is not released
until the program terminates. This memory may or may not be writable;
assume that it is not writeable.
The variable a is a pointer to char with auto extent, and it is
initialized with the address of the string literal "Hello" (a ==
0x00000100). Assume for this example that the address of a is
0x00400000. The *variable* a is writeable; you can always assign
another pointer value to it, such as a = d (a will contain the base
address of the d array, or 0x00400018), or a = "Goodbye". However,
what a initially *points to* (address 0x00000100) may not be writable,
so operations like strcpy (a, "Goodbye"); and a[0] = 'J'; may result
in undefined behavior.
The variable b is an array of char with auto extent, implicitly sized
to hold the string literal that is being used to initialize it (sizeof
b == 6), and initialized with the *contents* of the string literal.
The contents of b are writeable, so operations like strcpy (b, "Joe")
and b[0] = 'J'; will succeed. The *variable* b is not writeable; you
may not assign another address to it, so operations like b = a or b =
c are disallowed.
The variable c is an array of char with auto extent, explicitly sized
to hold 8 characters, and initialized with the contents of the string
literal. Behavior is the same as b.
The variable d is an array of char with auto extent, explicitly sized
to hold 8 characters. It has not been initialized, and must be
assumed to contain garbage. Behavior is the same as b and c. After
the call to strcpy, the contents of d are {'H', 'e', 'l', 'l', 'o', 0,
..., .., .., ..}, where '..' represent a random byte value.
The variable e is a pointer to char with auto extent. It has not been
initialized, and since it is auto, it may or may not be initialized to
NULL (assume it initially contains garbage). If the call to malloc()
succeeds, e will contain the address of a chunk of dynamically
allocated memory 1024 bytes long (assume address 0x08000000). This
memory will be writable, so operations like strcpy (e, "Hello"); and
e[0] = 'J'; will succeed. Like a, the *variable* e is writable, so
operations like e = b are permitted.
b, c, d, and e may be copied to as much as you want, as long as you
don't attempt to copy anything larger than what they're sized to hold.
Executing strcpy (b, "Goodbye"); may cause undefined behavior, since
the string being copied is longer than what b is sized to hold.
Here's what the memory looks when the program is about to exit:
Item Address 00 01 02 03 04 05 06 07
---- -------
"Hello" 0x00000100 'H' 'e' 'l' 'l' 'o' 0 .. ..
...
a 0x00400000 00 40 00 18 .. .. .. ..
b 0x00400008 'H' 'e' 'l' 'l' 'o' 0 .. ..
c 0x00400010 'H' 'e' 'l' 'l' 'o' 0 .. ..
d 0x00400018 'G' 'o' 'o' 'd' 'b' 'y' 'e' 0
e 0x00400020 08 00 00 00 .. .. .. ..
...
0x08000000 'H' 'e' 'l' 'l' 'o' 'G' 'o' 'o'
0x08000008 'd' 'b' 'y' 'e' 0 .. .. ..
...
0x08000400
Hope that helps.