|
Any reason why you can't use std::string?
You have a memory leak for unexName and you have delegated responsiblity to the caller to free partName.
|
|
|
|
|
hfry wrote: Any reason why you can't use std::string?
It's regular C.
hfry wrote: You have a memory leak for unexName and you have delegated responsiblity to the caller to free partName.
So... What can I do about it? Thanks! (Sorry if I sounded n00bish, but I am after all... )
Lord Kixdemp
www.SulfurMidis.com
www.SulfurSoft.tk
[ftp://][http://][hotline://]tsfc.ath.cx
-- modified at 10:26 Tuesday 13th June, 2006
|
|
|
|
|
Your problem:
a) strlen returns the length of the string WITHOUT null-termination.
leads to
b) you are allocating only enough memory for the "text", not for the terminator.
and
c) strncpy realizes that there is not enough room for the null-terminator (remember, strncopy copies AT MOST n characters - including the null terminator. If you had used strcpy, you would instead have written outside your allocated memory.
Solution: use (strlen(input)+1) for mallocing and strncpy.
Cheers,
Sebastian
Note: Before someone shouts "but the mtx extension is only 3 characters", let me add: there is a dot in there as well.
--
Contra vim mortem non est medicamen in hortem.
-- modified at 10:45 Tuesday 13th June, 2006
|
|
|
|
|
If there is a malloc, there must be a free. And you shouldn't return a pointer to a C string that you allocated on the heap as it's not usually obvious to the person using your function that it's a string that he must free.
I'd probably do something like this..
int getpartname(char *filename, char *partname, size_t partnamesize)
{
int dot = -1;
int filenamelen = strlen(filename);
int pos;
for(pos = filenamelen; pos >= 0; pos--)
{
if(filename[pos] == '.')
{
dot = pos;
break;
}
}
if(dot == -1) return -1;
if(partname == 0 || partnamesize < dot + 4 + 1)
{
if(partnamesize != 0) partname[0] = '\0';
return dot + 4 + 1;
}
strncpy(partname, filename, dot);
partname[dot] = '\0';
strcat(partname, ".000");
return 0;
}
The person using your function would have 2 options
char partname[80];
getpartname("is730enu.exe.mtx", partname, sizeof(partname));
char *partname;
int len = getpartname("is730enu.exe.mtx", 0, 0);
partname = (char*) malloc(len);
getpartname("is730enu.exe.mtx", partname, len);
: :
free(partname);
Oh, and just a friendly warning about strncpy...
http://blogs.msdn.com/oldnewthing/archive/2005/01/07/348437.aspx[^]
|
|
|
|
|
You should be able to use _splitpath and _makepath for this (not sure how it will handle the double extensions, but it should be okay).
Alternatively, you can copy the string to a new string and use strtok to parse the string and then reinsert the '.' where necessary.
For performance, if you don't need the middle name for anything, you could just replace the original characters with your new ones.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Zac Howland wrote: not sure how it will handle the double extensions...
Everything after the rightmost dot is considered the extension. All else, including additional dots, is considered the base name.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
That is what I thought, but I hadn't tried it with those types of filenames.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
<br />
strncpy(unexName,filename,strlen(filename)-4);<br />
unexName[strlen(filename)-4]='\0';<br />
strcpy(partName,unexName);<br />
strcat(partName,".000");<br />
printf("\n%s",partName);<br />
Try this and tell me if it worked.
<marquee scrollamount="1" scrolldelay="1" direction="up" height="10" step="1">--[ ]--
[My Current Status]
|
|
|
|
|
Crap... I'm not getting this at all... Doesn't free ing make the string NULL ? How can I return/pass a null string? The only reason I'm using malloc() is to make the char* long enough to contain the other one... And, if I allocate only a certain amount of memory for a string, and I try to add something that's larger than that, why does it add it anyways without crashing? C is confusing... I'd be using C# if this app didn't require to work on DOS. But, hfry, why parse the file myself if the extension is always 3 chars? (plus the dot) Thanks you all!
And: VuNic, that code crashed Winders.
[edit] A few minutes later...
Now I really AM confused. If I only copy 8 characters, look at what I get:
is730enucrosoft é
...crosoft? Why is Microsoft involved here?! I got no "Microsoft" string in my whole freaking program!
But if I copy 9:
is730en
I have never been so confused in my life...
[/edit]
Lord Kixdemp
www.SulfurMidis.com
www.SulfurSoft.tk
[ftp://][http://][hotline://]tsfc.ath.cx
-- modified at 13:49 Tuesday 13th June, 2006
|
|
|
|
|
Exactly what is wrong with the code that I have provided?
The point is you shouldn't pass the pointer out, instead the caller should pass a buffer in. The caller has an option of creating a buffer on the heap or on the stack. If the caller creates the buffer on the heap using malloc, then he would also know that he has to free it.
Extensions are not always 3 characters.
I try to add something that's larger than that, why does it add it anyways without crashing
There is no reason why writing past an allocated buffer must cause a crash. It largely depends on what you happen to be writing over. If you are writing past an allocated buffer on the heap, fun things might happen in totally unrelated portions of your application that happen to be using that memory that you overwritten.
|
|
|
|
|
hmm.. well Kixdemp, I had to leave office fast and didn't read your problem well, I gave that answer with a rough Idea of your problem. Dont worry today I'll sit down for it. You would be able to do it certainly
<marquee scrollamount="1" scrolldelay="1" direction="up" height="10" step="1">--[ ]--
[My Current Status]
|
|
|
|
|
hfry wrote: The point is you shouldn't pass the pointer out, instead the caller should pass a buffer in. The caller has an option of creating a buffer on the heap or on the stack. If the caller creates the buffer on the heap using malloc, then he would also know that he has to free it.
Oh... Now I understand why it's always done that way... Now, is that the source of the problem or can I leave that change (all my functions do it the other way...) for when I finish the program?
hfry wrote: Extensions are not always 3 characters.
In this program they are (*.mtx and *.000 thru *.fff)
VuNic: Alright!
Thanks you both!
Lord Kixdemp
www.SulfurMidis.com
www.SulfurSoft.tk
[ftp://][http://][hotline://]tsfc.ath.cx
|
|
|
|
|
Hi,
I am using Excel automation to plot the chart.I am not able to get a handle on the X-axis (primary), I already have a handle on the Chart (Because I can change the title/plot chart), I am trying to change X-axis property to Catagory(we can set X-axis to Automatic/Catagory/Time-Scale) using the following spinets
Axes axes;
lpDisp = chart.Axes( COleVariant((short)(2)), 1);
ASSERT(lpDisp);
axes.AttachDispatch(lpDisp);
////////////////Second way-------------------------
Axis axis;
lpDisp = axes.Item(2, 1);
ASSERT(lpDisp);
axis.AttachDispatch(lpDisp);
Does anyone know what I'm doing wrong?
Mike
|
|
|
|
|
Dears Sirs;
How can I make a color to my Formview
I learn my self
|
|
|
|
|
Talking in terms of VC7.0 or VS6.0?
In vc7.0 it's very easy just open the form view. Go to properties > Appearance >Backcolor and select your choice of color.
In am answering in the context because you asked for FormView
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
-- modified at 10:02 Tuesday 13th June, 2006
|
|
|
|
|
I am using Visual c++ PLease
|
|
|
|
|
Supposing you have an MFC application, I think you have to handle the WM_ERASEBKGND notification. Add a handler for this message in your frame class. Within the handler you will have a CDC object as a parameter. Use drawing functions to fill the frame with colour you prefer.
|
|
|
|
|
I don't see this handle : WM_ERASEBKGND
|
|
|
|
|
you can use WM_PAINT and use CPaintDC dc(this);
whitesky
|
|
|
|
|
Hi,
I m using Windows Timers in my application which has 4 threads. I need a separate timer for each thread. At most how many timers can I use in an application as in future I will update my application for 32 threads? Regards
Cyber Friend
|
|
|
|
|
Cyber Friend wrote: At most how many timers can I use in an application as in future I will update my application for 32 threads?
I'm not exactly sure. In any case, they are a limited resource. Check SetTimer() 's return value.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
The Following link have more about Timers which may resolve your problem for creating timers and limit for creating them.
Timers[^]
Knock out 't' from can't,
You can if you think you can
|
|
|
|
|
Windows resources are very precious. I recall one of the old applications in which the developer used around 20 timers, each to refresh different portion of the display. The refresh rate is 10-20 Hz. Whenever that program is run, it used up all 100% CPU. Of ocourse, the 20 timers have been later combined to one centralized, high-resolution timer.
In general, 4 threads should be fine, but I have concerns about 32 threads. The number does matter. Not just the number of timers, but also the number of thredas. Each thread (and timer) takes a lot of overheads. I don't know of your situation, but you might want to think again about the design.
- It's easier to make than to correct a mistake.
|
|
|
|
|
Hi,
Thanx for the repies from all of u.
Jun Du wrote: Whenever that program is run, it used up all 100% CPU. Of ocourse, the 20 timers have been later combined to one centralized, high-resolution timer.
Jun I m using timers first time in an application and dont know much about them. Can you plz mention the link of some article/tutorial about the usage of a "High-resolution timer", instead of so many timers.
Regards.
Cyber Friend
|
|
|
|
|
Search CodeProject for "multimedia timer".
- It's easier to make than to correct a mistake.
|
|
|
|