How to create a linked list using a struct inside the node element - linked-list

I tried to create a linked list, using a node that had a structure inside it to hold the info data. Whenever I try to do that , I have a compiled error telling me that I am using an "incomplete" type, and I am not sure why.
The exact error I have is the following:
main.c: In function ‘createList’:
main.c:53:46: error: invalid application of ‘sizeof’ to incomplete type ‘typeNode’ {aka ‘struct StructInfoNodo’}
53 | if ((new_node = (typeNode *) malloc (sizeof (typeNode))) == NULL)
| ^~~~~~~~
main.c:59:9: error: dereferencing pointer to incomplete type ‘typeNode’ {aka ‘struct StructInfoNodo’}
59 | new_node->info.idCell=nodeinfo.idCell;
| ^~
main2.c:67:5: warning: statement with no effect [-Wunused-value]
67 | list->num_nodes;
| ~~~~^~~~~~~~~~~
In my code, I have a createList funtion, which tries to create the first node of the list, using some sample data. However, I am unable to the initial malloc, seems it does not recognize the new type that was defined (typeNet) to hold the info inside a structure.
I was using for that the following instruct: new_node = (typeNode *) malloc (sizeof (typeNode))
and after that, I was trying to do new_node->info.idCell=nodeinfo.idCell;
That lines result in the errors shown above.
Any idea on how I can solve it, and be able to write info on the node?
Please find below the complete code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXCHAR 80
struct structInfoNet
{
int idCell;
char MacAddress[MAXCHAR];
char Essid[MAXCHAR];
char Mode[MAXCHAR];
int Channel;
};
typedef struct structInfoNet typeNet;
struct structInfoNode
{
typeNet info;
struct structInfoNode* nextNode;
};
typedef struct StructInfoNodo typeNode;
/* Struct to store pointers to firs & last element linked list */
struct structInfoLista {
typeNode *firstNode;
typeNode *lastNode;
int num_nodes;
};
typedef struct structInfoLista typeInfoList;
/* Initialitate frist & last elements linked list */
void initLinkedList (typeInfoList *list){
list->firstNode = NULL;
list->lastNode = NULL;
list->num_nodes= 0;
}
/* Insert First Element on the List (CreateList) */
int createList (typeInfoList * list, typeNet nodeinfo) {
typeNode *new_node;
if ((new_node = (typeNode *) malloc (sizeof (typeNode))) == NULL)
{
printf ("Error. Unable to create node");
return -1;
}
new_node->info.idCell=nodeinfo.idCell;
new_node->nextNode = NULL;
list->firstNode = new_node;
list->lastNode = new_node;
list->num_nodes;
return 0;
}
int main()
{
typeNet net;
typeInfoList *list;
// Get memory to store struct with pointers to first & last Nodes
if ((list = (typeInfoList *) malloc (sizeof (typeInfoList))) == NULL)
{
printf ("Error!! Unable to get memory");
exit (1);
}
//Create auxiliar list to store pointers to first & last nodes
initLinkedList (list);
// Sample data to add to first element of the list
net.idCell=5;
//Create initial linked list (first element)
createList (list, net);
}

this
typedef struct StructInfoNodo typeNode;
should be
typedef struct structInfoNodo typeNode;

Related

Set the number of linked list nodes C

I have a trace text file with several numbers written on it.
I want to set the number of output nodes to 100, 500, or 1000.
I want to measure the hit rate of the implemented FiFO page replacement algorithm using the given trace file.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct list {
char *string;
struct list *next;
};
typedef struct list LIST;
int main(void) {
FILE *fp;
char line[128];
LIST *current, *head;
head = current = NULL;
fp = fopen("test.txt", "r");
while(fgets(line, sizeof(line), fp)){
LIST *node = malloc(sizeof(LIST));
node->string = strdup(line);//note : strdup is not standard function
node->next =NULL;
if(head == NULL){
current = head = node;
} else {
current = current->next = node;
}
}
fclose(fp);
//test print
for(current = head; current ; current=current->next){
printf("%s", current->string);
}
//need free for each node
return 0;
}

Why pointer argv is not updating?

Can somebody help me understand why the pointer head is not updated after new() call?
expected: val:0 # call new(), update l0.val to 0
actual: val:253784 # why update l0.val not update by the pointer
https://www.edaplayground.com/x/54Nz
#include <stdio.h>
#include <stdlib.h>
typedef struct _node {
int val;
struct _node *next;
} node;
//construct the struct
void new(node *head) {
//malloc return a pointer, type casting to (node*)
node *head_l = (node*)malloc(sizeof(node));
if(!head_l) {
printf("Create Fail!\n");
exit(1);
}
head_l->val = 0;
head_l->next = NULL;
printf("head_l->val:%0d\n",head_l->val);
//why head = head_l doesn't work??
head = head_l;
//The line below works
//*head = *head_l;
}
int main() {
node l0;
new(&l0);
printf("val:%0d\n",l0.val);
}
Function parameters receive only the value they are passed, not any reference or other connection to the argument. When the function is called, the parameter head is set to the value of a pointer to l0. Changing head does not change l0.
By referring to the post - Having a function change the value a pointer represents in C, I am able to find the root cause.
Let's say Address of head is [0x0000_0010] -> node object with NULL.
Address of head_l is [0x0003_DF58] -> node object with node.val=0.
head = head_l; only modify head from 0x0000_0010 to 0x0003_DF58.
*head = *head_l; modify [0x0000_0010] - the value of head points, to [0x0003_DF58] - the value of head_l points.
The latter one will change the destination value(NULL) to new value(node.val=0).

c programming how to write this in main

You can write the prototypes without the variable names?
int example(examplestruct *var1, examplestruct *var2);
void done(examplestruct *var1,FILE *f);
struct {
int* field1;
int field2;
}examplestruct;
Is it possible to write the prototypes without name variables?
Can anyone tell me if this is acceptable in C language? You can write the prototypes without the variable names?
Yes.
As for the second question:
If you want a function to be inside main(), then take the body of the function, put it in main() and make sure that the arguments that the function had are well handled.
This example will clear things up:
#include <stdio.h>
void print(int);
void inc_p(int);
int main(void) {
int num = 5;
print(num);
inc_p(num);
// to get rid of inc_p(), copy paste it's body inside main
// and you will get this
// a++;
// print(a);
// However, a was an argument, here you need to use
// the variable declared in main(), i.e. 'num'
num++;
print(num);
return 0;
}
void print(int a) {
printf("%d\n", a);
}
void inc_p(int a) {
a++;
print(a);
}

How to keep track of a variable with Clang's static analyzer?

Suppose I'm working with the following C snippet:
void inc(int *num) {*num++;}
void dec(int *num) {*num--;}
void f(int var) {
inc(&var);
dec(&var);
}
By using a static analyzer, I want to be able to tell if the value of var didn't change during the function's execution. I know I have to keep its state on my own (that's the point of writing a Clang checker), but I'm having troubles getting a unique reference of this variable.
For example: if I use the following API
void MySimpleChecker::checkPostCall(const CallEvent &Call,
CheckerContext &C) const {
SymbolRef MyArg = Call.getArgSVal(0).getAsSymbol();
}
I'd expect it to return a pointer to this symbol's representation in my checker's context. However, I always get 0 into MyArg by using it this way. This happens for both inc and dec functions in the pre and post callbacks.
What am I missing here? What concepts did I get wrong?
Note: I'm currently reading the Clang CFE Internals Manual and I've read the excellent How to Write a Checker in 24 Hours material. I still couldn't find my answer so far.
Interpretation of question
Specifically, you want to count the calls to inc and dec applied to each variable and report when they do not balance for some path in a function.
Generally, you want to know how to associate an abstract value, here a number, with a program variable, and be able to update and query that value along each execution path.
High-level answer
Whereas the tutorial checker SimpleStreamChecker.cpp associates an abstract value with the value stored in a variable, here we want associate an abstract value with the variable itself. That is what IteratorChecker.cpp does when tracking containers, so I based my solution on it.
Within the static analyzer's abstract state, each variable is represented by a MemRegion object. So the first step is to make a map where MemRegion is the key:
REGISTER_MAP_WITH_PROGRAMSTATE(TrackVarMap, MemRegion const *, int)
Next, when we have an SVal that corresponds to a pointer to a variable, we can use SVal::getAsRegion to get the corresponding MemRegion. For instance, given a CallEvent, call, with a first argument that is a pointer, we can do:
if (MemRegion const *region = call.getArgSVal(0).getAsRegion()) {
to get the region that the pointer points at.
Then, we can access our map using that region as its key:
state = state->set<TrackVarMap>(region, newValue);
Finally, in checkDeadSymbols, we use SymbolReaper::isLiveRegion to detect when a region (variable) is going out of scope:
const TrackVarMapTy &Map = state->get<TrackVarMap>();
for (auto const &I : Map) {
MemRegion const *region = I.first;
int delta = I.second;
if (SymReaper.isLiveRegion(region) || (delta==0))
continue; // Not dead, or unchanged; skip.
Complete example
To demonstrate, here is a complete checker that reports unbalanced use of inc and dec:
// TrackVarChecker.cpp
// https://stackoverflow.com/questions/23448540/how-to-keep-track-of-a-variable-with-clangs-static-analyzer
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
#include "clang/StaticAnalyzer/Core/Checker.h"
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
using namespace clang;
using namespace ento;
namespace {
class TrackVarChecker
: public Checker< check::PostCall,
check::DeadSymbols >
{
mutable IdentifierInfo *II_inc, *II_dec;
mutable std::unique_ptr<BuiltinBug> BT_modified;
public:
TrackVarChecker() : II_inc(nullptr), II_dec(nullptr) {}
void checkPostCall(CallEvent const &Call, CheckerContext &C) const;
void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
};
} // end anonymous namespace
// Map from memory region corresponding to a variable (that is, the
// variable itself, not its current value) to the difference between its
// current and original value.
REGISTER_MAP_WITH_PROGRAMSTATE(TrackVarMap, MemRegion const *, int)
void TrackVarChecker::checkPostCall(CallEvent const &call, CheckerContext &C) const
{
const FunctionDecl *FD = dyn_cast<FunctionDecl>(call.getDecl());
if (!FD || FD->getKind() != Decl::Function) {
return;
}
ASTContext &Ctx = C.getASTContext();
if (!II_inc) {
II_inc = &Ctx.Idents.get("inc");
}
if (!II_dec) {
II_dec = &Ctx.Idents.get("dec");
}
if (FD->getIdentifier() == II_inc || FD->getIdentifier() == II_dec) {
// We expect the argument to be a pointer. Get the memory region
// that the pointer points at.
if (MemRegion const *region = call.getArgSVal(0).getAsRegion()) {
// Increment the associated value, creating it first if needed.
ProgramStateRef state = C.getState();
int delta = (FD->getIdentifier() == II_inc)? +1 : -1;
int const *curp = state->get<TrackVarMap>(region);
int newValue = (curp? *curp : 0) + delta;
state = state->set<TrackVarMap>(region, newValue);
C.addTransition(state);
}
}
}
void TrackVarChecker::checkDeadSymbols(
SymbolReaper &SymReaper, CheckerContext &C) const
{
ProgramStateRef state = C.getState();
const TrackVarMapTy &Map = state->get<TrackVarMap>();
for (auto const &I : Map) {
// Check for a memory region (variable) going out of scope that has
// a non-zero delta.
MemRegion const *region = I.first;
int delta = I.second;
if (SymReaper.isLiveRegion(region) || (delta==0)) {
continue; // Not dead, or unchanged; skip.
}
//llvm::errs() << region << " dead with delta " << delta << "\n";
if (ExplodedNode *N = C.generateNonFatalErrorNode()) {
if (!BT_modified) {
BT_modified.reset(
new BuiltinBug(this, "Delta not zero",
"Variable changed from its original value."));
}
C.emitReport(llvm::make_unique<BugReport>(
*BT_modified, BT_modified->getDescription(), N));
}
}
}
void ento::registerTrackVarChecker(CheckerManager &mgr) {
mgr.registerChecker<TrackVarChecker>();
}
bool ento::shouldRegisterTrackVarChecker(const LangOptions &LO) {
return true;
}
To hook this in to the rest of Clang, add entries to:
clang/include/clang/StaticAnalyzer/Checkers/Checkers.td and
clang/lib/StaticAnalyzer/Checkers/CMakeLists.txt
Example input to test it:
// trackvar.c
// Test for TrackVarChecker.
// The behavior of these functions is hardcoded in the checker.
void inc(int *num);
void dec(int *num);
void call_inc(int var) {
inc(&var);
} // reported
void call_inc_dec(int var) {
inc(&var);
dec(&var);
} // NOT reported
void if_inc(int var) {
if (var > 2) {
inc(&var);
}
} // reported
void indirect_inc(int val) {
int *p = &val;
inc(p);
} // reported
Sample run:
$ gcc -E -o trackvar.i trackvar.c
$ ~/bld/llvm-project/build/bin/clang -cc1 -analyze -analyzer-checker=alpha.core.TrackVar trackvar.i
trackvar.c:10:1: warning: Variable changed from its original value
}
^
trackvar.c:21:1: warning: Variable changed from its original value
}
^
trackvar.c:26:1: warning: Variable changed from its original value
}
^
3 warnings generated.
I think you missed the check that this call event is a call to your function inc/dec. You should have something like
void MySimpleChecker::checkPostCall(const CallEvent &Call,
CheckerContext &C) const {
const IdentifierInfo* callee = Call.getCalleeIdentifier();
if (callee->getName().str() == "inc" || callee->getName().str() == "dec")
SymbolRef MyArg = Call.getArgSVal(0).getAsSymbol();
}

About the parameter of function pthread_create?

We know that we call pthread like this:
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void* arg);
Hi guys, i want to know why the return type of third parameter is void*? why not void?
Because there is no way for a start function to know what kind of data a developer wants to return from the function they use a void* that can point to any type. It is up to the developer of the start function to then cast the void* to appropriate type he actually returned before using whatever the void* points to. So now the start function can return a pointer that may in actually point to anything. If the start function is declared to return void, it means this function returns nothing, then what if the developer wants the start function to return a int, a struct? For example:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
struct test {
char str[32];
int x;
};
void *func(void*) {
struct test *eg = (struct test *)malloc(sizeof(struct test));
strcpy(eg->str,"hello world");
eg->x = 42;
pthread_exit(eg);
}
int main (void) {
pthread_t id;
struct test *resp;
pthread_create(&id, NULL, func, NULL);
pthread_join(id,(void**)&resp);
printf("%s %d\n",resp->str,resp->x);
free(resp);
return 0;
}
More details on this post: What does void* mean and how to use it?

Resources