ATV Connection Magazine

OT: C Programmers...I can't think today HELP!

(Click here to view the original thread with full colors/images)




Pages: 1


Posted by: shanebv2

I would guess that your problem is in another place, the time you will save might only matterin a real time system. For a webServer it should not make a difference ...

What is your responce time now and what do you need it to be ????

Posted by: pimp250r

Hopefully I can get an answer here, so programmers of many kinds should know what I'm talking about (C/C++, Assembly, Delphi, etc etc etc)
I've been beating myself up over this problem all day. I am trying to come up with a way to reduce processing time on my webserver for linux and
unix environments. In my webserver(I'll have a website up with the GPL source code up later) there on a few occasions where I receive
a string from an arguement. This arguement is then obviously copied to another (usually char *temp) , manipulated , and returned to
overwrite the original string. I am trying to reduce processing time and memory by manipulating pointers to point to the same allocated memory
space so it can be directly manipulated. Instead of using a strcpy() or something.
Here's what I have right now:

----------------------------- d_strcpy.c ------------------------------------
#include <stdio.h>
int main(void)
{
char *str;
char *dst;
printf("Current location of STR : %p\n", str);
printf("Current location of DST: %p\n", dst);
dst = str; /* changes the pointer location of dst to match str */
printf("Current location of STR : %p\n", str);
printf("Current location of DST : %p\n", dst);
return 0;
}

#gcc -o d_strcpy d_strcpy.c
(No optimizations, standard GCC compile)
Output:
# ./d_strcpy
Current location of STR : 0xbffff408
Current location of DST: 0x4000bcd0
Current location of STR : 0xbffff408
Current location of DST : 0xbffff408

So, this is exactly what I am looking for. DST now points to the same area as SRC but now I need a function to do it.
Apparently in the C language if you pass a pointer to a function, a temp variable is made for you and if you were to %p
the variable within that function you would only get the memory location of that temporary variable. This is what I am trying to bypass.
From what I hear, in C++ either by default or manually doing so the variable you pass to:

char *func(char *src)
the variable src would actually contain the & address of the variable passed. Is this true?
Is this possible in C or would it be easier to write it in C++ and use it as a precompiled object file for the webserver?

Any help or ideas will be a big big help!
Thanks,
Brad


Posted by: pimp250r

The function I have now for testing is looped 1,000,000 times (simply strcpy function of my own that carries a few more arguements)
and increments a variable each of those times for the counter obviously...

#time ./test
real .055s
user .040s

Those are my times right now. I figured with this new function it could be a fraction of that time. Do you think regardless of what function I use
all operations would be done in a single clock cycle anyway and it wouldn't matter?
I guess I am also trying to save memory by using a variable with an allocated memory spacer, then just a pointer that points to it.
Instead of using 2 variables both with allocated memory that contain the same data

Posted by: pimp250r

Bump

Posted by: pimp250r

Woooohooooo! I finally got it figured out.
Was right infront of me the entire time:

void swap(char **ptr1, char **ptr2)
{
*ptr2 = *ptr1;
}



int main(int argc, char *argv[])
{
char *str;
char *dst;
swap(&str,&dst);
return 0;
}