Usage des pointeurs dans un objet
Bonsoir,
j'ai des difficultés à obtenir un code fonctionnel qui consiste à remplir un simple vector en utilisant le pointeur (membre private de ma classe ) qui pointe dessus.
Voilà mon code:
Le fichier flux.h
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#include <iostream>
#include <vector>
class img_stream {
public:
img_stream();
~img_stream();
void fill_stream(int img);
void cout_stream();
private:
std::vector<int>* img_queue;
}; |
Le fichier flux.cpp
Code:
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
|
#include "flux.h"
img_stream::img_stream()
{
std::vector<int>* img_queue = new std::vector <int> (0) ; //On crée un tableau vide
}
img_stream::~img_stream()
{
}
void img_stream::fill_stream(int img){
unsigned int size = static_cast<int>(this->img_queue->vector::size());
std::cout << size << std::endl;
while(size < 2){ //Initialement, j'avais utilisé le while en comparant directement la valeur de img_queue->size() mais cela affichait une erreur
this->img_queue->vector::push_back(img); // car size() serait constant.
size +=4;
}
}
void img_stream::cout_stream()
{
for(std::vector<int>::iterator it = this->img_queue->begin(); it!=this->img_queue->end(); it++){
std::cout << *it << std::endl;
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
Le fichier main.cpp:
#include "flux.h"
#include <vector>
int main(){
img_stream flux ;
int T = 3;
flux.img_stream::fill_stream(T);
flux.img_stream::cout_stream();
} |
Je ne suis pas à l'aise avec les pointeurs et je ne parviens pas à comprendre ce que la console affiche:
Code:
1 2 3
|
4253334149 \\Je m'attendais à trouver 2 ici
Erreur de segmentation |
En ce qui concerne l'erreur de segmentation voilà ce que j'obtiens avec valgrind:
Code:
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 45 46 47 48 49 50 51 52 53 54 55 56 57
|
!Damien@Damien-ThinkPad-X200:~/Documents/projet$ valgrind ./a.out --leak-check=full
==7998== Memcheck, a memory error detector
==7998== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==7998== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==7998== Command: ./a.out --leak-check=full
==7998==
==7998== Use of uninitialised value of size 8
==7998== at 0x401034: std::vector<int, std::allocator<int> >::size() const (in /home/Damien/Documents/projet/a.out)
==7998== by 0x400E3B: img_stream::fill_stream(int) (in /home/Damien/Documents/projet /a.out)
==7998== by 0x400CA1: main (in /home/Damien/Documents/projet/a.out)
==7998==
==7998== Use of uninitialised value of size 8
==7998== at 0x40103F: std::vector<int, std::allocator<int> >::size() const (in /home/Damien/Documents/projet/a.out)
==7998== by 0x400E3B: img_stream::fill_stream(int) (in /home/Damien/Documents/projet/a.out)
==7998== by 0x400CA1: main (in /home/Damien/Documents/projet/a.out)
==7998==
4253334149
==7998== Use of uninitialised value of size 8
==7998== at 0x40159C: __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >::__normal_iterator(int* const&) (in /home/Damien/Documents/projet/a.out)
==7998== by 0x4010E7: std::vector<int, std::allocator<int> >::begin() (in /home/Damien/Documents/projet/a.out)
==7998== by 0x400EA9: img_stream::cout_stream() (in /home/Damien/Documents/projet/a.out)
==7998== by 0x400CAD: main (in /home/Damien/Documents/projet/a.out)
==7998==
==7998== Use of uninitialised value of size 8
==7998== at 0x40159C: __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >::__normal_iterator(int* const&) (in /home/Damien/Documents/projet/a.out)
==7998== by 0x401133: std::vector<int, std::allocator<int> >::end() (in /home/Damien/Documents/projet/a.out)
==7998== by 0x400EBC: img_stream::cout_stream() (in /home/Damien/Documents/projet/a.out)
==7998== by 0x400CAD: main (in /home/Damien/Documents/projet/a.out)
==7998==
==7998== Invalid read of size 4
==7998== at 0x400EE4: img_stream::cout_stream() (in /home/Damien/Documents/projet/a.out)
==7998== by 0x400CAD: main (in /home/Damien/Documents/projet/a.out)
==7998== Address 0x41ff894156415741 is not stack'd, malloc'd or (recently) free'd
==7998==
==7998==
==7998== Process terminating with default action of signal 11 (SIGSEGV)
==7998== General Protection Fault
==7998== at 0x400EE4: img_stream::cout_stream() (in /home/Damien/Documents/projet/a.out)
==7998== by 0x400CAD: main (in /home/Damien/Documents/projet/a.out)
==7998==
==7998== HEAP SUMMARY:
==7998== in use at exit: 72,728 bytes in 2 blocks
==7998== total heap usage: 3 allocs, 1 frees, 73,752 bytes allocated
==7998==
==7998== LEAK SUMMARY:
==7998== definitely lost: 24 bytes in 1 blocks
==7998== indirectly lost: 0 bytes in 0 blocks
==7998== possibly lost: 0 bytes in 0 blocks
==7998== still reachable: 72,704 bytes in 1 blocks
==7998== suppressed: 0 bytes in 0 blocks
==7998== Rerun with --leak-check=full to see details of leaked memory
==7998==
==7998== For counts of detected and suppressed errors, rerun with: -v
==7998== Use --track-origins=yes to see where uninitialised values come from
==7998== ERROR SUMMARY: 5 errors from 5 contexts (suppressed: 0 from 0)
Erreur de segmentation |
Merci d'avance de votre aide!
Cordialement.