tolower (static pointer + malloc + realloc)


 
Thread Tools Search this Thread
Top Forums Programming tolower (static pointer + malloc + realloc)
# 1  
Old 01-21-2010
tolower (static pointer + malloc + realloc)

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(OUTPUTsizeof(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;

# 2  
Old 01-22-2010
Code:
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));
    }

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'.

Code:
   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 !

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.

Cheers,
Loïc.
# 3  
Old 01-22-2010
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(OUTPUTstrlen(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++; 
    }

    
OUTPUT[COUNT] = '\0';
 
    return 
OUTPUT


Last edited by limmer; 01-31-2010 at 10:20 AM..
# 4  
Old 01-22-2010
What do you mean by static pointer?

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..
# 5  
Old 01-22-2010
Quote:
Originally Posted by limmer
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()...

Cheers,
Loïc.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Malloc to void pointer fails

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)
Discussion started by: rupeshkp728
5 Replies

2. UNIX for Dummies Questions & Answers

Awk Help - toupper/tolower

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)
Discussion started by: vanand420
6 Replies

3. Shell Programming and Scripting

toupper or tolower case of first letter of the line depending on another file

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)
Discussion started by: louisJ
16 Replies

4. Programming

realloc() fails

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)
Discussion started by: mamboknave
14 Replies

5. Programming

Even the Static cURL Library Isn't Static

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)
Discussion started by: BrandonShw
8 Replies

6. Programming

C++ program is crashing on re-assigning const static member variable using an int pointer

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)
Discussion started by: royalibrahim
6 Replies

7. Programming

malloc vs realloc

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)
Discussion started by: cyler
5 Replies

8. IP Networking

I need HELP to Set up Coyote Linux router with 1 static IP & 64 internal static IP

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)
Discussion started by: dlwoaud
0 Replies

9. Programming

pass a pointer-to-pointer, or return a pointer?

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

10. Programming

Realloc

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)
Discussion started by: wojtyla
0 Replies
Login or Register to Ask a Question