- 4,650
- 39
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?