Want to prevent a duplication of a header file.

How can I prevent the duplication of stdlib.h in main.cpp?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// AllocateVector.h
#ifndef ALLOCATEVECTOR_H
#define ALLOCATEVECTOR_H
#include <stdlib.h>

template <class dataType>
int allocateData1DbyMalloc(dataType*& data1D, unsigned int nRow) {
	data1D = (dataType*)malloc(nRow * sizeof(dataType)); // sizeof(type1));
	return (data1D != NULL) ? EXIT_SUCCESS : EXIT_FAILURE;
}
#endif


// AllocateMatrix.h
#ifndef ALLOCATEMATRIX_H
#define ALLOCATEMATRIX_H
#include <stdlib.h>

int allocateData2DbyMalloc(dataType**& data2D, unsigned int nRow, unsigned int nColumn) {
	data2D = (dataType * *)malloc(nRow * sizeof(dataType*)); // calloc(nRow, sizeof(type1*));
	if (data2D == NULL) {
		return EXIT_FAILURE;
	}

	// Now point each row to an array of nColumn type1 objects
	for (unsigned int i = 0; i < nColumn; ++i) {
		data2D[i] = (dataType*)malloc(nColumn * sizeof(dataType)); //calloc(nColumn, sizeof(type1));
		if (data2D[i] == NULL) {
			return EXIT_FAILURE;
		}
	}
	return EXIT_SUCCESS;
}
#endif


//main.cpp
#include "AllocateVector.h"
#include "AllocateMatrix.h"
int main()
{

	return 0;
}
How can I prevent the duplication of stdlib.h in main.cpp?

Already done with header guards in the header, naturally.
Yes. I did. Hey, you did not reply my last question from the last post in the present block. I am trying to understand, how does it click on your head if a people share same post in two different block, just to achieve a better answer. Can you reply it?.
Now I saw, my last massage was reported. I hope, the admin will ask question to me and the corresponding guy before taking any action.
Don't worry about #including stdlib.h multiple times. It has its own header guards.

Also, don't define non-inline or non-template functions inside a header file. If you end up with multiple compilation units (.cpp files) that include that header file, you'll get multiple definitions of the function. Instead, declare the function in the header and define it in a cpp file

// AllocateMatrix.h
1
2
3
4
5
6
#ifndef ALLOCATEMATRIX_H
#define ALLOCATEMATRIX_H

// Declaration
int allocateData2DbyMalloc(dataType**& data2D, unsigned int nRow, unsigned int nColumn);
#endif 


// AllocateMatrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "AllocationMatrix.h"
#include <stdlib.h>

//Definition
int allocateData2DbyMalloc(dataType**& data2D, unsigned int nRow, unsigned int nColumn) {
	data2D = (dataType * *)malloc(nRow * sizeof(dataType*)); // calloc(nRow, sizeof(type1*));
	if (data2D == NULL) {
		return EXIT_FAILURE;
	}

	// Now point each row to an array of nColumn type1 objects
	for (unsigned int i = 0; i < nColumn; ++i) {
		data2D[i] = (dataType*)malloc(nColumn * sizeof(dataType)); //calloc(nColumn, sizeof(type1));
		if (data2D[i] == NULL) {
			return EXIT_FAILURE;
		}
	}
	return EXIT_SUCCESS;
}

> How can I prevent the duplication of stdlib.h in main.cpp?

The C standard guarantees that including a standard header multiple times has the same effect as including it just once.
Standard headers may be included in any order; each may be included more than once in a given scope, with no effect different from being included only once, except that the effect of including <assert.h> depends on the definition of NDEBUG

This guarantee holds, even though the header need not be an actual file:
A header is not necessarily a source file, nor are the < and > delimited sequences in header names necessarily valid source file names.
Thanks Dave and JL Borges. Nice idea.
@shafiul0304034 Why are you ignoring Furry Guy, who was the first person to give you the same advice?
I am sorry for him.
Topic archived. No new replies allowed.