I am a new C programmer and the pointers + arrays are messing me over a lot. I don't get any errors and the code runs fine...until somewhere in the middle the code starts acting up because some element in an array is set to something other than I wanted it to be. I wanted to know if there is a program that visualizes the memory of the code after it has finished running?
Eg after I run:
#include<stdio.h>
int main(){
int array[2] = {0,1};
array[1] = 4;
printf("%d\n",array[1]);
}
It will show a block of memory where array has two elements with 0 and 4 .
Right now to avoid running into a problem where the array contains elements from previous action I clear the memory of that array by doing:
memset(tokenized,0,MAX_CHARS);
It seems to work, but I don't know if its actually doing what I think it's doing at the back end.
Edit: I am using Valgrind now and I just want to know, how do I know what line the error is referring to? For example I got this:
==24394== Source and destination overlap in strncpy(0x7ff000006, 0x7ff000006, 6)
==24394== at 0x4C2C236: strncpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==24394== by 0x400D8A: tokenize_quotes (in /home/zee/Documents/seng265/repos/assignment2/a.out)
==24394== by 0x40184E: main (in /home/zee/Documents/seng265/repos/assignment2/a.out)
==24394==
==24394== Conditional jump or move depends on uninitialised value(s)
==24394== at 0x4C2C007: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==24394== by 0x400E06: tokenize_quotes (in /home/zee/Documents/seng265/repos/assignment2/a.out)
==24394== by 0x40184E: main (in /home/zee/Documents/seng265/repos/assignment2/a.out)
==24394==
But I don't know what line the error is on? I know its something about strcpy Thanks!
Use Valgrind :
Valgrind is a GPL'd system for debugging and profiling Linux programs. With Valgrind's tool suite you can automatically detect many memory management and threading bugs, avoiding hours of frustrating bug-hunting, making your programs more stable. You can also perform detailed profiling to help speed up your programs.
valgrind is nice tool try using that.
avinash@ubuntu:~$ valgrind ./test
==2559== Memcheck, a memory error detector
==2559== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==2559== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==2559== Command: ./test
==2559==
4
==2559==
==2559== HEAP SUMMARY:
==2559== in use at exit: 0 bytes in 0 blocks
==2559== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==2559==
==2559== All heap blocks were freed -- no leaks are possible
==2559==
==2559== For counts of detected and suppressed errors, rerun with: -v
==2559== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
avinash@ubuntu:~$
I don't think Valgrind is the tool you are looking for: Valgrind is really really really good for checking uninitialised memory and invalid memory accesses (etc), but in this case, I don't Valgrind is going to answer the question.
If you want to check that memory is zero (ie you've reset it properly), you can use
char test = 0;
unsigned i;
for (i = 0; i < MAX_CHARS; i++) {
test |= tokenized[i];
}
printf("Memory %s zero\n", test == 0 ? "is" : "isn't");
(After the loop, test
will be 0 only if every single byte in tokenized
is zero.)
You won't want this permanently in your code though: you can trust memset
does what you want if you pass it the right parameters.
The error you are getting from Valgrind is a different problem: you happen to be calling strncpy
with two pointers that point to the same data, eg
strncpy(ptr, ptr, 6);
and that data is uninitialised. As in, it is essentially random: your program hasn't written anything to that block of memory yet.
(Also, if you compile with -g
(debugging symbols) Valgrind will give you more informative output, including line numbers.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.