I've heard the curses library can be useful when trying to implement the handy DOS-only tools of gotoxy() and clrscr() using move() and initscr(). Though, there is a way to write your own gotoxy() in the Linux environment. This topic isn't touched often, so I would like to change that. I'll also show an example of how to use clrscr(), but it isn't our main focus.

Greetings,

String parsing isn't always an easy task. Especially in cases where you need to split a single string into a great multitude, but also accounting for maximum performance.

The following code presented does this task simply. Using precise allocation techniques perform greatly when writing an algorithm to precision.

Hello,

I am trying to determine the best possible solution to accomplish a simple, yet complex task. ([i]Regarding: Socket Programming[/i])

I've been working on a new project for quite some time and have written a custom framework from the ground up. It's more or less a ([b]Client --> Server --> Server <-- Client[/b]) system.

So far I have the basics done. Here's the process:
[list=1][]I expect to have one client connect to my listening server.
[
]That client will periodically send me a string of IP Addresses ([i]10 or more at a time[/i]).
[*]When I receive the data, I split it up accordingly and query the IPs just received.[/list]

All of that I can handle and have completed the code for up to 95%. The string of IP Addresses I expect to receive are of other servers in which I will attempt to query and gather data from.

However, the problem is I may need to query up to 100 or 1000 servers and I don't want to have to wait for one to finish, in case it's unresponsive and gets delayed the full [i]timeout[/i] length before moving on.

With that said, I want to query as many servers as fast as possible. And I don't care which order they come back in. As soon as I receive the data I just need to save it to a file ([i]no further communication to the original client is required[/i]).

I am writing this in C and in the Linux environment -- so ...

Hey cscgal,

Thanks. I have certainly missed this place myself. I think it's time for me to poke around in the C/C++ section again. :)

Glad to see some familiar faces around!

[b]eFax Messenger Plus — What Really Happened?[/b]

To be honest, I don't know what really happened to the software. I recently discovered that [url=www.efax.com]eFax[/url] no longer supports the eFax Messenger Plus software and that it has ceased to be made available to the consumer from both eFax and [url=www.j2global.com]j2 Global Communications[/url] with no mention of why.

For those of us that enjoyed the wide range of features found in the Plus version, like the automatic TIF extension registration and the handy Live Menu plug-in—that would be available in software packages like Microsoft Word, Excel and Notepad to make converting documents to eFax format as easy as 1-2-3—are no longer made available with the Standard eFax Messenger version of the software.

All of the top Download sites like [url=http://www.tucows.com]Tucows[/url] and [url=http://www.downloads.com]Downloads.com[/url] have removed their entries of the eFax Messenger Plus software. I'm not sure why eFax was so quick to remove this version of their software from existence only to replace it with a stripped down version, along with the fact that they haven't updated the eFax community with a valid explanation either. However, I hope that will change sooner than later.

The Plus version of the eFax software still works for a fact as a company I work with still uses it on a regular basis. I was even more upset when I discovered that the eFax paying customers, for the Plus service, aren't able to download the Plus version of the software anywhere on their site and are forced ...

Hello crunchie-

Thanks for your help. Here is my new HJT log:

Logfile of HijackThis v1.99.1
Scan saved at 10:33:19 AM, on 5/10/2005
Platform: Windows 2000 SP4 (WinNT 5.00.2195)
MSIE: Internet Explorer v6.00 SP1 (6.00.2800.1106)

Running processes:
C:\WINNT\System32\smss.exe
C:\WINNT\system32\winlogon.exe
C:\WINNT\system32\services.exe
C:\WINNT\system32\lsass.exe
C:\WINNT\system32\svchost.exe
C:\WINNT\system32\spoolsv.exe
C:\WINNT\system32\CTsvcCDA.exe
C:\WINNT\System32\svchost.exe
C:\Program Files\ewido\security suite\ewidoctrl.exe
C:\Program Files\Common Files\Microsoft Shared\VS7Debug\mdm.exe
C:\WINNT\system32\nvsvc32.exe
C:\WINNT\system32\oodag.exe
C:\WINNT\system32\MSTask.exe
C:\Program Files\VMware\VMware Workstation\vmware-authd.exe
C:\WINNT\system32\vmnat.exe
C:\WINNT\System32\WBEM\WinMgmt.exe
C:\WINNT\system32\MsPMSPSv.exe
C:\WINNT\system32\svchost.exe
C:\WINNT\system32\vmnetdhcp.exe
C:\WINNT\system32\userinit.exe
C:\WINNT\Explorer.EXE
C:\Program Files\Armor2net\Armor2net Personal Firewall\Armor2net.exe
C:\Program Files\Common Files\Symantec Shared\ccApp.exe
C:\PROGRA~1\SYMANT~1\VPTray.exe
C:\Program Files\FarStone\VirtualDrive\VDTask.exe
C:\WINNT\SM1BG.EXE
C:\Program Files\Creative\SBAudigy2\Surround Mixer\CTSysVol.exe
C:\Program Files\Creative\SBAudigy2\DVDAudio\CTDVDDet.EXE
C:\WINNT\system32\CTHELPER.EXE
C:\Program Files\Java\jre1.5.0_02\bin\jusched.exe
C:\Program Files\Creative\MediaSource\RemoteControl\RcMan.exe
C:\Program Files\Webroot\Spy Sweeper\SpySweeper.exe
C:\WINNT\system32\rundll32.exe
C:\Program Files\CursorXP\CursorXP.exe
C:\Program Files\UltraMon\UltraMon.exe
C:\Program Files\Common Files\Intuit\QuickBooks\QBUpdate\qbupdate.exe
C:\Program Files\UltraMon\UltraMonTaskbar.exe
C:\WINNT\Integrator.exe
E:\Misc\Protection\HijackThis.exe

R1 - HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings,ProxyServer = 169.229.50.17:3128
R0 - HKCU\Software\Microsoft\Internet Explorer\Toolbar,LinksFolderName =
O2 - BHO: AcroIEHlprObj Class - {06849E9F-C8D7-4D59-B87D-784B7D6BE0B3} - C:\Program Files\Adobe\Acrobat 6.0\Reader\ActiveX\AcroIEHelper.dll
O2 - BHO: CCHelper - {0CF0B8EE-6596-11D5-A98E-0003470BB48E} - C:\Program Files\Panicware\Pop-Up Stopper Pro\CCHelper.dll
O2 - BHO: A2NPopUpKiller Class - {8A321C7D-9CED-45A8-870D-DAE843A45FD0} - C:\Program Files\Armor2net\Armor2net Personal Firewall\PopUpKiller.dll
O3 - Toolbar: @msdxmLC.dll,-1@1033,&Radio - {8E718888-423F-11D2-876E-00A0C9082467} - C:\WINNT\System32\msdxm.ocx
O3 - Toolbar: Pa&nicware Pop-Up Stopper Pro - {B1E741E7-1E77-40D4-9FD8-51949B9CCBD0} - C:\Program Files\Panicware\Pop-Up Stopper Pro\popuppro.dll
O4 - HKLM..\Run: [Synchronization Manager] mobsync.exe /logon
O4 - HKLM..\Run: [High Definition Audio Property Page Shortcut] HDAudPropShortcut.exe
O4 - HKLM..\Run: [Armor2net] C:\Program Files\Armor2net\Armor2net Personal Firewall\Armor2net.exe
O4 - HKLM..\Run: [ccApp] "C:\Program Files\Common Files\Symantec Shared\ccApp.exe"
O4 - HKLM..\Run: [vptray] C:\PROGRA~1\SYMANT~1\VPTray.exe
O4 - HKLM..\Run: [VirtualDrive] "C:\Program Files\FarStone\VirtualDrive\VDTask.exe" /AutoRestore
O4 - HKLM..\Run: [SM1BG] C:\WINNT\SM1BG.EXE
O4 - HKLM..\Run: [CTSysVol] C:\Program Files\Creative\SBAudigy2\Surround Mixer\CTSysVol.exe
O4 - HKLM..\Run: [CTDVDDet] C:\Program Files\Creative\SBAudigy2\DVDAudio\CTDVDDet.EXE
O4 - HKLM..\Run: [CTHelper] CTHELPER.EXE
O4 - HKLM..\Run: [CTStartup] "C:\Program Files\Creative\Splash Screen\CTEaxSpl.EXE" /run
O4 - HKLM..\Run: [SunJavaUpdateSched] ...

Hello,

I beleive I found a suspicious file on my machine used as an [i]explorer.exe[/i] shell. I've been trying to remove it all day, with no luck. Below is my HJT Log:

Logfile of HijackThis v1.98.2
Scan saved at 11:16:48 AM, on 5/9/2005
Platform: Windows 2000 SP4 (WinNT 5.00.2195)
MSIE: Internet Explorer v6.00 SP1 (6.00.2800.1106)

Running processes:
C:\WINNT\System32\smss.exe
C:\WINNT\system32\csrss.exe
C:\WINNT\system32\winlogon.exe
C:\WINNT\system32\services.exe
C:\WINNT\system32\lsass.exe
C:\WINNT\system32\svchost.exe
C:\WINNT\System32\WBEM\WinMgmt.exe
C:\WINNT\Explorer.exe
C:\Program Files\mozilla.org\Mozilla\mozilla.exe
E:\Misc\Protection\Highjack This\HijackThis.exe

R1 - HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings,ProxyServer = 169.229.50.17:3128
R0 - HKCU\Software\Microsoft\Internet Explorer\Toolbar,LinksFolderName =
F2 - REG:system.ini: Shell=Explorer.exe C:\WINNT\system32\stivc.exe
O2 - BHO: AcroIEHlprObj Class - {06849E9F-C8D7-4D59-B87D-784B7D6BE0B3} - C:\Program Files\Adobe\Acrobat 6.0\Reader\ActiveX\AcroIEHelper.dll
O2 - BHO: CCHelper - {0CF0B8EE-6596-11D5-A98E-0003470BB48E} - C:\Program Files\Panicware\Pop-Up Stopper Pro\CCHelper.dll
O2 - BHO: A2NPopUpKiller Class - {8A321C7D-9CED-45A8-870D-DAE843A45FD0} - C:\Program Files\Armor2net\Armor2net Personal Firewall\PopUpKiller.dll
O3 - Toolbar: @msdxmLC.dll,-1@1033,&Radio - {8E718888-423F-11D2-876E-00A0C9082467} - C:\WINNT\System32\msdxm.ocx
O3 - Toolbar: Pa&nicware Pop-Up Stopper Pro - {B1E741E7-1E77-40D4-9FD8-51949B9CCBD0} - C:\Program Files\Panicware\Pop-Up Stopper Pro\popuppro.dll
O3 - Toolbar: (no name) - {62999427-33FC-4baf-9C9C-BCE6BD127F08} - (no file)
O4 - HKLM..\Run: [Synchronization Manager] mobsync.exe /logon
O4 - HKLM..\Run: [High Definition Audio Property Page Shortcut] HDAudPropShortcut.exe
O4 - HKLM..\Run: [SoundMan] SOUNDMAN.EXE
O4 - HKLM..\Run: [AlcWzrd] ALCWZRD.EXE
O4 - HKLM..\Run: [Alcmtr] ALCMTR.EXE
O4 - HKLM..\Run: [Armor2net] C:\Program Files\Armor2net\Armor2net Personal Firewall\Armor2net.exe
O4 - HKLM..\Run: [ccApp] "C:\Program Files\Common Files\Symantec Shared\ccApp.exe"
O4 - HKLM..\Run: [vptray] C:\PROGRA~1\SYMANT~1\VPTray.exe
O4 - HKLM..\Run: [VirtualDrive] "C:\Program Files\FarStone\VirtualDrive\VDTask.exe" /AutoRestore
O4 - HKLM..\Run: [SM1BG] C:\WINNT\SM1BG.EXE
O4 - HKLM..\Run: [CTSysVol] C:\Program Files\Creative\SBAudigy2\Surround Mixer\CTSysVol.exe
O4 - HKLM..\Run: [CTDVDDet] C:\Program Files\Creative\SBAudigy2\DVDAudio\CTDVDDet.EXE
O4 - HKLM..\Run: [CTHelper] CTHELPER.EXE
O4 - HKLM..\Run: [CTStartup] "C:\Program Files\Creative\Splash Screen\CTEaxSpl.EXE" /run
O4 - HKLM..\Run: [SunJavaUpdateSched] C:\Program Files\Java\jre1.5.0_02\bin\jusched.exe
O4 ...

Hello,

I see a problem with the following code:[code]int main(void)
{
myfunction(argument1, argument2);

[color=red]int argument1 = 4, argument2 = 5;[/color]

return 0;
}[/code]Simple. You create your variables after you try and pass them to [i][b]myfunction()[/b][/i]. Rather, your code should be:[code]int main(void)
{
[color=green]int argument1 = 4, argument2 = 5;[/color]

myfunction(argument1, argument2);

return 0;
}[/code]

  • [color=#CC3200]S[/color]tack [color=#CC3200]O[/color]verflow

Hello,

You may want to try:[code][color=#008800]// my first program in C++[/color]

[color=blue]#include[/color] <iostream>
[color=blue]using[/color] [color=blue]namespace[/color] std;

[color=blue]int[/color] main ([color=blue]void[/color]) {
[color=#008800] // Display text[/color]
cout << "Hello World!" << endl;
[color=#008800] // Pause screen[/color]
cin.get();
[color=#008800] // Terminate program[/color]
[color=blue]return[/color] 0;
}[/code]The function [url=http://www.cplusplus.com/ref/iostream/istream/get.html]get()[/url] extracts a character from the stream and returns its value. It's a good way of pausing the console screen.

  • [color=#CC3200]S[/color]tack [color=#CC3200]O[/color]verflow

Hello,

[quote=Fasola]Hmmmmm....in the function multiply() how did you go from parameters x and y to i and j, that looks like a "call-by-reference" but i don't understand why you did that and how it works[/quote][b]»[/b] One aspect of C functions may be unfamiliar to programmers who are used to some other languages, particularly Fortran, is [b]Call by Value[/b]. In C, all function arguments are passed "by value". This means that the called function is given the values of its arguments in temporary variables rather than the originals. This leads to some different properties that are seen with "call by reference" languages. The main distinction is that in C the called function cannot directly alter a variable in the calling function; it can only alter its private, temporary copy.

Call by value is an asset, however, a liability. It usually leads to more compact programs with fewer extraneous variables, because paramaters can be treated as conveniently initialized local variables in the called routine. For example, here is a version of power that makes use of this property:[code][color=blue]int[/color] power([color=blue]int[/color] base, [color=blue]int[/color] n) {
[color=blue]int[/color] p;

[color=blue]for[/color] (p = 1; n > 0; --n)
    p = p * base;
[color=blue]return[/color] p;

}

[color=blue]int[/color] main() {
[color=blue]int[/color] x = 2, y = 3;

printf("%d\n", power(2, 3));

printf("%d\n", power(x, y));

[color=blue]return[/color] 0;

}[/code]The paramater [i]n[/i] is used as a temporary variable, and is counted down until it becomes zero. Whatever is done to [i]n[/i] inside [b]power[/b] has no effect on the argument that [b]power[/b] was ...

Hello,

I have not done TCL script programming before, though I do believe the following links can help you:

[list]
[][url=http://www.beedub.com/book/]Practical Programming in Tcl and Tk[/url]
[
][url=http://hegel.ittc.ku.edu/topics/tcltk/]Tcl/Tk Information[/url]
[][url=http://www.techiwarehouse.com/Tcl_Tk/Tcl_Tk_Tutorial.html]Tcl/Tk Tutorial[/url]
[
][url=ftp://ftp.tcl.tk/pub/tcl//doc/TclTkElRef803.pdf]TCL Electronic Reference[/url] *pdf file
[/list]
I can provide additional links if need be.

  • [color=#CC3200]S[/color]tack [color=#CC3200]O[/color]verflow

Hello,

You're welcome. If you have any further questions regarding the information I provided, don't hesitate to ask.

  • [color=#CC3200]S[/color]tack [color=#CC3200]O[/color]verflow

Hello,

I would recommend [url=http://www.bloodshed.net/devcpp.html]Bloodshed Dev-C++[/url] for C++ programming.

If you want to compile C code, I would suggest using a C compiler such as GCC. I'm un-aware of any free available IDE envrionments for the C language. Though you may be able to compile C code in Dev-C++. I've never tried, though.

  • [color=#CC3200]S[/color]tack [color=#CC3200]O[/color]verflow

Hello,

[quote=kloony]what could be reasons for malloc to fail then?[/quote]Keep in mind if the space cannot be allocated, a null pointer is returned. If you try to write to the NULL block of memory, it will cause a segmentation fault. It's best to test for NULL values before writing to the address.

[quote=kloony]May I know what is meant by memory leaks?[/quote]Definition: Memory leaks are often thought of as failures to release unused memory by a computer program. Strictly speaking, that behaviour is just more memory consumption. A memory leak occurs when the program loses even the ability to free the memory. Either behaviour diminishes the performance of the computer, as it becomes unable to use all its available memory.

  • [color=#CC3200]S[/color]tack [color=#CC3200]O[/color]verflow

Hello,

In addition, you may find this short tutorial on Segmentation Faults handy: [url=http://cboard.cprogramming.com/showthread.php?t=59721#post421421]Locating a Segmentation Fault[/url]

Or if you have access to [b]gdb[/b] or [b]Valgrind[/b], the following articles may be of use:

[list]
[][url=http://www.cprogramming.com/debugging/segfaults.html]Debugging Segmentation Faults[/url]
[
][url=http://www.cprogramming.com/debugging/valgrind.html]Finding Memory Leaks and Invalid Memory Use Using Valgrind[/url]
[/list]

Also, I put together a C example of healthy memory allocation techniques:[code][color=blue]#include[/color] <stdio.h>
[color=blue]#include[/color] <stdlib.h>

[color=blue]#define[/color] DATA 2

[color=blue]struct[/color] List {
[color=blue]char[/color] **data;
};

[color=blue]struct[/color] Page {
[color=blue]struct[/color] List *list;
};

[color=blue]int[/color] main() {
[color=#008800]/ local variables /[/color]
[color=blue]int[/color] i, j, k;

[color=#008800]/* allocate memory; page */[/color]
[color=blue]struct[/color] Page *page_1 = malloc([color=blue]sizeof[/color] *page_1);
[color=blue]if[/color] (page_1 == NULL) {
    fprintf(stderr, "page_1 allocation failed\n");
    [color=blue]return[/color] 0;
}
printf("page_1 allocated\n");

[color=#008800]/* allocate memory; list */[/color]
page_1->list = malloc([color=blue]sizeof[/color] *page_1->list);
[color=#008800]/* if allocation failed */[/color]
[color=blue]if[/color] (page_1->list == NULL) {
    fprintf(stderr, "page_1->list allocation failed\nfree page_1\n");
    [color=#008800]/* free memory; page */[/color]
    free(page_1);
    page_1 = NULL;
    [color=blue]return[/color] 0;
}
printf("page_1->list allocated\n");

[color=#008800]/* allocate memory; data */[/color]
page_1->list->data = malloc(DATA * [color=blue]sizeof[/color] *page_1->list->data);
[color=#008800]/* if allocation failed */[/color]
[color=blue]if[/color] (page_1->list->data == NULL) {
    fprintf(stderr, "page_1->list->data allocation failed\nfree page_1->list & page_1\n");
    [color=#008800]/* free memory; list */[/color]
    free(page_1->list);
    page_1->list = NULL;
    [color=#008800]/* free memory; page */[/color]
    free(page_1);
    page_1 = NULL;
}
printf("page_1->list->data allocated\n");

[color=#008800]/* allocate memory; data rows */[/color]
[color=blue]for[/color] (i = 0; i < DATA; i++) {
    page_1->list->data[i] = malloc(10);
    [color=#008800]/* allocation failed */[/color]
    [color=blue]if[/color] (page_1->list->data[i] == NULL) {
        fprintf(stderr, "page_1->list->data[%i] allocation failed\nfree page_1->data & page_1->list & page_1", i);
        [color=#008800]/* free any memory done during the loop */[/color]
        [color=blue]for[/color] (k = ...

Hello,

[quote=kloony]So if the free(q) and q=NULL is included in the loop, this will avoid the problem right?[/quote]Indeed it should. Each call to malloc will be subsequently freed during each loop iteration.

[quote=kloony]However, I was told that it is much safer to use UNIX as Windows sometimes allows you to use memory that is not supposed to be used, which could result in a problem later. Is this correct?[/quote]I'm not entirely sure. I don't have any hard evidence or facts at the moment, though I have run into cases were Windows can grab a hold of non-valid memory, thus causing a segmentation fault. While sometimes, its a programming error.

As a side note, a segmentation fault is an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

[quote=kloony]Now supposed I am done with G and I want to free the memory used for G, will

[code]free(G); G=NULL;[/code]

suffice? Or do I need to free() the adjacency matrix row by row, set to NULL and then free(G) and set G=NULL?[/quote]I'm afraid not. Freeing the memory allocated to G will free, but not G->adj. As G->adj points to MATRIXint(). You will have to de-allocate that memory ...

Hello,

I'm not very fluent in Win32 programming, though I might be able to point you in the right direction. Below are a list of links that may help you:

[list]
[][url=http://www.codeproject.com/treectrl/qfilter.asp]A checkbox tree control for use in filtering selections[/url]
[
][url=http://secure.codeproject.com/treectrl/vtree.asp]Tree control with bitmap checkboxes supported[/url]
[/list]
Hope this helps.

  • [color=#CC3200]S[/color]tack [color=#CC3200]O[/color]verflow

Hello,

[quote=kloony]for(i=0; i<1000; i++)
QUEUEinit(50);[/quote]
If I am correct in my thinking, you will have lost allocated memory in your program. Your pointer, [b]q[/b], is a variable. Allocating memory to it 1000 times will cause a problem.

According to the C89 Sec. 4.10.3: [font=courier new]The pointer returned if the allocation succeeds is suitably aligned so that it may be assigned to a pointer to any type of object and then used to access such an object in the space allocated (until the space is explicitly freed or reallocated).[/font]

Each time you call malloc, you request a block of freed space, if successful. You will have 1000 blocks of memory, as each time malloc is called, while [b]q[/b] may point to only one, and the last, block of memory allocated.

According to the C89 Sec. 4.10.3: [font=courier new]If the space cannot be allocated, a null pointer is returned. If the size of the space requested is zero, the behavior is implementation-defined; the value returned shall be either a null pointer or a unique pointer. The value of a pointer that refers to freed space is indeterminate.[/font]

Onto your next question.
[quote=kloony]What if, instead I had

Code:[code]for(i=0; i<1000; i++) {QUEUEinit(50); free(q); q=NULL;}[/code]

Does this mean that every time QUEUEinit() is called, the memory set aside for it will be freed before the next i, so I will never run out of memory?[/quote]
I believe so. Each time QUEUEinit() is called, you request a block of memory. As in this loop, you free ...

Hello there Elmo! Welcome to DaniWeb. I'm sure Dani will be around to say hi soon. :D
So welcome and have fun.

  • [color=#CC3200]S[/color]tack [color=#CC3200]O[/color]verflow

Hello,

1) What I mean't by function scope is simple. All it knows is what is happening within the function, and it doesn't have a clue as to what is happening in the 'int main' part of the program. This kind of scope is called 'local scope'. Whatever happens in the 'int main' is only visible to that section of the program. And on the other hand, anything that happens in the function 'test' is only visible to that part of the program, which in both cases is between the opening and closing curly braces.

2) That strikes me as well. Let me re-post my example using the [b]%p[/b] type condition instead of the [b]%0x%08x[/b]:[code][color=blue]#include[/color] <stdio.h>
[color=blue]#include[/color] <stdlib.h>

[color=blue]void[/color] test([color=blue]char[/color] *p) {
[color=#008800]/
run test; view memory address /[/color]
printf("Function scope: %p\n", ([color=blue]void[/color]
)p);
[color=#008800]/
free memory /[/color]
free(
p);
[color=#008800]/ set to NULL /[/color]
p = NULL;
printf("Function scope - After NULL: %p\n", ([color=blue]void[/color]
)*p);
}

[color=blue]int[/color] main() {
[color=blue]char[/color] *ptr = malloc(25);

[color=#008800]/* allocation may have failed */[/color]
[color=blue]if[/color] (ptr == NULL)
    [color=blue]return[/color] 0;

printf("Before call: %p\n", ([color=blue]void[/color] *)ptr);
[color=#008800]/* send memory address */[/color]
test(&ptr);
[color=#008800]/* run some tests */[/color]
printf("After call: %p\n", ([color=blue]void[/color] *)ptr);

[color=blue]return[/color] 0;

}[/code]In this particular case, and for testing purposes only, this is what my output looked like:[code]Before call: 0xa0501b8
Function scope: 0xa0501b8
Function scope - After NULL: 0x0
After call: 0x0[/code]As seen, the address of ptr is the same when in the scope of main, and dereferenced in the scope of test().

  • ...

Hello,

No problem. Let me answer this question by example:[code][color=blue]#include[/color] <stdio.h>
[color=blue]#include[/color] <stdlib.h>

[color=blue]void[/color] test([color=blue]char[/color] *p) {
[color=#008800]/
run test; view memory address /[/color]
printf("Function scope: %0x%08x\n", ([color=blue]un[color=blue]signed[/color][/color] [color=blue]int[/color])
p);
[color=#008800]/ free memory /[/color]
free(p);
[color=#008800]/
set to NULL /[/color]
p = NULL;
printf("Function scope - After NULL: %0x%08x\n", ([color=blue]un[color=blue]signed[/color][/color] [color=blue]int[/color])*p);
}

[color=blue]int[/color] main() {
[color=blue]char[/color] *ptr = malloc(25);

[color=#008800]/* allocation may have failed */[/color]
[color=blue]if[/color] (ptr == NULL)
    [color=blue]return[/color] 0;

[color=#008800]/* memory address before */[/color]
printf("Before call: %0x%08x\n", ([color=blue]un[color=blue]signed[/color][/color] [color=blue]int[/color])ptr);
[color=#008800]/* send memory address */[/color]
test(&ptr);
[color=#008800]/* run some tests */[/color]
printf("After call: %0x%08x\n", ([color=blue]un[color=blue]signed[/color][/color] [color=blue]int[/color])ptr);

[color=blue]return[/color] 0;

}[/code]If you run this code, you will see that I passed the variables address to test(). With the address, I can modify and free the memory from within that scope. You will see that the free() and NULL calls were successful by the time we return to main().

  • [color=#CC3200]S[/color]tack [color=#CC3200]O[/color]verflow

Hello,

When you call free, the memory pointed to by the passed pointer is freed, but the value of the pointer in the caller remains unchanged, because C's pass-by-value semantics mean that called functions never permanently change the values of their arguments.

A pointer value which has been freed is, strictly speaking, invalid, and any use of it, even if is not dereferenced can theoretically lead to trouble, though as a quality of implementation issue, most implementations will probably not go out of their way to generate exceptions for innocuous uses of invalid pointers.

[i]References: ANSI Sec. 4.10.3
ISO Sec. 7.10.3
Rationale Sec. 3.2.2.3 [/i]

  • [color=#CC3200]S[/color]tack [color=#CC3200]O[/color]verflow

Hello,

Also, be sure to set your variable to NULL after you free the memory. Example:[code]free(G); [color=#008800]/ Dynamically allocated memory is freed /[/color]
G = NULL; [color=#008800]/ This ensures that we point our pointer to NULL /[/color][/code]
This can ensure a few things. Firstly, it ensures our pointer doesn't point to our freed address. Secondly, it secures your code from crashing if you call free() twice on the same pointer. For instance:[code]free(G); [color=#008800]/ Dynamically allocated memory is freed /[/color]
free(G); [color=#008800]/ Will cause a crash. /[/color][/code]Where in fact:[code]free(G); [color=#008800]/ Dynamically allocated memory is freed /[/color]
G = NULL; [color=#008800]/ This ensures that we point our pointer to NULL /[/color]
free(G); [color=#008800]/ Won't cause a crash because G points to NULL. /[/color][/code]Hope this helps.

  • [color=#CC3200]S[/color]tack [color=#CC3200]O[/color]verflow

Hello,

When free() is called, a block of memory previously allocated by a call to malloc, calloc or realloc is freed:[code]Graph G=malloc([color=blue]sizeof[/color] G); [color=#008800]/ Memory is dynamically allocated /[/color]
[color=blue]if[/color] (G != NULL) { [color=#008800]/
G successfully points to an empty block of memory /[/color]
[color=#008800]/
Do task here /[/color]
}[color=blue]else if[/color] (G == NULL) {
[color=blue]return[/color] 0; [color=#008800]/
Memory allocation failed */[/color]
}

free(G); [color=#008800]/ Dynamically allocated memory is freed /[/color][/code]
Further documentation:

[url=http://www.cplusplus.com/ref/cstdlib/malloc.html]C Reference: malloc()[/url]
[url=http://www.cplusplus.com/ref/cstdlib/free.html]C Reference: free()[/url]

  • [color=#CC3200]S[/color]tack [color=#CC3200]O[/color]verflow

Hello,

Attached is a modified version of your main file: main.cpp

My workspace contains 1 Source File, main.cpp, and 1 Header File, free_list.h.

  • [color=#CC3200]S[/color]tack [color=#CC3200]O[/color]verflow

Hello,

An unresolved external occurs when some code has a call to a function in another module and the linker can't find that function in any of the modules or libraries that you are currently linking to.

In this specific case, it means one of two things. Either you are trying to write a Win32 GUI application (or non-console application) and accidently compiled it as a Console application... or you really are trying to compile a console application and didn't write or properly compile in a main() function.

P.S. Make sure your program has an entry point, i.e. main()

A basic implementation of a program entry point in C++ is the following:[code][color=blue]int[/color] main([color=blue]void[/color]) {
[color=blue]return[/color] 0;
}[/code]

  • [color=#CC3200]S[/color]tack [color=#CC3200]O[/color]verflow

Hello,

I have viewed and compiled the source files without warnings or errors with Visual C++ .Net 2003. I did not see main() declared in either of your two files, as I added it manually during my build. Would you mind sharing the precise error and what's your current compiler?

  • [color=#CC3200]S[/color]tack [color=#CC3200]O[/color]verflow

Hello,

If I understand correctly, you want to replace all space occurances to "[color=green]space[/color]" where [color=green]space[/color] represents the space, ' ', character:[code][color=red]sed[/color] [color=#CC3200]-i[/color] '[color=blue]s/ /[/color][color=green]\ /[/color]' [color=purple][i]file[/i][/color][/code]
To clarify:
[color=red]sed[/color] — Is the program.
[color=#CC3200]-i[/color] — Is a sed option that can edit files in place (makes backup if extension supplied)
[color=blue]s/ /[/color] — Finds all occurances of space
[color=green]\ /[/color] — Replaces the space with '\ '. Without the single quotation marks.
[color=purple][i]file[/i][/color] — Is the input file.

For reference, the [color=blue]s[/color] command is the [i]substitution[/i] command. The [color=blue]/ /[/color] statement is to find any occurance of a space. The [color=green]\ /[/color] statement will replace the space, ' ', found with '\ '.

  • [color=#CC3200]S[/color]tack [color=#CC3200]O[/color]verflow

Hello,

You're on the right path, though your syntax is a bit off. We know that sed substitutes text using the [color=green]s[/color] command. Suppose you wanted to change all occurrences of "Unix" in the text to "UNIX":[code]# Substitute Unix with UNIX
[color=red]sed[/color] '[color=brown]s[/color][color=green]/Unix/UNIX[/color]' [color=purple]intro[/color][/code]Where [color=red]sed[/color] is the [i]program[/i], [color=brown]s[/color] is the substitute command in sed, [color=green]/Unix/UNIX[/color] is the [i]command[/i], and [color=purple]intro[/color] is the [i]file[/i].

The dollar sign [i]$[/i] is used to match the end of the line. The following example will add [b]>>[/b] to the end of each line found in the [i]file[/i] specified:[code]# Append >> at the end of each line
1,$s/$/>>/[/code]With all of this in mind, we can create a simple script to accomplish your task:[code]# Substitute [i]and[/i] with [i]&[/i]; and append "Rob Sammons" to the end of each line
[color=red]sed[/color] [color=orange]-i[/color] '[color=brown]s[/color][color=blue]/and/\&/g[/color];[color=green]1,$s/$/"Rob Sammons"/[/color]' [color=purple]Coursework.txt[/color][/code]Output before running command: [quote]Hello, this is a test for sed [color=blue]and[/color] so on.
Let's hope this works.[/quote]Output after script executed:[quote]Hello, this is a test for sed [color=blue]&[/color] so on."Rob Sammons"
Let's hope this works."Rob Sammons"[/quote]To clarify:

[color=red]sed[/color] — Is the program.
[color=orange]-i[/color] — Is a sed option that can edit files in place (makes backup if extension supplied)
[color=brown]s[/color] — Is the substitute command in sed
[color=blue]/and/\&/g[/color] — Substitutes all occurences of [i]and[/i] to [i]&[/i]
[color=green]1,$s/$/"Rob Sammons"/[/color] — Appends [i]"Rob Sammons"[/i] at the end of each line.
[color=purple]Coursework.txt[/color] — Is the input file.

  • [color=#CC3200]S[/color]tack [color=#CC3200]O[/color]verflow

Hello,

The [font=courier new]switch[/font] statement is a multi-way decision that tests whether an expression matches one of a number of [i]constant[/i] integer values, and branches accordingly. The [font=courier new]break[/font] statment causes an immediate exit from the [font=courier new]switch[/font]. Because cases serve just as labels, after the code for one case is done, execution [i]falls through[/i] to the next unless you take explicit action to escape. [font=courier new]break[/font] and [font=courier new]return[/font] are the most common ways to leave a [font=courier new]switch[/font].

Falling through cases is a mixed blessing. On the positive side, it allows several cases to be attached to a single action. For example:[code][color=blue]int[/color] main([color=blue]void[/color]) {
[color=blue]int[/color] tmp = 5;

[color=blue]switch[/color] (tmp) {
    [color=blue]case[/color] 1:
    [color=blue]case[/color] 2:
    [color=blue]case[/color] 3:
        [color=#008800]/* Do something if 1, 2, or 3 */[/color]
    [color=blue]break[/color];  [color=#008800]/* Break statment if case expression matched */[/color]
    [color=blue]case[/color] 4:
    [color=blue]case[/color] 5:
        [color=#008800]/* Do something if 4 or 5 */[/color]
    [color=blue]break[/color];  [color=#008800]/* Break statment if case expression matched */[/color]
}

[color=blue]return[/color] 0;

}[/code]Keep in mind, this implies that normally each case must end with a [font=courier new]break[/font] to prevent falling through to the next. Falling through from one case to another is not robust, being prone to disintegration when the program is modified. With the exception of multiple labels for a single computation, fall-throughs should be used sparingly, and commented.

  • [color=#CC3200]S[/color]tack [color=#CC3200]O[/color]verflow