- #1
- 4,652
- 38
I am not sure I wrote this correctly, but I am trying to declare an array of 12 pointers to arrays of 50 characters:
char *names[12] [50]
Does this look OK? Thanks.
char *names[12] [50]
Does this look OK? Thanks.
#include <stdio.h>
#include <string.h>
int main (void)
{
char ArrayOne[] = "Run, Spot, Run!";
char *ArrayTwo=(char*)malloc(strlen(ArrayOne));
return 0;
}
TenaliRaman said:Yes malloc allocates memory.
Also yes u need #include<string.h> for using strlen().
all the string related functions are defined in string.h
these string functions include strlen,strcmp,strcmpi,strtok,strstr and so on...
-- AI
char ArrayOne[] = "Run, Spot, Run!";
int length =strlen(ArrayOne);
char ArrayTwo[length];
plover said:"Garbage Collection" in a computer program is an automatic system for detecting when pieces of allocated memory are no longer being used and automatically releasing them to the pool of unused memory.
When a C program starts the run-time environment establishes a section of memory called "the heap"; routines that allocate memory dynamically (such as malloc) find an unused chunk of the heap, mark it as "in-use", and return its address to the user. This chunk of memory is now unavailable for any other use until it is released.
Non-dynamic uses of memory (such as variables declared within functions) are not a problem. Memory for these is allocated in a different fashion that ensures that the memory is released at the end of the function. But one common reason for allocating memory dynamically is that
it may be unknown how long the program will require a particular piece of data.
So THAT'S what a memory leak is! I never knew what that meant.A chunk of memory from the heap becomes completely inaccessible, and thus unrecoverable, when there are no more pointers to the chunk. (Either because the last pointer was assigned to point somewhere else, or simply vanished at the end of a function.) At this point, if a system has garbage collection, it will notice that the chunk of memory has been orphaned and make sure it is marked as available again. In a non-garbage collecting environment, like most C and C++ environments, allowing this to happen is what programmers call a "memory leak", i.e. usable memory is "leaking" out of your heap.
The mechanism to deal with this in standard C is a function called "free" (which should be detailed alongside malloc in your documentation). When finished with a dynamically allocated chunk of memory, you pass a pointer to the chunk as an argument to "free" and the chunk is returned to the heap.
While the C standard does not specify any facilities for garbage collection, there is no reason why a given compiler/development environment can't include one. It will, however, most likely be specific to that environment, so if it is important that your code be portable, it is a bad idea to rely on such a facility.
chroot said:At this point in your coding, don't worry about garbage collection yet -- you're not really even using any dynamic memory allocation. You should just use whatever development environment your university suggests that you use.
When a program starts up, it receives memory from the operating system. There needs to be enough memory for at least the following:Math Is Hard said:That's very interesting. How big is a heap?
Yes...So you are saying for maximum portability, it's best that I do the memory management manually?
An array of pointers to arrays of characters in C is a data structure that allows you to store multiple strings in a single variable. It is commonly used for storing and manipulating strings in C programming.
To declare an array of pointers to arrays of characters in C, you can use the following syntax: char *array_name[size];
where array_name
is the name of your array and size
is the number of strings you want to store.
You can initialize an array of pointers to arrays of characters in C by assigning each element of the array to a string literal or a variable containing a string. For example: char *array_name[] = {"Hello", "World", "!"};
or char *array_name[] = {str1, str2, str3};
where str1
, str2
, and str3
are variables containing strings.
To access elements in an array of pointers to arrays of characters in C, you can use the array index notation. For example, to access the first string in the array, you can use array_name[0]
. To access a specific character in a string, you can use the string index notation. For example, to access the third character in the first string, you can use array_name[0][2]
.
To free memory allocated for an array of pointers to arrays of characters in C, you need to first free each individual string in the array using the free()
function. Then, you can use the free()
function again to free the memory allocated for the array itself. It is important to free memory in this order to avoid memory leaks.