10-19-2009
broli,
C does not hide memory pointer information.
"asdf" is a "string literal."
when initializing a char*, "asdf" is seen by the program as a pointer to that location in memory.
The same is not true for other basic types.
int *Bogus = { 0, 1, 2, 3, 4}; - does not work
int Correct[5] = { 0, 1, 2, 3, 4}; does work
malloc() allocates memory on a global heap, and requires a type cast.
using [] allocates memory locally, and the size of this chunk is limited, so don't use brackets for really big arrays.
Two operators will come in handy all the time... "*" and "&"
10 More Discussions You Might Find Interesting
1. Programming
void main()
{
int a={1,2,3,4,5,6,7,8,9,10};
int *p=a;
int *q=&a;
cout<<q-p+1<<endl;
}
The output is 10, how?
if we give cout<<q it will print the address, value won't print....
if we give cout<<p it will print the address, value won't print....
p has the base addr; q... (1 Reply)
Discussion started by: sarwan
1 Replies
2. Programming
what is far pointer in C (1 Reply)
Discussion started by: useless79
1 Replies
3. What is on Your Mind?
All Sys Administrators,
With due respect I would like to know what should be BEST Things to
do when LEAVING one job , and what Precaution MUST be taken
while taking over new JOB?? Please Discuss in detail the STEP to be
taken for both the TIME ?? (3 Replies)
Discussion started by: vakharia Mahesh
3 Replies
4. Programming
If one wants to get a start address of a array or a string or a block of memory via a function, there are at least two methods to achieve it:
(1) one is to pass a pointer-to-pointer parameter, like:
int my_malloc(int size, char **pmem)
{
*pmem=(char *)malloc(size);
if(*pmem==NULL)... (11 Replies)
Discussion started by: aaronwong
11 Replies
5. Homework & Coursework Questions
1. List commands
to create the directory hierarchy $HOME/a/b/c
in vi to replace all occurences of TMP with tmp in lines 1 through 10
in vi to replace first occurence of CPU_file with DISK_file at line 15
2. Explain with a very simple example, usage of "ls -a"
3. What do the... (2 Replies)
Discussion started by: jessesaini
2 Replies
6. Shell Programming and Scripting
Q-1
Write a shell script in Unix that lists files from your current working directory
· By modification time when called with lm
· By access time when called with la.
By default, the script should show the listing of all the files in the current directory.
Q-2
Write a... (1 Reply)
Discussion started by: Vishank Parikh
1 Replies
7. Homework & Coursework Questions
Q-1
Write a shell script in Unix that lists files from your current working directory
· By modification time when called with lm
· By access time when called with la.
By default, the script should show the listing of all the files in the current directory.
Q-2
Write a shell script which... (1 Reply)
Discussion started by: Vishank Parikh
1 Replies
8. Programming
When I compile the below code, I am getting error as
template<typename T> T AddFun(T i, T j) {
return i + j;
}
template<> T* AddFun<T*>(T* i, T* j) {
return new T(*i + *j);
}
int main() {
int n = AddFun<int>(10, 20);
int i = 10, j = 20;
int* p = AddFun<int*>(&i,... (1 Reply)
Discussion started by: royalibrahim
1 Replies
9. Homework & Coursework Questions
Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!
1. The problem statement, all variables and given/known data:
In the command line I will be given files or words which will be USER ID's or a file of USER ID's. I am to let... (4 Replies)
Discussion started by: ByronicX
4 Replies
10. UNIX for Dummies Questions & Answers
Hello all im currently working on this assignment and a little stump on how to check for an argument heres the instructions:
Step 4:
Modify your script so that if there is an argument called TestError you display the following error message with your usage statement.
TestError found
Example:... (1 Reply)
Discussion started by: bsn3971
1 Replies
LEARN ABOUT REDHAT
malloc
MALLOC(3) Linux Programmer's Manual MALLOC(3)
NAME
calloc, malloc, free, realloc - Allocate and free dynamic memory
SYNOPSIS
#include <stdlib.h>
void *calloc(size_t nmemb, size_t size);
void *malloc(size_t size);
void free(void *ptr);
void *realloc(void *ptr, size_t size);
DESCRIPTION
calloc() allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the allocated memory. The memory is
set to zero.
malloc() allocates size bytes and returns a pointer to the allocated memory. The memory is not cleared.
free() frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc() or realloc(). Oth-
erwise, or if free(ptr) has already been called before, undefined behaviour occurs. If ptr is NULL, no operation is performed.
realloc() changes the size of the memory block pointed to by ptr to size bytes. The contents will be unchanged to the minimum of the old
and new sizes; newly allocated memory will be uninitialized. If ptr is NULL, the call is equivalent to malloc(size); if size is equal to
zero, the call is equivalent to free(ptr). Unless ptr is NULL, it must have been returned by an earlier call to malloc(), calloc() or
realloc().
RETURN VALUE
For calloc() and malloc(), the value returned is a pointer to the allocated memory, which is suitably aligned for any kind of variable, or
NULL if the request fails.
free() returns no value.
realloc() returns a pointer to the newly allocated memory, which is suitably aligned for any kind of variable and may be different from
ptr, or NULL if the request fails. If size was equal to 0, either NULL or a pointer suitable to be passed to free() is returned. If real-
loc() fails the original block is left untouched - it is not freed or moved.
CONFORMING TO
ANSI-C
SEE ALSO
brk(2), posix_memalign(3)
NOTES
The Unix98 standard requires malloc(), calloc(), and realloc() to set errno to ENOMEM upon failure. Glibc assumes that this is done (and
the glibc versions of these routines do this); if you use a private malloc implementation that does not set errno, then certain library
routines may fail without having a reason in errno.
Crashes in malloc(), free() or realloc() are almost always related to heap corruption, such as overflowing an allocated chunk or freeing
the same pointer twice.
Recent versions of Linux libc (later than 5.4.23) and GNU libc (2.x) include a malloc implementation which is tunable via environment vari-
ables. When MALLOC_CHECK_ is set, a special (less efficient) implementation is used which is designed to be tolerant against simple
errors, such as double calls of free() with the same argument, or overruns of a single byte (off-by-one bugs). Not all such errors can be
protected against, however, and memory leaks can result. If MALLOC_CHECK_ is set to 0, any detected heap corruption is silently ignored;
if set to 1, a diagnostic is printed on stderr; if set to 2, abort() is called immediately. This can be useful because otherwise a crash
may happen much later, and the true cause for the problem is then very hard to track down.
Linux follows an optimistic memory allocation strategy. This means that when malloc() returns non-NULL there is no guarantee that the mem-
ory really is available. In case it turns out that the system is out of memory, one or more processes will be killed by the infamous OOM
killer.
GNU
1993-04-04 MALLOC(3)