Sponsored Content
Full Discussion: Dynamic Memory Allocation
Top Forums Programming Dynamic Memory Allocation Post 302532169 by pludi on Monday 20th of June 2011 06:47:21 AM
Old 06-20-2011
Both are wrong. In the first case you allocate 20 bytes of space for a string. However, instead of saving a string in that space, you change the pointer to point to a static, non-mutable string. The only difference to the second example is that you don't allocate space that you'll never be able to free again.

The correct way would be this:
Code:
char *str = (char *) malloc(20 * sizeof(char));
strncpy(str, "This is a string", 20 * sizeof(char));

 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

memory allocation

I would like to know how I could allocate some more memory to a process. Please note that I am not the root user. (1 Reply)
Discussion started by: sagar
1 Replies

2. Programming

Dynamic memory allocation

Hi, I am trying to process line by line of a file. But I should not be allocating static allocation for reading the contents of the file. The memory should be dynamically allocated. The confusion here is how do I determine the size of each line, put it into a buffer with the memory allocated... (11 Replies)
Discussion started by: naan
11 Replies

3. Programming

array dynamic allocation

Hi, I have the following problem: i must allocate a dynamic array from a subroutine which should return such array to main function. The subroutine has already a return parameter so i thought of pass the array as I/O parameter. I tried the following program but it doesn't work (segmentation... (11 Replies)
Discussion started by: littleboyblu
11 Replies

4. Programming

Memory allocation problem

I have a program that will fetch some particular lines and store it in a buffer for further operations.The code which is given below works but with some errors.I couldn't trace out the error.Can anybody help on this plz?? #include <stdio.h> #include <stdlib.h> #include<string.h> #define... (1 Reply)
Discussion started by: vigneshinbox
1 Replies

5. Programming

global variables and dynamic allocation

Hi, is it possible in C to allocate dynamically a global variable?? (3 Replies)
Discussion started by: littleboyblu
3 Replies

6. Programming

Is there a problem with the memory allocation???

I have a scenario like the client has to search for the active server.There will be many servers.But not all server are active.And at a time not more than one server will be active. The client will be in active state always i.e, it should always search for an active server until it gets one.I... (1 Reply)
Discussion started by: vigneshinbox
1 Replies

7. Programming

dynamic allocation vs static allocation in c

i wrote a tiny version of tail command using a large buffer statically allocated but, in a second time, i found another version in which i use a bidimensional array dynamically allocated. here is the first version /*my tiny tail, it prints the last 5 line of a file */ #include<stdio.h>... (4 Replies)
Discussion started by: lucasclaus
4 Replies

8. Programming

Memory allocation in C

Hi Experts I need some help in static memory allocation in C. I have a program in which I declared 2 variables, one char array and one integer. I was little surprised to see the addresses of the variables. First: int x; char a; printf("%u %u\n', &x, a); I got the addresses displayed... (2 Replies)
Discussion started by: unx_freak
2 Replies

9. Shell Programming and Scripting

memory allocation to a variable

hello all.. i'm a beginner in shell scripting. I need to know what is really happening when we are creating a variable in shell scripting? how memory is allocated for that variable? (3 Replies)
Discussion started by: aarathy
3 Replies

10. Programming

C++/ROOT Memory Allocation?

Hello, I am new to C++ programming, so I'm still getting a feel for things. I recently wrote a simple C++ program (to be used as a ROOT Macro) to conduct a statistical analysis of a varied version of the Monty Hall problem (code below). Basically, the programs runs a few simple calculations to... (7 Replies)
Discussion started by: Tyler_92
7 Replies
Ns_String(3aolserver)					   AOLserver Library Procedures 				     Ns_String(3aolserver)

__________________________________________________________________________________________________________________________________________________

NAME
Ns_Match, Ns_NextWord, Ns_StrCaseFind, Ns_StrCopy, Ns_StrDup, Ns_StrNStr, Ns_StrToLower, Ns_StrToUpper, Ns_StrTrim, Ns_StrTrimLeft, Ns_Str- TrimRight, Ns_StringPrint, ns_strcopy, ns_strdup - library procedures SYNOPSIS
#include "ns.h" char * Ns_Match(char *a, char *b) char * Ns_NextWord(char *line) char * Ns_StrCaseFind(char *s1, char *s2) char * Ns_StrCopy(char *str) ns_strcopy(char *str) char * Ns_StrDup(char *str) ns_strdup(char *str) char * Ns_StrNStr(char *pattern, char *expression) char * Ns_StrToLower(char *string) char * Ns_StrToUpper(char *string) char * Ns_StrTrim(char *string) char * Ns_StrTrimLeft(char *string) char * Ns_StrTrimRight(char *string) void Ns_StringPrint(char *string) _________________________________________________________________ DESCRIPTION
Ns_Match(a, b) Compare the beginnings of two strings, case insensitively. The comparison stops when the end of the shorter string is reached. Return NULL if no match, b if match. Ns_NextWord(line) Return a pointer to first character of the next word in a string; words are separated by white space. The returned pointer points into the original string. For example, Ns_NextWord("abc def") returns a pointer to the 'd' in that string. Ns_StrCaseFind(s1, s2) Locate the first occurrence of substring s2 within string s1 in a case-insensitive manner. The terminating ' ' characters are not compared. Returns a pointer that points into s1. Uses strstr(3) to do its work. Ns_StrCopy(str) ns_strcopy(str) Copy a string or NULL value using Ns_Malloc. The Ns_StrCopy function is identical to the Ns_StrDup function but allows for the string parameter to be NULL, in which case Ns_StrCopy does nothing and returns NULL. Ns_StrDup(str) ns_strdup(str) Copy a string using Ns_Malloc. The Ns_StrDup function calls Ns_Malloc to allocate enough memory to make a copy of the given string. Ns_StrNStr(pattern, expression) Search through pattern for expression, case insensitively. Return a pointer that points to where the match begins within pattern, or NULL if expression is not contained in pattern. Ns_StrToLower(string) Convert string to lowercase. Returns pointer to original string. Original string will be modified. Ns_StrToUpper(string) Convert string to uppercase. Returns pointer to original string. Original string will be modified. Ns_StrTrim(string) Trim leading and trailing white space from string. A pointer to the trimmed string will be returned, which will be in the original string. Do not lose your original pointer to the string if you later need to free it. Ns_StrTrimLeft(string) Trim leading white space from string. A pointer to the trimmed string will be returned, which will be in the original string. Do not lose your original pointer to the string if you later need to free it. Ns_StrTrimRight(string) Trim trailing white space from string. The original string pointer will be returned, but the string will have been modified: a ' 's will have been placed right after the last non-whitespace character of the string. Ns_StringPrint(string) Print a string to stdout. SEE ALSO
nsd(1), info(n) KEYWORDS
AOLserver 4.0 Ns_String(3aolserver)
All times are GMT -4. The time now is 09:45 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy