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,

[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,

[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,

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,

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,

C provides the infinitely-abusable goto statement, and labels branch to. Formally, the goto statement is never necessary, and in practice it is almost always easy to write code without it. The goto statement was once widely used. Unfortunately code that used goto extensively was poorly structured and could easily become unmanageable, and is known as "spaghetti code". Today, programming languages provide other ways to structure code so goto is rarely needed. In C++ it is always possible to use something other than goto to achieve what you need.

Note that the use of goto is a religious issue and has provoked a great deal of debate. In C++ there is always a better alternative for writing loops, so you don't need to use goto at all. There are other circumstances where goto may be more appropriate than the alternatives, though that is a seperate subject. Keep in mind when you use it to exit a block which contains aggregates requiring destructors, the destructors will run before the goto transfers control. The compiler still forbids using goto to enter a scope that requires constructors.

The indiscriminate use of goto statements has caused tangled, miserable, impossible-to-read programs. To avoid the use of goto, more sophisticated, tightly controlled looping commands have been introduced: for, while, and do-while. Using these makes programs more easily understood, and goto is generally avoided, but one might argue that the case has been a bit overstated. Like any tool, carefully used and in the right hands, goto ...

Were you in the red the two prior years you only had three advertisements on your site?

How much are you paying for your hosting services? Are you exceeding your bandwidth, and being charged a premium fee?

Explain losing revenue, since before the $2,500 campaign there was virtually no other revenue. How, then, could you be losing money? And why weren't you losing money the prior two years?

If the campaign is $2,500 a month then: $2,500/mth = $83.3/day
So you are meaning to say 96% off your campaign funding came from 3 ads?

If the campaign is $2,500/yr and your hosting is $250/month, then that's 10 months of hosting. Which you were still hosting before you had a campaign.

With the exception of a $400 trademark, $100 incorporation fee, $500 legal fees, why do the expenses suddenly outweigh revenue? What type of corporaton is DaniWeb? And why can't I find a filed DaniWeb trademark?

And please tell me why there is a sudden loss of revenue after getting a campaign? What new expenses have been incurred after securing an ad campaign that places you in the red if the campaign funding is reduced?

I could keep asking questions, but they would all derive from the same thought-process.

Ah, I'll see what I can do about the contests.

Maybe something a little simpler than my previous two contests ;)

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

Greetings,

A simple fix I saw. In your function gettemps() change:[code]scanf("%f",temp[i]);[/code]To"[code]scanf("%f",[b]&[/b]temp[i]);[/code]if If you want to store the result of a scanf operation on a standard variable you should precede it with the reference operator, i.e. an ampersand sign (&).

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