N00B here. This function would be easier using a char pointer along with free. But I wish to learn how to use char static pointers (they do not require free, right ?).
How do I erase the content of a static pointer ? Terminating the string works but the static pointer's content is not being erased, right ? Why is realloc not working like I want it to ?
PHP Code:
char *STRINGtoLOWERCASE(const char *IN) // converts all chars in string to lower case (will not work on non-ASCII chars) { static char *OUTPUT = NULL; if(OUTPUT == NULL) { OUTPUT = (char *)malloc(sizeof(IN)); }else{ OUTPUT[0] = '\0'; // Why will this not erase the string ??? OUTPUT = (char *)realloc(OUTPUT, sizeof(IN)); }
int COUNT = 0; while(IN[COUNT]) { OUTPUT[COUNT] = (char)(tolower(IN[COUNT])); // need to cast to (char) or it will return the ASCII value no the char value COUNT++; }
// OUTPUT[COUNT] = '\0'; // Commenting this shows that OUTPUT is not being erased !
return OUTPUT; }
int main(void) { // OUTPUT is not being erased... puts(STRINGtoLOWERCASE("HeLlO WoRlD")); // hello world�� puts(STRINGtoLOWERCASE("HeLlO")); // hello world�� puts(STRINGtoLOWERCASE("HeLlO WoRlD"));// hello world�� return 0; }
The malloc/realloc size is wrong. Note that sizeof(IN) is the size of a pointer to char (i.e. 4 or 8 bytes depending on your processor architecture), and not the size of the string. You need to use strlen().
OUTPUT[0]='\0' is not needed.
Note that realloc() doesn't set newly area to 0:
Quote:
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.
So you need to properly terminate the string with '\0'.
uncomment OUTPUT[COUNT]='\0'. string needs to be zero terminated in C.
Finally, it's unusual to write function or variable names in C USING UPPER CASE.
POST UPDATE: Thanks for the heads up on the strlen vs sizeof. It also needs to add 1 to include the string terminator. here is the final version:
PHP Code:
char *STRINGtoLOWERCASE(const char *IN) // converts all chars in string to lower case (will not work on non-ASCII chars) { static char *OUTPUT = NULL; if(OUTPUT == NULL) { OUTPUT = (char *)malloc(strlen(IN) + 1); }else{ OUTPUT = (char *)realloc(OUTPUT, strlen(IN) + 1); // I am still not sure if this actually is reallocating. Can you realloc a static pointer ? (In C, there is no standard function to find out the size of allocated memory.) }
int COUNT = 0; while(IN[COUNT]) { OUTPUT[COUNT] = (char)(tolower(IN[COUNT])); // need to cast to (char) or it will return the ASCII value no the char value COUNT++; }
Whether the pointer is static, const, or plaid green monkey has no effect on the memory it points to.
And I don't think your static variable there is doing what you think it does. You have the rough idea, it acts like a global, but I don't understand why you're using one here. Why preserve a pointer between function calls when each realloc() would almost certainly invalidate every pointer given before by STRINGtoLOWERCASE anyway?
Never call realloc() or free() on memory that didn't ultimately come from a calloc(), malloc(), or strdup(). This is true no matter what kind of pointer you store it in -- the realloc() call really has no way to tell the difference. If it's failing it's failing for other reasons.
Last edited by Corona688; 01-22-2010 at 10:50 AM..
POST UPDATE: Thanks for the heads up on the strlen vs sizeof. It also needs to add 1 to include the string terminator. here is the final version:
In your code, char* OUTPUT should be declared static. Otherwise, you shall run into serious troubles.
Quote:
Originally Posted by corona688
And I don't think your static variable there is doing what you think it does. You have the rough idea, it acts like a global, but I don't understand why you're using one here. Why preserve a pointer between function calls when each realloc() would almost certainly invalidate every pointer given before by STRINGtoLOWERCASE anyway?
Never call realloc() or free() on memory that didn't ultimately come from a calloc(), malloc(), or strdup(). This is true no matter what kind of pointer you store it in -- the realloc() call really has no way to tell the difference. If it's failing it's failing for other reasons.
Your second paragraph just explain why it has to be static if you go for a realloc! It is true that realloc invalidates the pointer, but to do so it needs first a valid pointer that comes from a realloc() or malloc()...
I have a function to which I will pass a struct ID and it will return me a string.
I will pass a pointer to store the name string and that pointer will be allocated memory by the function called.
int ConvertIDToName(void *id, void *name, size_t *size)
{
int status = 0;
... (5 Replies)
Hi,
I am learning awk and faced few queries. Kindly suggest on the same. Where it is wrong.
$ awk '{if (toupper($1) ~ /a/) print $0}' inv
$ awk '{if (toupper($1) ~ /A/) print $0}' inv -- Why this output
Jan 13 25 15 115
Mar 15 24 34 228
Apr 31 52 63 420
May 16 34 29 208... (6 Replies)
Hi
I would like to read if the first letter of a line in a first file (gauche.txt) is uppercase or lowercase, and change consequently the first letter of the corresponding line in the second file (droiteInit.txt).
I have done this but it won't work (I launch this using gawk -f... (16 Replies)
Not sure in which forum to post this. I'm trying here, in Programming.
I'm working on a PC with Intel Duo processor & 2GB of ram. OS is Ubuntu 10.04.
I'm having problems with a C++ program that makes extensive use of realloc(). It happens that as soon as the overall memory allocated(OS +... (14 Replies)
I'm writing a program which uses curl to be run on Linux PCs which will be used by a number of different users. I cannot make the users all install curl on their individual machines, so I have tried to link curl in statically, rather than using libcurl.so. I downloaded the source and created a... (8 Replies)
Hi,
Can any one tell me why my following program is crashing?
#include <iostream>
using namespace std;
class CA {
public:
const static int i;
};
const int CA::i = 10;
int main() {
int* pi = const_cast<int*>(&CA::i);
*pi = 9;
cout << CA::i << endl;
} (6 Replies)
Why when using realloc, john is reversed 3 times but not the other 2 names ? But if I use malloc, then the 3 names are reversed correctly ? (but then there is a memory leak)
How can I reverse all 3 names without a memory leak ?
char *BUFFER = NULL;
char *STRREVERSE(const char *STRING)
{... (5 Replies)
hello, i need help on setting my coyote linux, i've working on this for last 5 days, can't get it to work. I've been posting this message to coyote forum, and other linux forum, but haven't get any answer yet. Hope someone here can help me...... please see my attached picture first.
... (0 Replies)
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)
Can Any body give me a exampla which has the usage of realloc
i want a function which uses realloc & increases /decreases the size of a pointer (0 Replies)