Hi there,
I've written a function that 'converts' a structure of type timeval to
a c-string:
char *tv2Str(struct timeval tvX)
{
char caStr[20];
char *cpResult;
sprintf(caStr, "%ld.%ld", tvX.tv_sec, tvX.tv_usec);
cpResult = (char *)malloc(sizeof(char)*strlen(caStr));
strcpy(cpResult, caStr);
return cpResult;
}
I use it like this:
printf("%s", tv2Str((struct timeval){123, 456}) );
Question:
How (if at all) does the compiler (gcc) ensure that the memory
allocated in the above function is deallocated after the printf call ?
Thanks,
Cornel 4 2481
>char *tv2Str(struct timeval tvX) { char caStr[20]; char *cpResult;
sprintf(caStr, "%ld.%ld", tvX.tv_sec, tvX.tv_usec);
cpResult = (char *)malloc(sizeof(char)*strlen(caStr)); strcpy(cpResult, caStr);
return cpResult; }
I use it like this:
printf("%s", tv2Str((struct timeval){123, 456}) );
Question: How (if at all) does the compiler (gcc) ensure that the memory allocated in the above function is deallocated after the printf call ?
It does not. That's your responsibility, and you blew it.
You have a memory leak.
Gordon L. Burditt
Gordon Burditt wrote: char *tv2Str(struct timeval tvX) { char caStr[20]; char *cpResult;
sprintf(caStr, "%ld.%ld", tvX.tv_sec, tvX.tv_usec);
cpResult = (char *)malloc(sizeof(char)*strlen(caStr));
1) Lose the cast. It's not necessary -- and can possibly hide errors
(see the FAQ for further details).
2) sizeof(char) is _defined_ to be 1 -- so you don't need/want this term
3) If you're copying a C-string, always allocate strlen(orig) + 1, to
account for the trailing null.
strcpy(cpResult, caStr);
return cpResult; }
I use it like this:
printf("%s", tv2Str((struct timeval){123, 456}) );
Question: How (if at all) does the compiler (gcc) ensure that the memory allocated in the above function is deallocated after the printf call ?
It does not. That's your responsibility, and you blew it. You have a memory leak.
Yup. You do!
HTH,
--ag
--
Artie Gold -- Austin, Texas
Gordon Burditt wrote: [attribution to c.*****@bluewin.ch re-inserted]: char *tv2Str(struct timeval tvX) { char caStr[20]; char *cpResult;
sprintf(caStr, "%ld.%ld", tvX.tv_sec, tvX.tv_usec);
cpResult = (char *)malloc(sizeof(char)*strlen(caStr)); strcpy(cpResult, caStr);
return cpResult; }
I use it like this:
printf("%s", tv2Str((struct timeval){123, 456}) );
Question: How (if at all) does the compiler (gcc) ensure that the memory allocated in the above function is deallocated after the printf call ?
It does not. That's your responsibility, and you blew it. You have a memory leak.
Worse: he has undefined behavior because the memory
allocated to `cpResult' is one byte too small to hold the
data copied into it by strcpy(). And that's even ignoring
the possibility of malloc() failure, for which he's written
no test.
-- Er*********@sun.com
<c.*****@bluewin.ch> wrote in message news:16**************************@posting.google.c om... Hi there,
I've written a function that 'converts' a structure of type timeval to a c-string:
char *tv2Str(struct timeval tvX) { char caStr[20]; char *cpResult;
sprintf(caStr, "%ld.%ld", tvX.tv_sec, tvX.tv_usec);
cpResult = (char *)malloc(sizeof(char)*strlen(caStr)); strcpy(cpResult, caStr);
return cpResult; }
I use it like this:
printf("%s", tv2Str((struct timeval){123, 456}) );
Question: How (if at all) does the compiler (gcc) ensure that the memory allocated in the above function is deallocated after the printf call ?
Thanks, Cornel
No, it doesn't. It is memory leak
I think it is not common to allocate a block of memory and return it (because you can't free it).
If you want to retrun a string result, you can request a char* in the function argument and fill it.
char *tv2Str(struct timeval tvX, char *strResult)
{
sprintf(strResult, "%ld.%ld", tvX.tv_sec, tvX.tv_usec);
return strResult;
}
You have to provide the buffer to the call
char somebuffer[50];
printf("%s", tv2Str((struct timeval){123, 456}, somebuffer) );
--
BC This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Andreas Suurkuusk |
last post by:
Hi,
I just noticed your post in the "C# memory problem: no end for our problem?"
thread.
In the post you implied that I do not how the garbage collector works and
that I mislead people. Since...
|
by: Frank Esser |
last post by:
I am using SQL 8 Personal edition with sp2 applied. I set the max
server memory to 32MB and leave the min server memory at 0. When my
application starts hitting the database hard the memory usage...
|
by: John |
last post by:
Hi all:
When I run my code, I find that the memory that the code uses keeps
increasing.
I have a PC with 2G RAM running Debian linux. The code consumes 1.5G
memory by the time it finishes...
|
by: Franklin Lee |
last post by:
Hi All,
I use new to allocate some memory,even I doesn't use delete to release them.
When my Application exit, OS will release them.
Am I right?
If I'm right, how about Thread especally on...
|
by: xixi |
last post by:
hi, we are using db2 udb v8.1 for windows, i have changed the buffer
pool size to accommadate better performance, say size 200000, if i
have multiple connection to the same database from...
|
by: c.arnet |
last post by:
Hi there,
I've written a function that 'converts' a structure of type timeval to
a c-string:
char *tv2Str(struct timeval tvX)
{
char caStr;
char *cpResult;
|
by: tikcireviva |
last post by:
Hi Guys,
I've done a mulithread queue implementation on stl<queue>, my
developement environment is on VC6 as well as FC3. Let's talks about
the win32 side.
The suspected memory leak is find...
|
by: kumarmdb2 |
last post by:
Hi guys,
For last few days we are getting out of private memory error. We have a
development environment. We tried to figure out the problem but we
believe that it might be related to the OS...
|
by: Jean-Paul Calderone |
last post by:
On Tue, 22 Apr 2008 14:54:37 -0700 (PDT), yzghan@gmail.com wrote:
The test doesn't demonstrate any leaks. It does demonstrate that memory
usage can remain at or near peak memory usage even after...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |