|
Why bother? Returning a temporary would trouble you anyway.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
akira32 wrote: In getA, I can get 1-dimension array.
The first thing here is that you're not "get[ing] 1-dimension array." you're returning the address of the first element of where a 1 dimensional array was before the function returned. Depending on what you do with the pointer after the function returns you could crash your program or even leave it with a big security hole.
The bad news is that you can't return an array of any sort directly from a function. The slightly better news is that you can return a structure containing an array, e.g:
struct array_wrapper
{
int data[10];
};
struct array_wrapper get_array()
{
struct array_wrapper a;
a.data[5] = 100;
return a;
}
Quick caveat: I don't program in C anymore so I may have got the syntax a bit wrong.
Cheers,
Ash
Edited to fix slightly knackered code formatting
modified on Friday, May 14, 2010 5:54 PM
|
|
|
|
|
Both these calls leave you with memory leaks. If you want to allocate arrays to be returned from a function you should use one of the memory allocation functions, malloc() or new .
It's time for a new signature.
|
|
|
|
|
Hi,
Sorry to sound picky but it's important for C programmers to understand these things...
The original functions the original author posted didn't leak anything (well, the one that would have compiled anyway) - all the memory allocated for his arrays on the stack were cleaned up when the functions returned.
The problem he would have caused was actually the reverse of a leak: continuing to access memory that's been marked as inaccessible for some reason. This is usually called a "dangling pointer" as it's pointing somewhere it shouldn't. In this case it's pointing to a block of memory on the stack and writing to it could cause all sorts of fun and merriment.
Cheers,
Ash
|
|
|
|
|
You shouldn't be returning an array, the best thing is to accept a pointer to an array allocated by the caller (plus dimensions) and then fill it. That way you'll avoid the problem of who has to do the cleanup of the memory. For example this function will fill the array with random values:
int foo (int* dstArr, int m, int n)
{
for (int i=0;i<m;i++)
for (int j=0;j<n;j++)
dstArr[i*n+j] = rand();
}
int main()
{
int arr[3][5], arr2[10][10];
foo((int*)arr, 3, 5);
foo((int*)arr2, 10, 10);
}
There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition.
Blaise Pascal
|
|
|
|
|
|
Hi all,
This project is using C++ with MFC with Visual Studio 2005.
I'm converting over an old WinCE project which used to use SHSipPreference and some keyboard dlls called NumSip and AlphaNumSip which essentially popped up a virtual keyboard on the PDA.
I'm trying to find something similar for WinXP that I can use instead. I need an ability to control the keyboard from within the program, tell it when to pop up and close, etc.
Aside from just writing a virtual keyboard from scratch (which would work fine but I don't have a lot of time to do this), does anyone know of an existing API/lib/code or anything that does this already?
Thanks!
KR
|
|
|
|
|
Hi I am using writeprivateprofilestring function to update two files.
Suppose Two files are A.ini and B.ini.
First we call writeprivateprofilestring for A.ini by and After
call writeprivateprofilestring for B.ini. Both file value update successfully.
but after call writeprivateprofilestring for b.ini and system is being restart then
Only second file B.ini value is update but A.ini value is not updated
|
|
|
|
|
shivanandgupta wrote: Both file value update successfully.
How are you verifying this?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
function do well.
but in case power restart then first value is not updated
but second value is updated.
|
|
|
|
|
Hi all,
i m using Rapi function ro connect with Windows based mobile phone.
but after analysis i found this function generate problem in handling of CFilDialog,
when i use CFileDialog with Rapi function than some times CFileDialog crash with AccessViolation error.
if i use CFileDialog without Rapi Functions,than its working fine in same conditions and situations.
please help me,and provide me guidance for this.
thanks in advance.
|
|
|
|
|
Le@rner wrote: please help me,and provide me guidance for this.
You should better use your debugger for this purpose. This will give you way more information than what we could.
|
|
|
|
|
Its not crash when i run it from code,its generate problem when i run application from exe.
|
|
|
|
|
Obviously having this work under a true cross platform umbrella is beyond the topic of this message. Right now I'm curious on just how hard or if anyone has been successful in taking some of the most excellent MFC controls found here and converted them to work under wxWidgets. I'm particularly interested in the XListCtrl since that is basically a control that always wows me and I feel having something like available under wxWidgets would be awesome.
I'm not asking that others do it for me, I'm just curious on if anyone has tips on where to start first and the best method to tackle it. If it's not possible or requires to completely re-invent the wheel, then maybe it's a bit over my head.
Thanks!
|
|
|
|
|
I've been playing with wxWidgets for a year or so now but mostly just to have a GUI interface for testing my machine vision tools. I used MFC for a long time before. It should be possible to do it since it's all just programming and there seems to be a very high correspondence between MFC message handlers and the handlers in wxWidgets.
I'm not familiar with the control you mentioned but a potential problem is where the MFC class is just a wrapper for a Window's control. In that case if you're looking for a truly cross platform solution, then it gets a lot tougher since the underlying control won't exist on other platforms or if a similar control does exist, you'll have to do essentially multiple implementaions under the covers to make it transparent at the top level.
MFC being Windows specific allows you get get to and use the underlying Windows API pretty easily and depending on how much that has been done may cause problems, too.
The wonderful thing about the Darwin Awards is that everyone wins, especially the members of the audience.
|
|
|
|
|
Hello;
I am a student in software engineering. I should do a project that is maze.
a game in console32 mode that define with an 20*40 array. walls and blocks in this game should be given as an input. a 2 character worm should start from a place and finish the game in another place. This program should have a timer.
CAN ANYONE HELP ME ?!?!?
<<<i've no="" word="" to="" express="" my="" seep="" gratitude="" from="" anyone="" who="" help="" me="" in="" this="" hard="" work="">>>
|
|
|
|
|
What is your trouble about? What have you done so far? Please ask specific questions.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
CPallini wrote: What have you done so far?
Are you new around here? He has copied his homework assignment to this forum, just like all the others do, like it or not. Now, can you finish it by Monday morning? it is due in the second period.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
plz gimme codez or u shudup!
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
a-maze-ingly no.
unless you show us that you at least did something, wrote a bit of code, analyzed the problem, ...
Watched code never compiles.
|
|
|
|
|
hasani2007 wrote: CAN ANYONE HELP ME ?!?!?
Your teacher?
I'm being serious. I would hope the project is within the abilities of someone who's been paying attention during their course.
Is this an idea you thought of yourself? Is it specified homework?
Break your problem into managable bits. Can you write a console program? Have you done a timer? Refresh the screen? I wouldn't use a console for a game, but still... [*]
Can you parse input?
All these questions should be answerable for you. If not, you've chosen too challengina project, or you need to reread your notes.
Good luck,
Iain.
[*] Not since I was using vt102 terminals, anyway...
I have now moved to Sweden for love (awwww).
|
|
|
|
|
hasani2007 wrote: CAN ANYONE HELP ME ?!?!?
With what? Be specific. It is your project after all.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
Hi all,
I'm new to JavaScript and Google Maps API. I got some problems but I cannot find out the reason. Hope someone can help me.
What I tried to do:
I'm trying to develop an application in MFC (Visual Studio 2005) with a html dialog showing a Google map. I will send location data from the MFC application to a JS function to calculate directions. Another JS function will return the number of routes and steps in the GDirection object. The result will be sent back as a message string. The codes I used for the HTML dialog was downloaded from CodeProject.
The problem I encountered:
If I open the html file using IE and manually input the location data and use alert function to show the message, everything works well. I can also send the location data from MFC to JS. However, after that, I cannot get the route number back... It always returns the number as "undefined".
Some codes:
function initialize() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map_canvas"));
gdir = new GDirections(map, document.getElementById("directions"));
GEvent.addListener(gdir, "load", onGDirectionsLoad);
GEvent.addListener(gdir, "error", handleErrors);
}
}
function UpdateGPS(ar) // this is where MFC send the location data to JS
{
var a = new VBArray(ar);
var b = a.toArray();
rExp = /(\w|\s)*[^@]/ig;
var res = b[0].match(rExp); // Name @ size @ free_space
lan = parseInt(res[1]) / 1000000;
lon = parseInt(res[2]) / 1000000;
str = lan + "," + lon;
setDirections(str, "1.300983,103.772704");
}
function getRoute() // this is where JS should send information back to MFC
{
return message;
}
function setDirections(fromAddress, toAddress)
{
gdir.load("from: " + fromAddress + " to: " + toAddress, {getPolyline:true, getSteps:true, travelMode:G_TRAVEL_MODE_WALKING});
}
function onGDirectionsLoad()
{
n_route = gdir.getNumRoutes();
message = str + ". we have " + n_route + " route.";
alert(message);
}
Can someone help me here? Thanks a lot!
|
|
|
|
|
HI,
I got some code to load png image in button but it is giving error:
Could you suggest what might be the reason?
HBITMAP hBitmap;
Bitmap* bitmapfile = new Bitmap(L"test.png");
Status status = bitmapfile->GetHBITMAP( NULL, &hBitmap );
::SendMessage(m_imageBtn.GetSafeHwnd(),BM_SETIMAGE,(LPARAM)(DWORD)hHandle,0);
Error:"'Gdiplus::GdiplusBase::operator new' : function does not take 3 arguments"....
|
|
|
|
|
Googling I've found this [^] page...
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|